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.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
  • 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: "range",
      type: "number",
      compile([min, max], parentSchema) {
        return parentSchema.exclusiveRange === true
          ? (data) => data > min && data < max
          : (data) => data >= min && data <= max
      },
      errors: false,
      metaSchema: {
        // schema to validate keyword value
        type: "array",
        items: [{type: "number"}, {type: "number"}],
        minItems: 2,
        additionalItems: false,
      },
    })
    
    const schema = {
      range: [2, 4],
      exclusiveRange: true,
    }
    const validate = ajv.compile(schema)
    console.log(validate(2.01)) // true
    console.log(validate(3.99)) // true
    console.log(validate(2)) // false
    console.log(validate(4)) // 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
  • 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({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
  • apiValidator.loadSchemas = parser
        .dereference(path.resolve('src/backend/schema/index.json'))
        .then((schema) => {
            ajv.addSchema(schema);
            return schema;
        })
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">module</span> apiValidator <span class="token keyword">function</span> <span class="token function">apiValidator</span><span class="token punctuation">(</span><span class="token parameter">schema<span class="token operator">:</span> any<span class="token punctuation">,</span> payload<span class="token operator">:</span> any</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span><span class="token builtin">any</span><span class="token operator">></span></code><div class="typing-tooltip-markup"><em>@param</em> <code>schema</code><br><br><em>@param</em> <code>payload</code><br><br><em>@returns</em></div></pre>
    /jc21/docker-registry-ui mit
    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
  • 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
  • constructor(validatorOptions) {
        
        //...
    const validator = ajv({
          allErrors: true,
          errorDataPath: 'property',
          jsonPointers: true,
          verbose: true,
          schemas: this.schemas,
          schemaId: 'auto',
        });
    
        validator.addMetaSchema(jsonSchemaDraft06);
        ajvMergePatch(validator);
        this._addCustomFormats(validator);
        
        //...
    }
    
      
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript">constructor <span class="token function">SchemaValidator</span><span class="token punctuation">(</span>validatorOptions<span class="token operator">:</span> <span class="token punctuation">{</span> minManifestVersion<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span> maxManifestVersion<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span> schemas<span class="token operator">?</span><span class="token operator">:</span> object<span class="token punctuation">;</span> schemaObject<span class="token operator">?</span><span class="token operator">:</span> object<span class="token punctuation">;</span> themeSchemaObject<span class="token operator">?</span><span class="token operator">:</span> object<span class="token punctuation">;</span> messagesSchemaObject<span class="token operator">?</span><span class="token operator">:</span> object<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">:</span> SchemaValidator</code><div class="typing-tooltip-markup">Create a SchemaValidator instance, optionally customized by the given options.<br><br><em>@param</em> <code>validatorOptions</code></div></pre>
    /mozilla/addons-linter mpl-2.0
    0
  • //...
    //support draft-04
    validator.addMetaSchema(draft4);
    
    Object.keys(Schemas).forEach(function (key) {
      if (key === 'default') {
        return;
      }
      let val = Schemas[key];
    
      validator.addSchema(val, key);
    });
    
    
    
    //...
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> validator<span class="token operator">:</span> Ajv<span class="token punctuation">.</span>Ajv</code></pre>
    /adiwg/mdEditor GPL-3.0
    0
  • async function test() {
      
      //...
    const schema = JSON.parse(await fs.readFile("./schema/buttplug-schema.json", "utf-8"));
      const validator = new ajv();
      validator.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json"));
      const jsonValidator = validator.compile(schema);
      
      //...
    }
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">function</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span><span class="token keyword">void</span><span class="token operator">></span></code></pre>
    /buttplugio/buttplug bsd-3-clause
    0
  • const draft6MetaSchema = require("ajv/dist/refs/json-schema-draft-06.json")
    ajv.addMetaSchema(draft6MetaSchema)
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> draft6MetaSchema</code></pre>
    Docs
    0
  • //...
    const conf = require('../conf');
    
    const ajv = new Ajv({
        schemaId: 'auto',
        allErrors: true,
        verbose: true,
    });
    
    ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
    ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
    
    function URIfromId(id) {
        return url.parse(id).path;
    }
    
    
    
    //...
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> conf</code></pre>
    /openintegrationhub/openintegrationhub apache-2.0
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples