0.6.0 • Published 4 years ago
@beelab/json-validator v0.6.0
JSON Validator
Yep, another JSON validator :D
Usage
Installation
$ npm install --save @beelab/json-validator
Usage
const Validator = require('@beelab/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 and type validator (see below)
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,
"_parseInt": true, # parseInt before validation
"_parseFloat": true # parseFloat before validation
},
"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"]
}
}
Add type validator
You can add custom type validator
const Validator = require('@beelab/json-validator');
const validator = new Validator();
validator.addTypeValidator('equalOne', function(value, schemaNode) {
if (value === 1) {
return value;
}
throw new Error('Invalid value');
});
validator.addTypeValidator('lessFive', function(value, schemaNode) {
if (this.validateNumber(value, schemaNode)) {
return value;
}
throw new Error('Invalid value');
});
const schema = {
expectedOne: {
_type: 'equalOne'
},
expectedTwo: {
_type: 'lessFive'
},
};
validator.validate({ expectedOne: 1, expectedTwo: 2 }, schema); // return { expectedOne: 1, expectedTwo: 2 }
validator.validate({ expectedOne: 2, expectedTwo: '2' }, schema); // throw Error
validator.hasTypeValidator('equalOne'); // Check if type validator exists
validator.removeSchema('equalOne'); // Remove type validator
validator.clear(); // Remove all schemas and type validator
Test
$ npm test