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

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


mschema

用于描述JSON数据结构的简洁模式语言

功能

  • 简单直观的语法
  • 模式是JSON
  • 可以使用 require()
  • 链接模式

另见

用npm安装

 npm install mschema

使用< a href =https://github.com/component/component>组件

component install mschema/mschema

示例

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 )

数据

要验证的数据

模式

根据

验证数据的模式

Usage

请参阅: / examples / test 文件夹中的其他用法

键入分配为字符串

 {
   "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
     }
 }]

链接模式

可以使用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模式的关系

JSON模式是根据 XML模式的规范设计的,该模式旨在表达文件类型定义

简单地说:JSON-Schema有很多功能,大多数开发人员不需要或想要。 JSON模式的复杂性使得难于使用并难以构建工具。

mschema和JSON-Schema

之间的主要区别

mschema有…

  • 语法简洁
  • 较少功能
  • JavaScript支持




相关问题推荐