mschema
A concise schema language for describing the structure of JSON data
Features
- simple intuitive syntax
- schemas are JSON
- schemas can be linked using
require()
See Also
- mschema-rpc(http://github.com/mschema/mschema-rpc)
Install with npm
npm install mschema
component
Install withcomponent install mschema/mschema
Example
see '/test' folder for alternate syntax and examples
var mschema = ; var user = "name": "type": "string" "minLength": 5 "maxLength": 20 "password": "type": "string" "minLength": 8 "maxLength": 64 "email": "string" var data = "name": "Marak" "password": "atleasteight" "email": "foo@bar.com" // validates truevar result = mschema;console; var data = "name": "M" "password": "1234" "email": "foo@bar.com" // validates false with errorsvar result = mschema;console; var blog = "name": "string" "posts": "title": "type": "string" "maxLength": 15 "author": "string" "content": "string" ; var data = "name": "My blog" "posts": "title": "An example blog post" "author": "Marak" "content": "This is an example blog post" ; // validates truevar result = mschema;console;
API
mschema.validate(data, schema)
data
the data to be validated
schema
the schema to validate the data against
Usage
see: /examples
and /test
folders for additional usage
Type assignment as string
Type assignment as an object literal
Nesting Types
Typed arrays
Generic types
Typed with constraints
Array of objects
"posts": "title": "type": "string" "minLength": 3 "maxLength": 15 "content": "type": "string" "minLength": 3 "maxLength": 15
Linking Schemas
Schemas can be linked together using JS
var address = "street": "string" "city": "string" "zipcode": "string" var user = "name": "string" "age": "number" "address": address var data = "name": "Marak" "age": 42 "address": "street": "123 elm street" "city": "Canada" "zipcode": "12345-01" ; var validate = mschema; // validates to true
Relation to JSON-Schema
JSON-Schema was designed to the specifications of XML-Schema, which was designed to express Document Type Definitions.
Simply put: JSON-Schema has a lot of functionality that most developers don't need or want. The complexity of JSON-Schema makes it difficult to use and hard to build tools for.
Key differences between mschema and JSON-Schema
mschema has...
- Brevity of syntax
- Less features
- JavaScript Support