@5alid/schema 中文文档教程

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

Installation

npm <代码>npm i @5alid/schema yarn yarn 添加@5alid/schema

Usage

import createSchema from '@5alidz/schema';

const Person = createSchema({
  name: {
    type: 'string',
    minLength: 5,
    maxLength: 20,
  },
  age: {
    type: 'number',
    min: 13,
    max: 120,
  },
  pets: {
    type: 'array',
    optional: true,
    of: {
      type: 'object',
      shape: {
        name: {
          type: 'string',
        },
        animal: {
          type: 'string',
        },
      },
    },
  },
});

const errors = Person.validate({
  name: 'John Doe',
  age: 42,
  pets: [
    { name: 'fluffy', animal: 'dog' },
    { name: 'pirate', animal: 'parrot' },
  ],
});

console.log(errors); // -> null

const errors2 = Person.validate({
  name: true,
  age: '42',
});

console.log(errors2); // -> [ { key: 'name', message: 'expected string but received "boolean"' }, { key: 'age', message: 'expected number but received "string"' }]

Installation

npm npm i @5alid/schema yarn yarn add @5alid/schema

Usage

import createSchema from '@5alidz/schema';

const Person = createSchema({
  name: {
    type: 'string',
    minLength: 5,
    maxLength: 20,
  },
  age: {
    type: 'number',
    min: 13,
    max: 120,
  },
  pets: {
    type: 'array',
    optional: true,
    of: {
      type: 'object',
      shape: {
        name: {
          type: 'string',
        },
        animal: {
          type: 'string',
        },
      },
    },
  },
});

const errors = Person.validate({
  name: 'John Doe',
  age: 42,
  pets: [
    { name: 'fluffy', animal: 'dog' },
    { name: 'pirate', animal: 'parrot' },
  ],
});

console.log(errors); // -> null

const errors2 = Person.validate({
  name: true,
  age: '42',
});

console.log(errors2); // -> [ { key: 'name', message: 'expected string but received "boolean"' }, { key: 'age', message: 'expected number but received "string"' }]
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文