3.1.6 • Published 6 years ago

mt-json-validator v3.1.6

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

Tiny JSON validator

A Node.js lib that has less lines than this readme™.

It supports a tiny subset of the json schema specs. The main goal of this project is to provide a nice and easy to follow implementation of a json validator. You may fork it and extend it as you please.

Release versioning follows SemVer.

Installation

$ npm install mt-json-validator

It is supported Node.js and bowser

Example

'use strict';

let validator = require('mt-json-validator');

let book_schema = {
  type: 'object',
  required: true,
  properties: {
    title: {
      type: 'string',
      required: true
    },
    author: {
      type: 'object',
      required: true,
      properties: {
        name: {
          type: 'string',
          required: true
        },
        age: {
          type: 'integer',
          required: true
        },
        city: {
          type: 'string'
        }
      }
    },
    related_titles: {
      type: 'array',
      required: true,
      items: {
        type: 'string'
      }
    }
  }
};

let data = {
  title: 'A Game of Thrones',
  author: {
    name: 'George R. R. Marti'
  },
  related_titles: [1, 2, 'A Song of Ice and Fire'],
  extra: 'this will get removed'
};

let res = validator(book_schema, data);

res.isValid;
// false

res.errors;
// {author.age: "is required", related_titles.0: 'type must be string', related_titles.1: 'type must be string'}

res.data;
// {title: 'A Game of Thrones', author: {name: 'George R. R. Marti'}, related_titles: ['A Song of Ice and Fire']}

Running tests

$ npm run test

Changelog

3.1.2: object create 3.1.1: add isArray fn 3.1.0: update version 3.0.2: fixed bug 3.0.1: support browser

2.0.1: add number and boolen types

License

MIT

3.1.6

6 years ago

3.1.5

6 years ago

3.1.4

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago