Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mschema

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mschema

A schema language for defining the structure of JSON data

  • 0.5.3
  • npm
  • Socket score

Version published
Weekly downloads
471
increased by4.67%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 17 Jun 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc