-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-schema-draft4.js
167 lines (139 loc) · 4.3 KB
/
json-schema-draft4.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const Model = require('./Model');
const Enum = require('./Enum');
const DEFAULT_TO_REF = function (Type) {
if (!Type.typeName) {
throw new Error('Cannot build ref to type that does not have "typeName"');
}
return Type.typeName;
};
const DEFAULT_IS_IGNORED_PROPERTY = function (name, property) {
return false;
};
const SPECIAL_TYPES = {
'any': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
delete jsonSchemaProperty.type;
}
},
'object': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
jsonSchemaProperty.type = 'object';
}
},
'boolean': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
jsonSchemaProperty.type = 'boolean';
}
},
'date': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
jsonSchemaProperty.type = 'string';
jsonSchemaProperty.format = 'date-time';
}
},
'integer': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
jsonSchemaProperty.type = 'integer';
}
},
'number': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
jsonSchemaProperty.type = 'number';
jsonSchemaProperty.format = 'double';
}
},
'string': {
configureJsonSchemaProperty: function (jsonSchemaProperty) {
jsonSchemaProperty.type = 'string';
}
}
};
function _configure (jsonSchemaProperty, Type, options) {
const typeName = Type.typeName;
let specialType;
if (typeName && ((specialType = SPECIAL_TYPES[typeName]) !== undefined)) {
specialType.configureJsonSchemaProperty(jsonSchemaProperty);
} else {
jsonSchemaProperty.$ref = options.toRef(Type);
}
}
const IGNORED_PROPERTIES = {
constructor: 1,
$super: 1
};
exports.configureSchema = function (schema, Type, options) {
options = options || {};
options.toRef = options.toRef || DEFAULT_TO_REF;
_configure(schema, Type, options);
};
exports.fromModel = function (Type, options) {
options = options || {};
options.toRef = options.toRef || DEFAULT_TO_REF;
options.isIgnoredProperty = options.isIgnoredProperty || DEFAULT_IS_IGNORED_PROPERTY;
const schema = {};
if (Type.typeName) {
schema.id = Type.typeName;
}
['title', 'description', 'pattern'].forEach(function (attr) {
const value = Type[attr];
if (value !== undefined) {
schema[attr] = value;
}
});
const useAllOf = (options.useAllOf !== false);
if (useAllOf) {
// "allOf" is used to express composition.
// However, swagger-ui currently doesn't handle composition very well
// so we provide an option for disabling its usage.
// When "allOf" is disabled, the inherited properties are automatically
// put into the derived type definitions
const SuperType = Type.$super;
if (SuperType && (SuperType !== Model) && !SuperType.isCompatibleWith(Enum)) {
schema.allOf = [
{
$ref: options.toRef(SuperType)
}
];
}
}
if (Type.isCompatibleWith(Enum)) {
schema.type = 'string';
schema.enum = Type.names;
} else if (Type.hasProperties()) {
schema.type = 'object';
const properties = schema.properties = {};
Type.forEachProperty({
inherited: !useAllOf
}, function (declaredProperty) {
const key = declaredProperty.getKey();
if (!IGNORED_PROPERTIES[key] && !options.isIgnoredProperty(key, declaredProperty)) {
const jsonSchemaProperty = properties[key] = {};
['title', 'description'].forEach(function (attr) {
const value = declaredProperty[attr];
if (value !== undefined) {
jsonSchemaProperty[attr] = value;
}
});
if (options.handleProperty) {
options.handleProperty(key, declaredProperty, jsonSchemaProperty);
}
const PropertyType = declaredProperty.type;
if (declaredProperty.type.typeName === 'array') {
jsonSchemaProperty.type = 'array';
const items = declaredProperty.items;
jsonSchemaProperty.items = {};
if (items) {
_configure(jsonSchemaProperty.items, items.type, options);
}
} else {
_configure(jsonSchemaProperty, PropertyType, options);
}
}
});
} else if (Type.jsonSchemaType) {
schema.type = Type.jsonSchemaType;
} else {
return null;
}
return schema;
};