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()
    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
  • const ajv = new Ajv({loadSchema: loadSchema})
    
    ajv.compileAsync(schema).then(function (validate) {
      const valid = validate(data)
      // ...
    })
    
    async function loadSchema(uri) {
      const res = await request.json(uri)
      if (res.statusCode >= 400) throw new Error("Loading error: " + res.statusCode)
      return res.body
    }
    
    Docs
  • const ajv = new Ajv({processCode: transpileFunc})
    const validate = ajv.compile(schema) // transpiled es7 async function
    validate(data).then(successFunc).catch(errorFunc)
    
    Docs
  • const otherLogger = new OtherLogger()
    const ajv = new Ajv({
      logger: {
        log: console.log.bind(console),
        warn: function warn() {
          otherLogger.logWarn.apply(otherLogger, arguments)
        },
        error: function error() {
          otherLogger.logError.apply(otherLogger, arguments)
          console.error.apply(console, arguments)
        },
      },
    })
    
    Docs
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples