@10pearls/revalidator 中文文档教程

发布于 3年前 浏览 27 项目主页 更新于 3年前

revalidator Build Status

JSONSchema 兼容性为主要目标的跨浏览器/node.js 验证器。

Example

revalidator 的核心简单明了: revalidator.validate(obj, schema)

  var revalidator = require('revalidator');

  console.dir(revalidator.validate(someObject, {
    properties: {
      url: {
        description: 'the url the object should be stored at',
        type: 'string',
        pattern: '^/[^#%&*{}\\:<>?\/+]+$',
        required: true
      },
      challenge: {
        description: 'a means of protecting data (insufficient for production, used as example)',
        type: 'string',
        minLength: 5
      },
      body: {
        description: 'what to store at the url',
        type: 'any',
        default: null
      }
    }
  }));

这将返回一个值,指示 obj 是否符合到 schema。 如果没有,将返回一个描述性对象,其中包含验证遇到的错误。

  {
    valid: true // or false
    errors: [/* Array of errors if valid is false */]
  }

在浏览器中,只需包含 revalidator.js 即可在 window.validate 上公开验证功能。

Installation

Installing npm (node package manager)

  $ curl http://npmjs.org/install.sh | sh

Installing revalidator

  $ [sudo] npm install revalidator

Usage

revalidator 将 json-schema 作为输入来验证对象。

revalidator.validate (obj, schema, options)

这将返回一个值,指示 obj 是否符合 schema。 如果没有,将返回一个描述性对象,其中包含验证遇到的错误。

{
  valid: true // or false
  errors: [/* Array of errors if valid is false */]
}

Available Options

  • validateFormats: Enforce format constraints (default true)
  • validateFormatsStrict: When validateFormats is true treat unrecognized formats as validation errors (default false)
  • validateFormatExtensions: When validateFormats is true also validate formats defined in validate.formatExtensions (default true)
  • additionalProperties: When additionalProperties is true allow additional unvisited properties on the object. (default true)
  • cast: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. "42" => 42, but "forty2" => "forty2" for the integer type.

Schema

对于属性, 是作为验证输入给出的值,其中 预期值 是以下字段

required

的值 如果为真,则该值不应未定义

{ required: true }

allowEmpty

如果false,该值不能为空字符串

{ allowEmpty: false }

type

type of value 应等于

{ type: 'string' }
{ type: 'number' }
{ type: 'integer' }
{ type: 'array' }
{ type: 'boolean' }
{ type: 'object' }
{ type: 'null' }
{ type: 'any' }
{ type: ['boolean', 'string'] }

pattern

期望值 期望值正则表达式需要被值满足 值

{ pattern: /^[a-z]+$/ }

maxLength

的长度必须大于或等于expected value value

{ maxLength: 8 }

minLength

的长度必须小于等于expected value

{ minLength: 8 }

minimum

必须大于等于expected value

{ minimum: 10 }

maximum

必须小于等于expected value

{ maximum: 10 }

allowEmpty

不能为空

{ allowEmpty: false }

exclusiveMinimum

Value必须大于expected value

{ exclusiveMinimum: 9 }

exclusiveMaximum

value必须小于预期值

{ exclusiveMaximum: 11 }

divisibleBy

值必须可以被预期值整除

{ divisibleBy: 5 }
{ divisibleBy: 0.5 }

minItems

值必须包含多于预期的项目数

{ minItems: 2 }

maxItems

值必须包含少于预期的项目数 值必须包含一组

{ maxItems: 5 }

uniqueItems

唯一的值

{ uniqueItems: true }

enum

值必须存在于预期值数组中

{ enum: ['month', 'year'] }

format

值必须是有效的 f ormat

{ format: 'url' }
{ format: 'email' }
{ format: 'ip-address' }
{ format: 'ipv6' }
{ format: 'date-time' }
{ format: 'date' }
{ format: 'time' }
{ format: 'color' }
{ format: 'host-name' }
{ format: 'utc-millisec' }
{ format: 'regex' }

conform

值必须符合预期值表示的约束

{ conform: function (v) {
    if (v%3==1) return true;
    return false;
  }
}

dependencies

仅当相关值有效时值才有效

{
  town: { required: true, dependencies: 'country' },
  country: { maxLength: 3, required: true }
}

Nested Schema

我们还允许嵌套模式

{
  properties: {
    title: {
      type: 'string',
      maxLength: 140,
      required: true
    },
    author: {
      type: 'object',
      required: true,
      properties: {
        name: {
          type: 'string',
          required: true
        },
        email: {
          type: 'string',
          format: 'email'
        }
      }
    }
  }
}

Custom Messages

我们还允许针对不同约束的自定义消息

