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

  • require("ajv-keywords")(ajv, ["transform"])
    
    const schema = {
      type: "array",
      items: {
        type: "string",
        transform: ["trim", "toEnumCase"],
        enum: ["pH"],
      },
    }
    
    const data = ["ph", " Ph", "PH", "pH "]
    ajv.validate(schema, data)
    console.log(data) // ['pH','pH','pH','pH']
    
    Docs
  • const fs = require("fs")
    const path = require("path")
    const Ajv = require("ajv")
    const standaloneCode = require("ajv/dist/standalone").default
    
    const schema = {
      $id: "https://example.com/bar.json",
      $schema: "http://json-schema.org/draft-07/schema#",
      type: "object",
      properties: {
        bar: {type: "string"},
      },
      "required": ["bar"]
    }
    
    // The generated code will have a default export:
    // `module.exports = <validateFunctionCode>;module.exports.default = <validateFunctionCode>;`
    const ajv = new Ajv({code: {source: true}})
    const validate = ajv.compile(schema)
    let moduleCode = standaloneCode(ajv, validate)
    
    // Now you can write the module code to file
    fs.writeFileSync(path.join(__dirname, "../consume/validate-cjs.js"), moduleCode)
    
    Docs
  • require("ajv-keywords")(ajv, "transform")
    
    const schema = {
      type: "array",
      items: {
        type: "string",
        transform: ["trim", "toLowerCase"],
      },
    }
    
    const data = ["  MixCase  "]
    ajv.validate(schema, data)
    console.log(data) // ['mixcase']
    
    Docs
  • 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
  • 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
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples