0.1.3 • Published 4 years ago

@vitalets/micro-schema v0.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago