pxl-json-validator
v0.3.0
Published
JSON schema validator
Downloads
45
Readme
JSON Validator
Yep, another JSON validator :D
Usage
Installation
$ npm install --save pxl-json-validator
Usage
const Validator = require('pxl-json-validator');
const validator = new Validator();
validator.addSchema('string-schema', { foo: _type: 'string' });
validator.addSchema('number-schema', { foo: _type: 'number' });
validator.addSchema('boolean-schema', { foo: _type: 'boolean' });
validator.addSchema('array-schema', { foo: _type: 'array' });
validator.addSchema('object-schema', { foo: _type: 'object' });
validator.validate({ foo: 'bar' }, 'string-schema'); // return { foor: 'bar' }
validator.validate({ foo: 2 }, 'string-schema'); // throw error "Parameter has to be a string"
validator.hasSchema('string-schema'); // Check if schema exists
validator.removeSchema('string-schema'); // Remove schema
validator.clear(); // Remove all schemas
You can use pxl-json-validator
into navigator, but please use polyfill for ES6.
<script src="//cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill.min.js" charset="utf-8"></script>
<script src="node_modules/pxl-json-validator/index.js" charset="utf-8"></script>
Schema example
{
"stringParameter": {
"_type": "string",
"_required": true,
"_default": "foo",
"_optional": false,
"_minLength": 10,
"_maxLength": 30,
"_pattern": "[a-z]+\.$"
},
"numberParameter": {
"_type": "number",
"_required": true,
"_default": 1,
"_multipleOf": 2,
"_maximum": 100,
"_exclusiveMaximum": 110,
"_minimum": 5,
"_exclusiveMinimum": 1
},
"booleanParameter": {
"_type": "boolean",
"_required": true,
"_default": false,
"_optional": false
},
"arrayOfStringParameter": {
"_type": "array",
"_required": true,
"_optional": false,
"_items": {
"_type": "string",
"_default": "foo"
},
"_default": ["foo"]
},
"arrayOfArrayOfNumberParameter": {
"_type": "array",
"_required": true,
"_optional": false,
"_items": {
"_type": "array",
"_items": {
"_type": "number",
"_default": 1
}
},
"_default": ["foo"]
},
"objectParameter": {
"_type": "object",
"_required": true,
"_optional": false,
"_parameters": {
"parameter-1": {
"_type": "string",
"_required": true,
"_default": "foo"
},
"parameter-2": {
"_type": "number",
"_required": true,
"_default": 1
},
"parameter-3": {
"_type": "object",
"_required": true,
"_parameters": { ... }
},
"parameter-4": {
"_type": "array",
"_required": true,
"_items": { ... },
"_default": ["foo"]
}
},
"_default": ["foo"]
}
}
Test
$ npm test