MCPcopy
hub / github.com/nuysoft/Mock / toJSONSchema

Function toJSONSchema

src/mock/schema/schema.js:12–45  ·  view source on GitHub ↗
(template, name, path /* Internal Use Only */ )

Source from the content-addressed store, hash-verified

10var Parser = require('../parser')
11
12function toJSONSchema(template, name, path /* Internal Use Only */ ) {
13 // type rule properties items
14 path = path || []
15 var result = {
16 name: typeof name === 'string' ? name.replace(Constant.RE_KEY, '$1') : name,
17 template: template,
18 type: Util.type(template), // 可能不准确,例如 { 'name|1': [{}, {} ...] }
19 rule: Parser.parse(name)
20 }
21 result.path = path.slice(0)
22 result.path.push(name === undefined ? 'ROOT' : result.name)
23
24 switch (result.type) {
25 case 'array':
26 result.items = []
27 Util.each(template, function(value, index) {
28 result.items.push(
29 toJSONSchema(value, index, result.path)
30 )
31 })
32 break
33 case 'object':
34 result.properties = []
35 Util.each(template, function(value, name) {
36 result.properties.push(
37 toJSONSchema(value, name, result.path)
38 )
39 })
40 break
41 }
42
43 return result
44
45}
46
47module.exports = toJSONSchema

Callers 1

validFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected