-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonschema2jsdoc.js
137 lines (120 loc) · 3.29 KB
/
jsonschema2jsdoc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* eslint-disable no-console */
import json from 'json-pointer'
/**
*
* @param {Object} schema
* @param {{ignore: string[]}} options
*/
export default function generate(schema, options = {}) {
let jsdoc = ''
if (!schema || Object.keys(schema).length === 0) {
return jsdoc
}
jsdoc += '/**\n'
jsdoc += writeDescription(schema)
if (!json.has(schema, '/properties')) {
return jsdoc
}
jsdoc += processProperties(schema, '', options)
jsdoc += ' */\n'
return jsdoc
}
/**
* 生成
* @param {*} schema
* @param {*} nested
* @param {*} options
*/
function processProperties(schema, nested, options = {}) {
const props = json.get(schema, '/properties')
const required = json.has(schema, '/required')
? json.get(schema, '/required')
: []
let text = ''
for (let property in props) {
let optional = !required.includes(property)
if (Array.isArray(options.ignore) && options.ignore.includes(property)) {
continue
} else {
let prefix = nested ? `${nested}.` : ''
if (props[property].type === 'object' && props[property].properties) {
text += writeParam(
'Object',
prefix + property,
props[property].description,
optional
)
text += processProperties(props[property], prefix + property)
} else if (props[property].type === 'array' && props[property].items) {
let { items } = props[property]
// 如果是object 继续循环
if (items.type === 'object') {
text += writeParam(
'Object[]',
prefix + property,
props[property].description,
!required.includes(property)
)
text += processProperties(items, prefix + property + '[]')
} else {
/**
* 非object
* @todo 支持第三方类 upperFirst(property)
*/
text += writeParam(
getType(props[property].items) + '[]',
prefix + property,
props[property].description,
optional
)
}
} else {
let type = getType(props[property]) || upperFirst(property)
text += writeParam(
type,
prefix + property,
props[property].description,
optional
)
}
}
}
return text
}
function writeDescription(schema, suffix = 'object') {
let name = schema.id || schema.description
let text = schema.description || `Represents a ${name} ${suffix}`
text += `\n * @name ${upperFirst(name)}`
return ` * ${text}\n *\n`
}
/**
*
* @param {*} type
* @param {*} field
* @param {*} description
* @param {boolean} optional
*/
function writeParam(type = '', field, description = '', optional = true) {
const fieldTemplate = optional ? `[${field}]` : field
return ` * @property {${type}} ${fieldTemplate} - ${description} \n`
}
function getType(schema) {
if (schema.$ref) {
const ref = json.get(schema, schema.$ref.substr(1))
return getType(ref)
}
if (schema.enum) {
return 'enum'
}
if (Array.isArray(schema.type)) {
if (schema.type.includes('null')) {
return `?${schema.type[0]}`
} else {
return schema.type.join('|')
}
}
return schema.type
}
function upperFirst(str = '') {
return str.substr(0, 1).toUpperCase() + str.substr(1)
}