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 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
  • 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
  • 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 ESM, the export name needs to be a valid export name, it can not be `export const #/definitions/Foo = ...;` so we
    // need to provide a mapping between a valid name and the $id field. Below will generate
    // `export const Foo = ...;export const Bar = ...;`
    // This mapping would not have been needed if the `$ids` was just `Bar` and `Foo` instead of `#/definitions/Foo`
    // and `#/definitions/Bar` respectfully
    const ajv = new Ajv({schemas: [schemaFoo, schemaBar], code: {source: true, esm: true}})
    let moduleCode = standaloneCode(ajv, {
      "Foo": "#/definitions/Foo",
      "Bar": "#/definitions/Bar"
    })
    
    // Now you can write the module code to file
    fs.writeFileSync(path.join(__dirname, "../consume/validate-esm.mjs"), 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
  • 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
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples