Nuclear Powered Macros

    schemato

    0.2.0 • Public • Published

    schemato

    JSON/Object schema validator

    var Validator = require('schemato').Validator;
    
    var schema = {
    	"type": "object",
    	"properties": {
    		"fieldString": {
    			"type": "string",
    			"enum": [ "hello world", "forever young"]
    		},
    		"fieldNumber": {
    			"type": "number"
    		},
    		"fieldObject": {
    			"type": "object",
    			"properties": {
    				"objfield1": {
    					"type": "boolean"
    				},
    				"objfield2": {
    					"type": "number"
    				}
    			},
    			"required": [ "objfield1", "objfield2" ]
    		},
    		"fieldSchema": {
    			"type": "object",
    			"oneOf": [{
    				"$schema": "http://json-schema.org/draft-04/schema"
    			}]
    		}
    	}
    }
    
    var validator = new Validator();
    
    var data = {
    	fieldString: "goodbye world",
    	fieldNumber: true,
    	fieldObject: {
    		"objfield1": 100,
    		"objfield2": false
    	},
    	fieldSchema: {
    		"type": "object",
    		"properties": {
    			"f_boolean": {
    				"type": "boolean"
    			},
    			"f_string": {
    				"type": "string",
    				"enum": [ "value1", "value2", "value3" ]
    			},
    			"f_object": {
    				"type": "object",
    				"properties": {
    					"sub1": {
    						"type": "number"
    					},
    					"sub2": {
    						"type": "array"
    					}
    				}
    			}
    		}
    	}
    };
    
    var result = validator.validate(data, schema);
    
    console.log("RESULT: %s", JSON.stringify(result, null, 2));

    Keywords

    none

    Install

    npm i schemato

    DownloadsWeekly Downloads

    30

    Version

    0.2.0

    License

    MIT

    Unpacked Size

    5.77 kB

    Total Files

    6

    Last publish

    Collaborators

    • devebot