How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • const ajv = new Ajv({ allErrors: true })
    const validate = ajv.compile(schema.valueOf())
    let user = {}
    let valid = validate(user)
    console.log({ valid }) //=> {valid: false}
    console.log(validate.errors) //=> {valid: false}
    Docs
  • const fs = require("fs")
    const path = require("path")
    const Ajv = require("ajv")
    const standaloneCode = require("ajv/dist/standalone").default
    
    const schemaFoo = {
      $id: "#/definitions/Foo",
      $schema: "http://json-schema.org/draft-07/schema#",
      type: "object",
      properties: {
        foo: {"$ref": "#/definitions/Bar"}
      }
    }
    const schemaBar = {
      $id: "#/definitions/Bar",
      $schema: "http://json-schema.org/draft-07/schema#",
      type: "object",
      properties: {
        bar: {type: "string"},
      },
      "required": ["bar"]
    }
    
    // For CJS, it generates an exports array, will generate
    // `exports["#/definitions/Foo"] = ...;exports["#/definitions/Bar"] = ... ;`
    const ajv = new Ajv({schemas: [schemaFoo, schemaBar], code: {source: true}})
    let moduleCode = standaloneCode(ajv)
    
    // Now you can write the module code to file
    fs.writeFileSync(path.join(__dirname, "../consume/validate-cjs.js"), moduleCode)
    
    Docs
  • const uuid = require("uuid")
    
    const def = require("ajv-keywords/dist/definitions/dynamicDefaults")
    def.DEFAULTS.uuid = () => uuid.v4
    
    const schema = {
      dynamicDefaults: {id: "uuid"},
      properties: {id: {type: "string", format: "uuid"}},
    }
    
    const data = {}
    ajv.validate(schema, data) // true
    data // { id: 'a1183fbe-697b-4030-9bcc-cfeb282a9150' };
    
    const data1 = {}
    ajv.validate(schema, data1) // true
    data1 // { id: '5b008de7-1669-467a-a5c6-70fa244d7209' }
    
    Docs
  • ajv.addKeyword({
      keyword: "constant",
      validate: (schema, data) =>
        typeof schema == "object" && schema !== null ? deepEqual(schema, data) : schema === data,
      errors: false,
    })
    
    const schema = {
      constant: 2,
    }
    const validate = ajv.compile(schema)
    console.log(validate(2)) // true
    console.log(validate(3)) // false
    
    const schema = {
      constant: {foo: "bar"},
    }
    const validate = ajv.compile(schema)
    console.log(validate({foo: "bar"})) // true
    console.log(validate({foo: "baz"})) // false
    
    Docs
  • const ajv = new Ajv({processCode: transpileFunc})
    const validate = ajv.compile(schema) // transpiled es7 async function
    validate(data).then(successFunc).catch(errorFunc)
    
    Docs
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples