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 ajv = new Ajv()
    const validate = ajv.compile<MyData>(schema)
    if (validate(data)) {
      // data is MyData here
      // ...
    } else {
      // DefinedError is a type for all pre-defined keywords errors,
      // validate.errors has type ErrorObject[] - to allow user-defined keywords with any error parameters.
      // Users can extend DefinedError to include the keywords errors they defined.
      for (const err of validate.errors as DefinedError[]) {
        switch (err.keyword) {
          case "maximum":
            console.log(err.limit)
            break
          case "pattern":
            console.log(err.pattern)
            break
          // ...
        }
      }
    }
    
    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
  • ajv.addFormat("byte", {
      type: "number",
      validate: (x) => x >= 0 && x <= 255 && x % 1 == 0,
    })
    
    Docs
  • const schema = {type: "number", range: [1, 3]}
    ajv.validate(schema, 1) // true
    ajv.validate(schema, 2) // true
    ajv.validate(schema, 3) // true
    ajv.validate(schema, 0.99) // false
    ajv.validate(schema, 3.01) // false
    
    const schema = {type: "number", exclusiveRange: [1, 3]}
    ajv.validate(schema, 1.01) // true
    ajv.validate(schema, 2) // true
    ajv.validate(schema, 2.99) // true
    ajv.validate(schema, 1) // false
    ajv.validate(schema, 3) // false
    
    Docs
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples