jsonpolice

2019-08-15

jsonpolice是什么

什么是jsonpolice,JSON Schema parser and validator

jsonpolice使用教程帮助文档

jsonpolice

A Javascript library implementing the JSON Schemadraft 7.

The library can optionally decorate parsed objects in order to have them return default values defined in the schema, for undefined properties.

travis buildCoverage Statusnpm version

Install

$ npm install jsonpolice

create(dataOrUri, options)

Create a new instance of schema validator.

  • dataOrUri, the schema to parse or a fully qualified URI to pass to retrieverto download the schema
  • options, parsing options, the following optional properties are supported:
    • scope(required), the current resolution scope (absolute URL) of URLs and paths.
    • registry, an object to use to cache resolved idand $refvalues. If no registry is passed, one is automatically created. Pass a registryif you are going to parse several schemas or URIs referencing the same idand $refvalues.
    • retriever, a function accepting a URL in input and returning a promise resolved to an object representing the data downloaded for the URI. Whenever a $refto a new URI is found, if the URI is not already cached in the store in use, it'll be fetched using this retriever. If not retrieveris passed and a URI needs to be downloaded, a no_retrieverexception is thrown. Refer to the documentation of jsonreffor sample retriever functions to use in the browser or with Node.js.

The function returns a Promise resolving to a new instance of Schema. Once created, a schema instance can be used repeatedly to validate data, calling the method Schema.validate.

Example

import * as jp from 'jsonpolice';

(async () => {

  const schema = jp.create({
    type: 'object',
    properties: {
      d: {
        type: 'string',
        format: 'date-time'
      },
      i: {
        type: 'integer'
      },
      b: {
        type: [ 'boolean', 'number' ]
      },
      c: {
        default: 5
      }
    }
  });

  try {
    const result = await schema.validate({
      d: (new Date()).toISOString(),
      i: 6,
      b: true
    });
  } catch(err) {
    // validation failed
  }

})();

Schema.validate(data _[, options]_)

Validates the input data

  • data, the data to parse
  • options, validation options, the following optional properties are supported:
    • setDefault, if truereturns the default value specified in the schema (if any) for undefined properties
    • removeAdditional, if truedeletes properties not validating against additionalProperties, without failing
    • context, if set to readdeletes writeOnly properties, if set to writedelete readOnly properties

Returns a decorated version of data, according to the specified options.

Example

Using the following schema:

{
  type: 'object',
  properties: {
    d: {
      type: 'string',
    },
    i: {
      type: 'integer'
    },
    b: {
      type: [ 'boolean', 'number' ]
    },
    c: {
      default: 5
    }
  }
}

And parsing the following data:

var output = schema.validate({
  d: 'test',
  i: 10,
  b: true
});

Produces the following output:

{
  "d": "test",
  "i": 10,
  "b": true,
  "c": 5
}

上一篇:jsonref
下一篇:React Native 混合开发与实现
相关教程
关注微信

扫码加入 JavaScript 社区

相关文章
暂无相关文章

首次访问,需要验证
微信扫码,关注即可
(仅需验证一次)

欢迎加入 JavaScript 社区

号内回复关键字:

回到顶部