-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Mock.toJSONSchema()
mozhi.gyy edited this page Dec 16, 2015
·
3 revisions
- Mock.toJSONSchema( template )
把 Mock.js 风格的数据模板 template
转换成 JSON Schema。
必选。
表示数据模板,可以是对象或字符串。例如 { 'list|1-10':[{}] }
、'@EMAIL'
。
var template = {
'key|1-10': '★'
}
Mock.toJSONSchema(template)
// =>
{
"name": undefined,
"path": [
"ROOT"
],
"type": "object",
"template": {
"key|1-10": "★"
},
"rule": {},
"properties": [{
"name": "key",
"path": [
"ROOT",
"key"
],
"type": "string",
"template": "★",
"rule": {
"parameters": ["key|1-10", "key", null, "1-10", null],
"range": ["1-10", "1", "10"],
"min": 1,
"max": 10,
"count": 3,
"decimal": undefined,
"dmin": undefined,
"dmax": undefined,
"dcount": undefined
}
}]
}
var template = {
'list|1-10': [{}]
}
Mock.toJSONSchema(template)
// =>
{
"name": undefined,
"path": ["ROOT"],
"type": "object",
"template": {
"list|1-10": [{}]
},
"rule": {},
"properties": [{
"name": "list",
"path": ["ROOT", "list"],
"type": "array",
"template": [{}],
"rule": {
"parameters": ["list|1-10", "list", null, "1-10", null],
"range": ["1-10", "1", "10"],
"min": 1,
"max": 10,
"count": 6,
"decimal": undefined,
"dmin": undefined,
"dmax": undefined,
"dcount": undefined
},
"items": [{
"name": 0,
"path": ["data", "list", 0],
"type": "object",
"template": {},
"rule": {},
"properties": []
}]
}]
}
Mock.js 最初的灵感来自 Elijah Manor 的博文 Mocking Introduction,语法参考了 mennovanslooten/mockJSON,随机数据参考了 victorquinn/chancejs。