A concise schema language for describing the structure of JSON data
- simple intuitive syntax
- schemas are JSON
- schemas can be linked using
require()
- mschema-rpc(http://github.com/mschema/mschema-rpc)
npm install mschema
Install with component
component install mschema/mschema
see '/test' folder for alternate syntax and examples
var mschema = require('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 true
var result = mschema.validate(data, user);
console.log(result);
var data = {
"name": "M",
"password": "1234",
"email": "foo@bar.com"
}
// validates false with errors
var result = mschema.validate(data, user);
console.log(result);
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 true
var result = mschema.validate(data, blog);
console.log(result);
the data to be validated
the schema to validate the data against
see: /examples
and /test
folders for additional usage
{
"name": "string",
"age": "number",
"address": "object",
"isActive": "boolean"
}
{
"id": {
"type": "string",
"minLength": 5,
"maxLength": 10
}
}
{
"name": "string",
"password": "string",
"address": {
"street": "string",
"city": "string",
"country": "string"
}
}
{ "posts": ["string"] }
{ "posts": [{ "type": "string", "minLength": 5, "maxLength": 10 }] }
{ "posts": [{
"title": {
"type": "string",
"minLength": 3,
"maxLength": 15
},
"content": {
"type": "string",
"minLength": 3,
"maxLength": 15
}
}]
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.validate(data, user);
// validates to true
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.
mschema has...
- Brevity of syntax
- Less features
- JavaScript Support