-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
index.ts
226 lines (212 loc) · 6.73 KB
/
index.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import AjvDraft4 from 'ajv-draft-04';
import { DataValidateFunction } from 'ajv/dist/types';
import ajvType from 'ajv/dist/vocabularies/jtd/type';
import addFormats from 'ajv-formats';
import { formats } from './formats';
import { OpenAPIV3, Options, SerDes } from '../types';
import * as traverse from 'json-schema-traverse';
interface SerDesSchema extends Partial<SerDes> {
kind?: 'req' | 'res';
}
export function createRequestAjv(
openApiSpec: OpenAPIV3.Document,
options: Options = {},
): AjvDraft4 {
return createAjv(openApiSpec, options);
}
export function createResponseAjv(
openApiSpec: OpenAPIV3.Document,
options: Options = {},
): AjvDraft4 {
return createAjv(openApiSpec, options, false);
}
function createAjv(
openApiSpec: OpenAPIV3.Document,
options: Options = {},
request = true,
): AjvDraft4 {
const { ajvFormats, ...ajvOptions } = options;
const ajv = new AjvDraft4({
...ajvOptions,
formats: formats,
});
// Clean openApiSpec
traverse(openApiSpec, { allKeys: true }, <traverse.Callback>(schema => {
if ('x-stoplight' in schema) {
delete schema['x-stoplight']
}
}))
// Formats will overwrite existing validation,
// so set in order of least->most important.
if (options.serDesMap) {
for (const serDesFormat of Object.keys(options.serDesMap)) {
ajv.addFormat(serDesFormat, true);
}
}
for (const [formatName, formatValidation] of Object.entries(formats)) {
ajv.addFormat(formatName, formatValidation);
}
if (ajvFormats) {
addFormats(ajv, ajvFormats);
}
for (let [formatName, formatDefinition] of Object.entries(options.formats)) {
ajv.addFormat(formatName, formatDefinition);
}
ajv.removeKeyword('propertyNames');
ajv.removeKeyword('contains');
ajv.removeKeyword('const');
if (options.serDesMap) {
// Alias for `type` that can execute AFTER x-eov-res-serdes
// There is a `type` keyword which this is positioned "next to",
// as well as high-level type assertion that runs before any keywords.
ajv.addKeyword({
...ajvType,
keyword: 'x-eov-type',
before: 'type',
});
}
if (request) {
if (options.serDesMap) {
ajv.addKeyword({
keyword: 'x-eov-req-serdes',
modifying: true,
errors: true,
// Deserialization occurs AFTER all string validations
post: true,
compile: (sch: SerDesSchema, p, it) => {
const validate: DataValidateFunction = (data, ctx) => {
if (typeof data !== 'string') {
// Either null (possibly allowed, defer to nullable validation)
// or already failed string validation (no need to throw additional internal errors).
return true;
}
try {
ctx.parentData[ctx.parentDataProperty] = sch.deserialize(data);
} catch (e) {
validate.errors = [
{
keyword: 'serdes',
instancePath: ctx.instancePath,
schemaPath: it.schemaPath.str,
message: e.message || `format is invalid`,
params: { 'x-eov-req-serdes': ctx.parentDataProperty },
},
];
return false;
}
return true;
};
return validate;
},
});
}
ajv.removeKeyword('readOnly');
ajv.addKeyword({
keyword: 'readOnly',
errors: true,
compile: (sch, p, it) => {
if (sch) {
const validate: DataValidateFunction = (data, ctx) => {
if (options.removeAdditional == true || options.removeAdditional == "all" || options.removeAdditional == "failing") {
// Remove readonly properties in request
delete ctx.parentData[ctx.parentDataProperty];
return true;
}
else {
const isValid = data == null;
if (!isValid) {
validate.errors = [
{
keyword: 'readOnly',
instancePath: ctx.instancePath,
schemaPath: it.schemaPath.str,
message: `is read-only`,
params: { writeOnly: ctx.parentDataProperty },
},
];
}
return false;
}
};
return validate;
}
return () => true;
},
});
} else {
// response
if (options.serDesMap) {
ajv.addKeyword({
keyword: 'x-eov-res-serdes',
modifying: true,
errors: true,
// Serialization occurs BEFORE type validations
before: 'x-eov-type',
compile: (sch: SerDesSchema, p, it) => {
const validate: DataValidateFunction = (data, ctx) => {
if (typeof data === 'string') return true;
try {
ctx.parentData[ctx.parentDataProperty] = sch.serialize(data);
} catch (e) {
validate.errors = [
{
keyword: 'serdes',
instancePath: ctx.instancePath,
schemaPath: it.schemaPath.str,
message: `format is invalid`,
params: { 'x-eov-res-serdes': ctx.parentDataProperty },
},
];
return false;
}
return true;
};
return validate;
},
});
}
ajv.removeKeyword('writeOnly');
ajv.addKeyword({
keyword: 'writeOnly',
schemaType: 'boolean',
errors: true,
compile: (sch, p, it) => {
if (sch) {
const validate: DataValidateFunction = (data, ctx) => {
if (options.removeAdditional == true || options.removeAdditional == "all" || options.removeAdditional == "failing") {
// Remove readonly properties in request
delete ctx.parentData[ctx.parentDataProperty];
return true;
}
else {
const isValid = data == null;
if (!isValid) {
validate.errors = [
{
keyword: 'writeOnly',
instancePath: ctx.instancePath,
schemaPath: it.schemaPath.str,
message: `is write-only`,
params: {writeOnly: ctx.parentDataProperty},
},
];
}
return false;
}
};
return validate;
}
return () => true;
},
});
}
if (openApiSpec.components?.schemas) {
Object.entries(openApiSpec.components.schemas).forEach(([id, schema]) => {
ajv.addSchema(
openApiSpec.components.schemas[id],
`#/components/schemas/${id}`,
);
});
}
return ajv;
}