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

  • ajv.addKeyword({
      keyword: "range",
      type: "number",
      macro: ([minimum, maximum]) => ({minimum, maximum}), // schema with keywords minimum and maximum
      // metaSchema: the same as in the example above
    })
    
    <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
  • 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
  • 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
  • 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
  • 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
  • const ajv = new Ajv({code: {es5: true}})
    
    <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
  • import Ajv from "ajv"
    const ajv = new Ajv()
    
    <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()
    
    <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({schemas: [schema, defsSchema]})
    const validate = ajv.getSchema("http://example.com/schemas/schema.json")
    
    <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
  • 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
  • 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']
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>property<span class="token punctuation">)</span> <span class="token keyword">require</span><span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    Docs
    0
  • require("ajv-keywords")(ajv, ["typeof", "instanceof"])
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>property<span class="token punctuation">)</span> <span class="token keyword">require</span><span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    Docs
    0
  • require('ajv-keywords')(ajv, 'select');
    
    const schema = {
      type: "object",
      required: ['kind'],
      properties: {
        kind: { type: 'string' }
      },
      select: { $data: '0/kind' },
      selectCases: {
        foo: {
          required: ['foo'],
          properties: {
            kind: {},
            foo: { type: 'string' }
          },
          additionalProperties: false
        },
        bar: {
          required: ['bar'],
          properties: {
            kind: {},
            bar: { type: 'number' }
          },
          additionalProperties: false
        }
      },
      selectDefault: {
        propertyNames: {
          not: { enum: ['foo', 'bar'] }
        }
      }
    };
    
    const validDataList = [
      { kind: 'foo', foo: 'any' },
      { kind: 'bar', bar: 1 },
      { kind: 'anything_else', not_bar_or_foo: 'any value' }
    ];
    
    const invalidDataList = [
      { kind: 'foo' }, // no property foo
      { kind: 'bar' }, // no property bar
      { kind: 'foo', foo: 'any', another: 'any value' }, // additional property
      { kind: 'bar', bar: 1, another: 'any value' }, // additional property
      { kind: 'anything_else', foo: 'any' } // property foo not allowed
      { kind: 'anything_else', bar: 1 } // property bar not allowed
    ];
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>property<span class="token punctuation">)</span> <span class="token keyword">require</span><span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    Docs
    0
  • 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']
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>property<span class="token punctuation">)</span> <span class="token keyword">require</span><span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    Docs
    0
  • const Ajv = require("ajv")
    const ajv = new Ajv()
    require("ajv-keywords")(ajv)
    
    ajv.validate({instanceof: "RegExp"}, /.*/) // true
    ajv.validate({instanceof: "RegExp"}, ".*") // false
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> Ajv</code></pre>
    Docs
    0
  • serialize({
        haxorXSS: '</script>'
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token builtin">any</span></code></pre>
    Docs
    0
  • serialize(obj, {unsafe: true});
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token builtin">any</span></code></pre>
    Docs
    0
  • function buildEmbeddedCode(data, webpackAssets) {
      
      //...
    return `
        <script>
          (function() {
            var namespace = '${buildConst.namespace}';
            var pkg = '${buildConst.pkgName}';
            if (typeof window != 'undefined') {
              if (!window.hasOwnProperty(namespace)) {
                window[namespace] = {};
              }
              if (window[namespace] && !window[namespace].hasOwnProperty(pkg)) {
                window[namespace][pkg] = [];
              }
              if (Array.isArray(window[namespace][pkg])) {
                var data = ${serialize(dataWithUuid)};
                window[namespace][pkg].push(data);
              }
            }
          })()
        </script>
        <div id=${uuid}></div>
        ${_.map(chunks, chunk => {
          
          //...
    }).join('')}
        ${_.map(bundles, bundle => {
          
          //...
    }).join('')}
      `
    }
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">function</span> <span class="token function">buildEmbeddedCode</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> <span class="token punctuation">{</span> chapters<span class="token operator">:</span> any<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> embeddedItems<span class="token operator">:</span> any<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> isFullWidth<span class="token operator">:</span> boolean<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> webpackAssets<span class="token operator">:</span> <span class="token punctuation">{</span> chunks<span class="token operator">:</span> string<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> bundles<span class="token operator">:</span> string<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span></code><div class="typing-tooltip-markup"><em>@export</em><br><br><em>@param</em> <code>data</code> — Data for Dual Channel Root React Component<br><br><em>@param</em> <code>webpackAssets</code> — webpack bundles and chunks<br><br><em>@returns</em> — embedded code</div></pre>
    /twreporter/orangutan-monorepo mit
    0
  • serialize(obj, {isJSON: true});
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token builtin">any</span></code></pre>
    Docs
    0
  • serialize(obj, {ignoreFunction: true});
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token builtin">any</span></code></pre>
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples