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({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
    }
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ajv</span> <span class="token keyword">import</span> ajv <span class="token keyword">const</span> ajv<span class="token operator">:</span> Ajv</code></pre>
    Docs
    0
  • const Ajv = require("ajv") // version >= v7.0.0
    const ajv = new Ajv({code: {source: true}}) // this option is required to generate standalone code
    const standaloneCode = require("ajv/dist/standalone").default
    
    const schema = {
      $id: "https://example.com/object.json",
      type: "object",
      properties: {
        foo: {
          type: "string",
          pattern: "^[a-z]+$",
        },
      },
    }
    
    // 1. generate module with a single default export (CommonJS and ESM compatible):
    const validate = ajv.compile(schema)
    let moduleCode = standaloneCode(ajv, validate)
    
    // 2. pass map of schema IDs to generate multiple exports,
    // it avoids code duplication if schemas are mutually recursive or have some share elements:
    let moduleCode = standaloneCode(ajv, {
      validateObject: "https://example.com/object.json",
    })
    
    // 3. or generate module with all schemas added to the instance (excluding meta-schemas),
    // export names would use schema IDs (or keys passed to addSchema method):
    let moduleCode = standaloneCode(ajv)
    
    // now you can
    // write module code to file
    const fs = require("fs")
    const path = require("path")
    fs.writeFileSync(path.join(__dirname, "/validate.js"), moduleCode)
    
    // ... or require module from string
    const requireFromString = require("require-from-string")
    const standaloneValidate = requireFromString(moduleCode) // for a single default export
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> Ajv</code></pre>
    Docs
    0
  • 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
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ajv</span> <span class="token keyword">import</span> ajv</code></pre>
    Docs
    0
  • const ajv = new Ajv({coerceTypes: true})
    const schema = {
      type: "object",
      properties: {
        foo: {type: "number"},
        bar: {type: "boolean"},
      },
      required: ["foo", "bar"],
    }
    
    const data = {foo: "1", bar: "false"}
    
    const validate = ajv.compile(schema)
    
    console.log(validate(data)) // true
    console.log(data) // { "foo": 1, "bar": false }
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ajv</span> <span class="token keyword">import</span> ajv <span class="token keyword">const</span> ajv<span class="token operator">:</span> Ajv</code></pre>
    Docs
    0
  • const ajv = new Ajv({removeAdditional: true})
    const schema = {
      additionalProperties: false,
      properties: {
        foo: {type: "number"},
        bar: {
          additionalProperties: {type: "number"},
          properties: {
            baz: {type: "string"},
          },
        },
      },
    }
    
    const data = {
      foo: 0,
      additional1: 1, // will be removed; `additionalProperties` == false
      bar: {
        baz: "abc",
        additional2: 2, // will NOT be removed; `additionalProperties` != false
      },
    }
    
    const validate = ajv.compile(schema)
    
    console.log(validate(data)) // true
    console.log(data) // { "foo": 0, "bar": { "baz": "abc", "additional2": 2 }
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ajv</span> <span class="token keyword">import</span> ajv <span class="token keyword">const</span> ajv<span class="token operator">:</span> Ajv</code></pre>
    Docs
    0
  • const ajv = new Ajv({coerceTypes: "array"})
    const schema = {
      properties: {
        foo: {type: "array", items: {type: "number"}},
        bar: {type: "boolean"},
      },
    }
    
    const data = {foo: "1", bar: ["false"]}
    
    const validate = ajv.compile(schema)
    
    console.log(validate(data)) // true
    console.log(data) // { "foo": [1], "bar": false }
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ajv</span> <span class="token keyword">import</span> ajv <span class="token keyword">const</span> ajv<span class="token operator">:</span> Ajv</code></pre>
    Docs
    0
  • const ajv = new Ajv()
    const validate = ajv.addSchema(defsSchema).compile(schema)
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ajv</span> <span class="token keyword">import</span> ajv <span class="token keyword">const</span> ajv<span class="token operator">:</span> Ajv</code></pre>
    Docs
    0
  • function (resolve, reject) {
    		if (!payload) {
    			
    			//...
    } else {
    			try {
    				
    				//...
    if (valid && !validate.errors) {
    					
    					//...
    } else {
    					let message = ajv.errorsText(validate.errors);
    					reject(new error.InternalValidationError(message));
    				}
    
    			} catch (err) {
    				
    				//...
    }
    
    		}
    
    	}
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>parameter<span class="token punctuation">)</span> <span class="token function-variable function">resolve</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">value<span class="token operator">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">void</span></code></pre>
    /jc21/nginx-proxy-manager mit
    0
  • import myFormats from "./my-formats"
    import Ajv, {_} from "ajv"
    const ajv = new Ajv({
      formats: myFormats,
      code: {
        source: true,
        formats: _`require("./my-formats")`,
      },
    })
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> myFormats</code></pre>
    Docs
    1
  • import {_, nil, KeywordCxt} from Ajv
    
    ajv.addKeyword({
      keyword: "range",
      type: "number",
      code(cxt: KeywordCxt) {
        const {schema, parentSchema, data} = cxt
        const [min, max] = schema
        const eq: Code = parentSchema.exclusiveRange ? _`=` : nil
        cxt.fail(_`${data} <${eq} ${min} || ${data} >${eq} ${max}`)
      },
      metaSchema: {
        type: "array",
        items: [{type: "number"}, {type: "number"}],
        minItems: 2,
        additionalItems: false,
      },
    })
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> _</code></pre>
    Docs
    0
  • import {_, KeywordCxt} from Ajv
    
    ajv.addKeyword({
      keyword: "even",
      type: "number",
      schemaType: "boolean",
      // $data: true // to support [$data reference](./guide/combining-schemas.md#data-reference), ...
      code(cxt: KeywordCxt) {
        const {data, schema} = cxt
        const op = schema ? _`!==` : _`===`
        cxt.fail(_`${data} %2 ${op} 0`) // ... the only code change needed is to use `cxt.fail$data` here
      },
    })
    
    const schema = {even: true}
    const validate = ajv.compile(schema)
    console.log(validate(2)) // true
    console.log(validate(3)) // false
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> _</code></pre>
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples