@vitalets/micro-schema

0.1.3 • Public • Published

micro-schema

Actions Status npm license

JavaScript implementation of JSON micro schema validation format.

Contents

Installation

npm install @vitalets/micro-schema

Usage

  1. Require validate function:

    const { validate } = require('@vitalets/micro-schema');
  2. Define schema:

    const schema = {
      productId: {
        $type: 'number',
        $required: true
      },
      productName: {
        $type: 'string',
        $required: true,
        $maxLength: 255
      },
      tags: [{
        $type: 'string'
      }]
    };
  3. Validate object:

    const object = {
      productId: '1',
      productName: undefined,
      tags: [42]
    };
    
    const errors = validate(schema, object);
  4. Handle validation errors:

    [
      {
        "validator": "$type",
        "path": "productId",
        "expectedType": "number",
        "actualType": "string"
      },
      {
        "validator": "$required",
        "path": "productName"
      },
      {
        "validator": "$type",
        "path": "tags.0",
        "expectedType": "string",
        "actualType": "number"
      }
    ]

Docs

Please see JSON micro schema docs for all available validators.

License

MIT @ Vitaliy Potapov

Readme

Keywords

Package Sidebar

Install

npm i @vitalets/micro-schema

Weekly Downloads

1

Version

0.1.3

License

MIT

Unpacked Size

10.3 kB

Total Files

7

Last publish

Collaborators

  • vitalets