https://github.com/nitely/tiny-json-validator
ES6 json validator
https://github.com/nitely/tiny-json-validator
javascript json-schema validator
Last synced: 4 months ago
JSON representation
ES6 json validator
- Host: GitHub
- URL: https://github.com/nitely/tiny-json-validator
- Owner: nitely
- License: other
- Archived: true
- Created: 2015-03-23T22:35:18.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-05-28T19:29:47.000Z (almost 5 years ago)
- Last Synced: 2024-11-15T22:45:59.624Z (6 months ago)
- Topics: javascript, json-schema, validator
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 6
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# Tiny JSON validator
[](https://travis-ci.org/nitely/tiny-json-validator)
[](https://www.npmjs.com/package/tiny-json-validator)A Node.js lib that has *less lines than this readme™*.
It supports a tiny subset of the [json schema](http://json-schema.org/) 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](http://semver.org/).
# Installation
```
$ npm install tiny-json-validator
```It is supported in all versions of [Node.js](https://nodejs.org) +4.2 without any flags.
# Example
```javascript
"use strict";let validator = require('tiny-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
// falseres.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
[changelog](https://github.com/nitely/tiny-json-validator/blob/master/CHANGES.md)
## License
MIT