{
  type: 'string',
  format: 'url'
  messages: {
    type: 'Not a string type',
    format: 'Expected format is a url'
  }
{
  conform: function () { ... },
  message: 'This can be used as a global message'
}

Tests

所有测试均使用 vows 并且应该与 npm 一起运行:

  $ npm test

Author: Charlie Robbins, Alexis Sellier

Contributors: Fedor Indutny, Bradley Meck, Laurie Harper, Martijn Swaagman

License: Apache 2.0

revalidator Build Status

A cross-browser / node.js validator with JSONSchema compatibility as the primary goal.

Example

The core of revalidator is simple and succinct: revalidator.validate(obj, schema):

  var revalidator = require('revalidator');

  console.dir(revalidator.validate(someObject, {
    properties: {
      url: {
        description: 'the url the object should be stored at',
        type: 'string',
        pattern: '^/[^#%&*{}\\:<>?\/+]+$',
        required: true
      },
      challenge: {
        description: 'a means of protecting data (insufficient for production, used as example)',
        type: 'string',
        minLength: 5
      },
      body: {
        description: 'what to store at the url',
        type: 'any',
        default: null
      }
    }
  }));

This will return with a value indicating if the obj conforms to the schema. If it does not, a descriptive object will be returned containing the errors encountered with validation.

  {
    valid: true // or false
    errors: [/* Array of errors if valid is false */]
  }

In the browser, the validation function is exposed on window.validate by simply including revalidator.js.

Installation

Installing npm (node package manager)

  $ curl http://npmjs.org/install.sh | sh

Installing revalidator

  $ [sudo] npm install revalidator

Usage

revalidator takes json-schema as input to validate objects.

revalidator.validate (obj, schema, options)

This will return with a value indicating if the obj conforms to the schema. If it does not, a descriptive object will be returned containing the errors encountered with validation.

{
  valid: true // or false
  errors: [/* Array of errors if valid is false */]
}

Available Options

  • validateFormats: Enforce format constraints (default true)
  • validateFormatsStrict: When validateFormats is true treat unrecognized formats as validation errors (default false)
  • validateFormatExtensions: When validateFormats is true also validate formats defined in validate.formatExtensions (default true)
  • additionalProperties: When additionalProperties is true allow additional unvisited properties on the object. (default true)
  • cast: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. "42" => 42, but "forty2" => "forty2" for the integer type.

Schema

For a property an value is that which is given as input for validation where as an expected value is the value of the below fields

required

If true, the value should not be undefined

{ required: true }

allowEmpty

If false, the value must not be an empty string

{ allowEmpty: false }

type

The type of value should be equal to the expected value

{ type: 'string' }
{ type: 'number' }
{ type: 'integer' }
{ type: 'array' }
{ type: 'boolean' }
{ type: 'object' }
{ type: 'null' }
{ type: 'any' }
{ type: ['boolean', 'string'] }

pattern

The expected value regex needs to be satisfied by the value

{ pattern: /^[a-z]+$/ }

maxLength

The length of value must be greater than or equal to expected value

{ maxLength: 8 }

minLength

The length of value must be lesser than or equal to expected value

{ minLength: 8 }

minimum

Value must be greater than or equal to the expected value

{ minimum: 10 }

maximum

Value must be lesser than or equal to the expected value

{ maximum: 10 }

allowEmpty

Value may not be empty

{ allowEmpty: false }

exclusiveMinimum

Value must be greater than expected value

{ exclusiveMinimum: 9 }

exclusiveMaximum

Value must be lesser than expected value

{ exclusiveMaximum: 11 }

divisibleBy

Value must be divisible by expected value

{ divisibleBy: 5 }
{ divisibleBy: 0.5 }

minItems

Value must contain more than expected number of items

{ minItems: 2 }

maxItems

Value must contain fewer than expected number of items

{ maxItems: 5 }

uniqueItems

Value must hold a unique set of values

{ uniqueItems: true }

enum

Value must be present in the array of expected values

{ enum: ['month', 'year'] }

format

Value must be a valid format

{ format: 'url' }
{ format: 'email' }
{ format: 'ip-address' }
{ format: 'ipv6' }
{ format: 'date-time' }
{ format: 'date' }
{ format: 'time' }
{ format: 'color' }
{ format: 'host-name' }
{ format: 'utc-millisec' }
{ format: 'regex' }

conform

Value must conform to constraint denoted by expected value

{ conform: function (v) {
    if (v%3==1) return true;
    return false;
  }
}

dependencies

Value is valid only if the dependent value is valid

{
  town: { required: true, dependencies: 'country' },
  country: { maxLength: 3, required: true }
}

Nested Schema

We also allow nested schema

{
  properties: {
    title: {
      type: 'string',
      maxLength: 140,
      required: true
    },
    author: {
      type: 'object',
      required: true,
      properties: {
        name: {
          type: 'string',
          required: true
        },
        email: {
          type: 'string',
          format: 'email'
        }
      }
    }
  }
}

Custom Messages

We also allow custom messages for different constraints

{
  type: 'string',
  format: 'url'
  messages: {
    type: 'Not a string type',
    format: 'Expected format is a url'
  }
{
  conform: function () { ... },
  message: 'This can be used as a global message'
}

Tests

All tests are written with vows and should be run with npm:

  $ npm test

Author: Charlie Robbins, Alexis Sellier

Contributors: Fedor Indutny, Bradley Meck, Laurie Harper, Martijn Swaagman

License: Apache 2.0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文