mschema

A schema language for defining the structure of JSON data

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
mschema
0.5.67 years ago9 years agoMinified + gzip package size for mschema in KB

Readme

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

Install with component

component install mschema/mschema

Example

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

{
  "name": "string",
  "age": "number",
  "address": "object",
  "isActive": "boolean"
}

Type assignment as an object literal

{ 
  "id": {
    "type": "string",
    "minLength": 5,
    "maxLength": 10
  }
}

Nesting Types

{ 
  "name": "string",
  "password": "string",
  "address": {
    "street": "string",
    "city": "string",
    "country": "string"
  }
}

Typed arrays

Generic types

{ "posts": ["string"] }

Typed with constraints

{ "posts": [{ "type": "string", "minLength": 5, "maxLength": 10 }] }

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.validate(data, user); 

// 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