$ npm i jsonschema --save
$ node ./v.js
$ npm i jsonschema --save
$ node ./v.js
| { | |
| "$schema": "http://json-schema.org/draft-04/schema#", | |
| "title": "InventoryItem", | |
| "type": "object", | |
| "properties": { | |
| "name": { | |
| "type": "string" | |
| }, | |
| "price": { | |
| "type": "number", | |
| "minimum": 0 | |
| }, | |
| "sku": { | |
| "description": "Stock Keeping Unit", | |
| "type": "integer" | |
| } | |
| }, | |
| "required": ["name", "price"] | |
| } |
| var Validator = require('jsonschema').Validator; | |
| var v = new Validator(); | |
| var schema = require('./schema-example.json'); | |
| var object; | |
| object = {name: "eggs", price: 21.47}; | |
| console.log(v.validate(object, schema)); | |
| console.log("========================================================================"); | |
| object = 4; | |
| console.log(v.validate(object, schema)); |
Hey man how can i check that data is validated correctly or data is in correct format whether it returns true or false ???
const res = v.validate(object, schema);
console.log(res.errors.length > 0);
Hey man how can i check that data is validated correctly or data is in correct format whether it returns true or false ???