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({ processCode: transpileFunc });
    const validate = ajv.compile(schema); // transpiled es7 async function
    validate(data).then(successFunc).catch(errorFunc);
    
    Docs
    1
  • const ajv = new Ajv()
    
    ajv.addKeyword({
      keyword: "idExists"
      async: true,
      type: "number",
      validate: checkIdExists,
    })
    
    async function checkIdExists(schema, data) {
      // this is just an example, you would want to avoid SQL injection in your code
      const rows = await sql(`SELECT id FROM ${schema.table} WHERE id = ${data}`)
      return !!rows.length // true if record is found
    }
    
    const schema = {
      $async: true,
      properties: {
        userId: {
          type: "integer",
          idExists: {table: "users"},
        },
        postId: {
          type: "integer",
          idExists: {table: "posts"},
        },
      },
    }
    
    const validate = ajv.compile(schema)
    
    validate({userId: 1, postId: 19})
      .then(function (data) {
        console.log("Data is valid", data) // { userId: 1, postId: 19 }
      })
      .catch(function (err) {
        if (!(err instanceof Ajv.ValidationError)) throw err
        // data is invalid
        console.log("Validation errors:", err.errors)
      })
    
    Docs
    0
  • const ajv = new Ajv({ processCode: transpileFunc });
    const validate = ajv.compile(schema); // transpiled es7 async function
    validate(data).then(successFunc).catch(errorFunc);
    
    Docs
    1
  • const ajv = new Ajv()
    
    ajv.addKeyword({
      keyword: "idExists"
      async: true,
      type: "number",
      validate: checkIdExists,
    })
    
    async function checkIdExists(schema, data) {
      // this is just an example, you would want to avoid SQL injection in your code
      const rows = await sql(`SELECT id FROM ${schema.table} WHERE id = ${data}`)
      return !!rows.length // true if record is found
    }
    
    const schema = {
      $async: true,
      properties: {
        userId: {
          type: "integer",
          idExists: {table: "users"},
        },
        postId: {
          type: "integer",
          idExists: {table: "posts"},
        },
      },
    }
    
    const validate = ajv.compile(schema)
    
    validate({userId: 1, postId: 19})
      .then(function (data) {
        console.log("Data is valid", data) // { userId: 1, postId: 19 }
      })
      .catch(function (err) {
        if (!(err instanceof Ajv.ValidationError)) throw err
        // data is invalid
        console.log("Validation errors:", err.errors)
      })
    
    Docs
    0
  • 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
    0
  • 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
    0
  • const schema = {
      type: "object",
      required: ["foo"],
      properties: {
        foo: { type: "integer" },
      },
      additionalProperties: false,
      errorMessage: {
        type: "should be an object", // will not replace internal "type" error for the property "foo"
        required: "should have property foo",
        additionalProperties:
          "should not have properties other than foo",
      },
    };
    
    const validate = ajv.compile(schema);
    console.log(validate({ foo: "a", bar: 2 })); // false
    console.log(validate.errors); // processed errors
    
    Docs
    0
  • const schema = {
      type: "object",
      required: ["foo"],
      properties: {
        foo: { type: "integer" },
      },
      additionalProperties: false,
      errorMessage:
        "should be an object with an integer property foo only",
    };
    
    const validate = ajv.compile(schema);
    console.log(validate({ foo: "a", bar: 2 })); // false
    console.log(validate.errors); // processed errors
    
    Docs
    0
  • const schema = {
      type: "object",
      required: ["foo", "bar"],
      allOf: [
        {
          properties: {
            foo: { type: "integer", minimum: 2 },
            bar: { type: "string", minLength: 2 },
          },
          additionalProperties: false,
        },
      ],
      errorMessage: {
        properties: {
          foo: "data.foo should be integer >= 2",
          bar: "data.bar should be string with length >= 2",
        },
      },
    };
    
    const validate = ajv.compile(schema);
    console.log(validate({ foo: 1, bar: "a" })); // false
    console.log(validate.errors); // processed errors
    
    Docs
    0
  • const schema = {
      type: "object",
      required: ["foo", "bar"],
      allOf: [
        {
          properties: {
            foo: { type: "integer", minimum: 2 },
            bar: { type: "string", minLength: 2 },
          },
          additionalProperties: false,
        },
      ],
      errorMessage: {
        type: "data should be an object",
        properties: {
          foo: "data.foo should be integer >= 2",
          bar: "data.bar should be string with length >= 2",
        },
        _: 'data should have properties "foo" and "bar" only',
      },
    };
    
    const validate = ajv.compile(schema);
    console.log(validate({})); // false
    console.log(validate.errors); // processed errors
    
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples