schemato

JSON/Object schema validator

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
schemato
0.2.014 days ago5 years agoMinified + gzip package size for schemato in KB

Readme

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));