forked from ajv-validator/ajv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandalone.spec.ts
308 lines (276 loc) · 9.68 KB
/
standalone.spec.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import type Ajv from "../dist/core"
import type {AnyValidateFunction} from "../dist/core"
import _Ajv from "./ajv"
import standaloneCode from "../dist/standalone"
import ajvFormats from "ajv-formats"
import requireFromString = require("require-from-string")
import assert = require("assert")
describe("standalone code generation", () => {
describe("multiple exports", () => {
let ajv: Ajv
const numSchema = {
$id: "https://example.com/number.json",
type: "number",
minimum: 0,
}
const strSchema = {
$id: "https://example.com/string.json",
type: "string",
minLength: 2,
}
describe("without schema keys", () => {
beforeEach(() => {
ajv = new _Ajv({code: {source: true}})
ajv.addSchema(numSchema)
ajv.addSchema(strSchema)
})
it("should generate module code with named exports", () => {
const moduleCode = standaloneCode(ajv, {
validateNumber: "https://example.com/number.json",
validateString: "https://example.com/string.json",
})
const m = requireFromString(moduleCode)
assert.strictEqual(Object.keys(m).length, 2)
testExports(m)
})
it("should generate module code with all exports", () => {
const moduleCode = standaloneCode(ajv)
const m = requireFromString(moduleCode)
assert.strictEqual(Object.keys(m).length, 2)
testExports({
validateNumber: m["https://example.com/number.json"],
validateString: m["https://example.com/string.json"],
})
})
})
describe("with schema keys", () => {
beforeEach(() => {
ajv = new _Ajv({code: {source: true}})
ajv.addSchema(numSchema, "validateNumber")
ajv.addSchema(strSchema, "validateString")
})
it("should generate module code with named exports", () => {
const moduleCode = standaloneCode(ajv, {
validateNumber: "validateNumber",
validateString: "validateString",
})
const m = requireFromString(moduleCode)
assert.strictEqual(Object.keys(m).length, 2)
testExports(m)
})
it("should generate module code with all exports", () => {
const moduleCode = standaloneCode(ajv)
const m = requireFromString(moduleCode)
assert.strictEqual(Object.keys(m).length, 2)
testExports(m)
})
})
function testExports(m: {[n: string]: AnyValidateFunction<unknown>}) {
assert.strictEqual(m.validateNumber(1), true)
assert.strictEqual(m.validateNumber(0), true)
assert.strictEqual(m.validateNumber(-1), false)
assert.strictEqual(m.validateNumber("1"), false)
assert.strictEqual(m.validateString("123"), true)
assert.strictEqual(m.validateString("12"), true)
assert.strictEqual(m.validateString("1"), false)
assert.strictEqual(m.validateString(12), false)
}
})
describe("issue #1361", () => {
describe("two refs to the same schema", () => {
const userSchema = {
$id: "user.json",
type: "object",
properties: {
name: {type: "string"},
},
required: ["name"],
}
const infoSchema = {
$id: "info.json",
type: "object",
properties: {
author: {$ref: "user.json"},
contributors: {
type: "array",
items: {$ref: "user.json"},
},
},
required: ["author", "contributors"],
}
describe("all exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})
const moduleCode = standaloneCode(ajv)
assertNoDuplicateFunctions(moduleCode)
const {"user.json": user, "info.json": info} = requireFromString(moduleCode)
testExports({user, info})
})
})
describe("named exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})
const moduleCode = standaloneCode(ajv, {user: "user.json", info: "info.json"})
assertNoDuplicateFunctions(moduleCode)
testExports(requireFromString(moduleCode))
})
})
})
describe("mutually recursive schemas", () => {
const userSchema = {
$id: "user.json",
type: "object",
properties: {
name: {type: "string"},
infos: {
type: "array",
items: {$ref: "info.json"},
},
},
required: ["name"],
}
const infoSchema = {
$id: "info.json",
type: "object",
properties: {
author: {$ref: "user.json"},
contributors: {
type: "array",
items: {$ref: "user.json"},
},
},
required: ["author", "contributors"],
}
describe("all exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})
const moduleCode = standaloneCode(ajv)
assertNoDuplicateFunctions(moduleCode)
const {"user.json": user, "info.json": info} = requireFromString(moduleCode)
testExports({user, info})
})
})
describe("named exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})
const moduleCode = standaloneCode(ajv, {user: "user.json", info: "info.json"})
assertNoDuplicateFunctions(moduleCode)
testExports(requireFromString(moduleCode))
})
})
})
function assertNoDuplicateFunctions(code: string): void {
const funcs = code.match(/function\s+([a-z0-9_$]+)/gi)
assert(Array.isArray(funcs))
assert(funcs.length > 0)
assert.strictEqual(funcs.length, new Set(funcs).size, "should have no duplicates")
}
function testExports(validate: {[n: string]: AnyValidateFunction<unknown>}): void {
assert.strictEqual(validate.user({}), false)
assert.strictEqual(validate.user({name: "usr1"}), true)
assert.strictEqual(validate.info({}), false)
assert.strictEqual(
validate.info({
author: {name: "usr1"},
contributors: [{name: "usr2"}],
}),
true
)
}
})
it("should generate module code with a single export (ESM compatible)", () => {
const ajv = new _Ajv({code: {source: true}})
const v = ajv.compile({
type: "number",
minimum: 0,
})
const moduleCode = standaloneCode(ajv, v)
const m = requireFromString(moduleCode)
testExport(m)
testExport(m.default)
function testExport(validate: AnyValidateFunction<unknown>) {
assert.strictEqual(validate(1), true)
assert.strictEqual(validate(0), true)
assert.strictEqual(validate(-1), false)
assert.strictEqual(validate("1"), false)
}
})
describe("standalone code with ajv-formats", () => {
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {
User: {
type: "object",
properties: {
email: {
type: "string",
format: "email",
},
},
required: ["email"],
additionalProperties: false,
},
},
}
it("should support formats with standalone code", () => {
const ajv = new _Ajv({code: {source: true}})
ajvFormats(ajv)
ajv.addSchema(schema)
const moduleCode = standaloneCode(ajv, {validateUser: "#/definitions/User"})
const {validateUser} = requireFromString(moduleCode)
assert(typeof validateUser == "function")
assert.strictEqual(validateUser({}), false)
assert.strictEqual(validateUser({email: "foo"}), false)
assert.strictEqual(validateUser({email: "[email protected]"}), true)
})
})
describe("standalone code with RegExp format", () => {
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {
User: {
type: "object",
properties: {
username: {
type: "string",
format: "username",
},
},
required: ["username"],
additionalProperties: false,
},
},
}
it("should support RegExp format with standalone code", () => {
const ajv = new _Ajv({code: {source: true}})
ajv.addFormat("username", /[a-z][a-z0-9_]*/i)
ajv.addSchema(schema)
const moduleCode = standaloneCode(ajv, {validateUser: "#/definitions/User"})
const {validateUser} = requireFromString(moduleCode)
assert(typeof validateUser == "function")
assert.strictEqual(validateUser({}), false)
assert.strictEqual(validateUser({username: "foo_bar"}), true)
assert.strictEqual(validateUser({email: "foo bar"}), false)
})
})
})