xergiodf / jzor

JSON Schema Validator that let you know whata hell is happening!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jzor

build-status-master

JSON Schema Validator that let you know whata hell is happening!

jzor-logo

Motivation

I know we have thousands of consolidated json validators, but most of then don't provide one simple thing: a good way to track the validation errors.

JZOR provide detailed information about each validation and it's respective path, so, you can link your form directly to the error path if you want.

Install

In terminal, run:

npm install jzor

Preview

Create your schema:

const schema = {
  $type: 'object',
  props: {
    name: {
      $type: 'string',
      minLength: 2,
      maxLength: 50
    },
    age: {
      $type: 'number',
      positive: true
    },
    weapons: {
      $type: 'array',
      maxItems: 2,
      item: [
        {
          $type: 'object',
          props: {
            title: {
              $type: 'string',
              minLength: 3,
              maxLength: 50
            },
            damage: {
              $type: 'number',
              min: 0,
              max: 999
            }
          }
        }
      ]
    }
  }
}

Create the value to be validated:

const value = {
  name: 'Tr',
  age: -5,
  weapons: [
    {
      title: 'Storm Breaker',
      damage: 1000
    },
    1,
    {
      title: 'Mjolnir',
      damage: -5
    }
  ]
}

Validate:

const { validateSchema } = require('jzor')

const result = validateSchema(schema, value)

Get the result:

{
  valid: false,
  errors: {
    $root: {
      age: {
        positive: {
          message: 'The value should be a positive number',
          value: -5
        }
      },
      weapons: {
        $0: {
          $root: {
            damage: {
              max: {
                max: 999,
                message: 'The value should be equal or less than 999',
                value: 1000
              }
            }
          }
        },
        $1: {
          $root: {
            type: { message: 'The value should be an object', value: 1 }
          }
        },
        $2: {
          $root: {
            damage: {
              min: {
                message: 'The value should be equal or greater than 0',
                min: 0,
                value: -5
              }
            }
          }
        },
        maxItems: {
          currentItems: 3,
          maxItems: 2,
          message: 'This field should contain at maximun 2 item(s)',
          value: [
            { damage: 1000, title: 'Storm Breaker' },
            1,
            { damage: -5, title: 'Mjolnir' }
          ]
        }
      }
    }
  }
}

Docs

Etc...

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

TODO

  • - Create docs for "Contribution"
  • - Create sample video for "Registering a new validator"

About

JSON Schema Validator that let you know whata hell is happening!

License:MIT License


Languages

Language:JavaScript 100.0%