Debian Codegraph @

spec/standalone.spec.ts

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 import type Ajv from "../dist/core"
2 import type {AnyValidateFunction} from "../dist/core"
3 import _Ajv from "./ajv"
4 import standaloneCode from "../dist/standalone"
5 import ajvFormats from "ajv-formats"
6 import * as requireFromString from "require-from-string"
7 import {importFromStringSync} from "module-from-string"
8 import * as assert from "assert"
9
10 function testExportTypeEsm(moduleCode: string, singleExport: boolean) {
11 //Must have
12 assert.strictEqual(moduleCode.includes("export const"), true)
13 if (singleExport) {
14 assert.strictEqual(moduleCode.includes("export default"), true)
15 }
16 //Must not have
17 assert.strictEqual(moduleCode.includes("module.exports"), false)
18 }
19 function testExportTypeCjs(moduleCode: string, singleExport: boolean) {
20 //Must have
21 if (singleExport) {
22 assert.strictEqual(moduleCode.includes("module.exports"), true)
23 } else {
24 assert.strictEqual(moduleCode.includes("exports.") || moduleCode.includes("exports["), true)
25 }
26 //Must not have
27 assert.strictEqual(moduleCode.includes("export const"), false)
28 }
29
30 describe("standalone code generation", () => {
31 describe("multiple exports", () => {
32 let ajv: Ajv
33 const numSchema = {
34 $id: "https://example.com/number.json",
35 type: "number",
36 minimum: 0,
37 }
38 const strSchema = {
39 $id: "https://example.com/string.json",
40 type: "string",
41 minLength: 2,
42 }
43
44 describe("without schema keys", () => {
45 it("should generate module code with named export - CJS", () => {
46 ajv = new _Ajv({code: {source: true}})
47 ajv.addSchema(numSchema)
48 ajv.addSchema(strSchema)
49 const moduleCode = standaloneCode(ajv, {
50 validateNumber: "https://example.com/number.json",
51 validateString: "https://example.com/string.json",
52 })
53 testExportTypeCjs(moduleCode, false)
54 const m = requireFromString(moduleCode)
55 assert.strictEqual(Object.keys(m).length, 2)
56 testExports(m)
57 })
58
59 it("should generate module code with named export - ESM", () => {
60 ajv = new _Ajv({code: {source: true, esm: true}})
61 ajv.addSchema(numSchema)
62 ajv.addSchema(strSchema)
63 const moduleCode = standaloneCode(ajv, {
64 validateNumber: "https://example.com/number.json",
65 validateString: "https://example.com/string.json",
66 })
67 testExportTypeEsm(moduleCode, false)
68 const m = importFromStringSync(moduleCode)
69 assert.strictEqual(Object.keys(m).length, 2)
70 testExports(m)
71 })
72
73 it("should generate module code with all exports - CJS", () => {
74 ajv = new _Ajv({code: {source: true}})
75 ajv.addSchema(numSchema)
76 ajv.addSchema(strSchema)
77 const moduleCode = standaloneCode(ajv)
78 testExportTypeCjs(moduleCode, false)
79 const m = requireFromString(moduleCode)
80 assert.strictEqual(Object.keys(m).length, 2)
81 testExports({
82 validateNumber: m["https://example.com/number.json"],
83 validateString: m["https://example.com/string.json"],
84 })
85 })
86
87 it("should generate module code with all exports - ESM", () => {
88 ajv = new _Ajv({code: {source: true, esm: true}})
89 ajv.addSchema(numSchema)
90 ajv.addSchema(strSchema)
91
92 try {
93 standaloneCode(ajv)
94 } catch (err) {
95 if (err instanceof Error) {
96 const isMappingErr =
97 `CodeGen: invalid export name: ${numSchema.$id}, use explicit $id name mapping` ===
98 err.message ||
99 `CodeGen: invalid export name: ${strSchema.$id}, use explicit $id name mapping` ===
100 err.message
101 assert.strictEqual(isMappingErr, true)
102 } else {
103 throw err
104 }
105 }
106 })
107 })
108
109 describe("with schema keys", () => {
110 beforeEach(() => {
111 ajv = new _Ajv({code: {source: true}})
112 ajv.addSchema(numSchema, "validateNumber")
113 ajv.addSchema(strSchema, "validateString")
114 })
115
116 it("should generate module code with named exports", () => {
117 const moduleCode = standaloneCode(ajv, {
118 validateNumber: "validateNumber",
119 validateString: "validateString",
120 })
121 const m = requireFromString(moduleCode)
122 assert.strictEqual(Object.keys(m).length, 2)
123 testExports(m)
124 })
125
126 it("should generate module code with all exports", () => {
127 const moduleCode = standaloneCode(ajv)
128 const m = requireFromString(moduleCode)
129 assert.strictEqual(Object.keys(m).length, 2)
130 testExports(m)
131 })
132 })
133
134 function testExports(m: {[n: string]: AnyValidateFunction<unknown>}) {
135 assert.strictEqual(m.validateNumber(1), true)
136 assert.strictEqual(m.validateNumber(0), true)
137 assert.strictEqual(m.validateNumber(-1), false)
138 assert.strictEqual(m.validateNumber("1"), false)
139
140 assert.strictEqual(m.validateString("123"), true)
141 assert.strictEqual(m.validateString("12"), true)
142 assert.strictEqual(m.validateString("1"), false)
143 assert.strictEqual(m.validateString(12), false)
144 }
145 })
146
147 describe("issue #1361", () => {
148 describe("two refs to the same schema", () => {
149 const userSchema = {
150 $id: "user.json",
151 type: "object",
152 properties: {
153 name: {type: "string"},
154 },
155 required: ["name"],
156 }
157
158 const infoSchema = {
159 $id: "info.json",
160 type: "object",
161 properties: {
162 author: {$ref: "user.json"},
163 contributors: {
164 type: "array",
165 items: {$ref: "user.json"},
166 },
167 },
168 required: ["author", "contributors"],
169 }
170
171 describe("all exports", () => {
172 it("should not have duplicate functions", () => {
173 const ajv = new _Ajv({
174 allErrors: true,
175 code: {optimize: false, source: true},
176 inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
177 schemas: [userSchema, infoSchema],
178 })
179
180 const moduleCode = standaloneCode(ajv)
181 assertNoDuplicateFunctions(moduleCode)
182 const {"user.json": user, "info.json": info} = requireFromString(moduleCode)
183 testExports({user, info})
184 })
185 })
186
187 describe("named exports", () => {
188 it("should not have duplicate functions", () => {
189 const ajv = new _Ajv({
190 allErrors: true,
191 code: {optimize: false, source: true},
192 inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
193 schemas: [userSchema, infoSchema],
194 })
195
196 const moduleCode = standaloneCode(ajv, {user: "user.json", info: "info.json"})
197 assertNoDuplicateFunctions(moduleCode)
198 testExports(requireFromString(moduleCode))
199 })
200 })
201 })
202
203 describe("mutually recursive schemas", () => {
204 const userSchema = {
205 $id: "user.json",
206 type: "object",
207 properties: {
208 name: {type: "string"},
209 infos: {
210 type: "array",
211 items: {$ref: "info.json"},
212 },
213 },
214 required: ["name"],
215 }
216
217 const infoSchema = {
218 $id: "info.json",
219 type: "object",
220 properties: {
221 author: {$ref: "user.json"},
222 contributors: {
223 type: "array",
224 items: {$ref: "user.json"},
225 },
226 },
227 required: ["author", "contributors"],
228 }
229
230 describe("all exports", () => {
231 it("should not have duplicate functions", () => {
232 const ajv = new _Ajv({
233 allErrors: true,
234 code: {optimize: false, source: true},
235 inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
236 schemas: [userSchema, infoSchema],
237 })
238
239 const moduleCode = standaloneCode(ajv)
240 assertNoDuplicateFunctions(moduleCode)
241 const {"user.json": user, "info.json": info} = requireFromString(moduleCode)
242 testExports({user, info})
243 })
244 })
245
246 describe("named exports", () => {
247 it("should not have duplicate functions", () => {
248 const ajv = new _Ajv({
249 allErrors: true,
250 code: {optimize: false, source: true},
251 inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
252 schemas: [userSchema, infoSchema],
253 })
254
255 const moduleCode = standaloneCode(ajv, {user: "user.json", info: "info.json"})
256 assertNoDuplicateFunctions(moduleCode)
257 testExports(requireFromString(moduleCode))
258 })
259 })
260 })
261
262 function assertNoDuplicateFunctions(code: string): void {
263 const funcs = code.match(/function\s+([a-z0-9_$]+)/gi)
264 assert(Array.isArray(funcs))
265 assert(funcs.length > 0)
266 assert.strictEqual(funcs.length, new Set(funcs).size, "should have no duplicates")
267 }
268
269 function testExports(validate: {[n: string]: AnyValidateFunction<unknown>}): void {
270 assert.strictEqual(validate.user({}), false)
271 assert.strictEqual(validate.user({name: "usr1"}), true)
272
273 assert.strictEqual(validate.info({}), false)
274 assert.strictEqual(
275 validate.info({
276 author: {name: "usr1"},
277 contributors: [{name: "usr2"}],
278 }),
279 true
280 )
281 }
282 })
283
284 it("should generate module code with a single export - CJS", () => {
285 const ajv = new _Ajv({code: {source: true}})
286 const v = ajv.compile({
287 type: "number",
288 minimum: 0,
289 })
290 const moduleCode = standaloneCode(ajv, v)
291 testExportTypeCjs(moduleCode, true)
292 const m = requireFromString(moduleCode)
293 testExport(m)
294 testExport(m.default)
295
296 function testExport(validate: AnyValidateFunction<unknown>) {
297 assert.strictEqual(validate(1), true)
298 assert.strictEqual(validate(0), true)
299 assert.strictEqual(validate(-1), false)
300 assert.strictEqual(validate("1"), false)
301 }
302 })
303
304 it("should generate module code with a single export - ESM", () => {
305 const ajv = new _Ajv({code: {source: true, esm: true}})
306 const v = ajv.compile({
307 type: "number",
308 minimum: 0,
309 })
310 const moduleCode = standaloneCode(ajv, v)
311 testExportTypeEsm(moduleCode, true)
312 const m = importFromStringSync(moduleCode)
313 testExport(m.validate)
314 testExport(m.default)
315
316 function testExport(validate: AnyValidateFunction<unknown>) {
317 assert.strictEqual(validate(1), true)
318 assert.strictEqual(validate(0), true)
319 assert.strictEqual(validate(-1), false)
320 assert.strictEqual(validate("1"), false)
321 }
322 })
323
324 describe("standalone code with ajv-formats", () => {
325 const schema = {
326 $schema: "http://json-schema.org/draft-07/schema#",
327 definitions: {
328 User: {
329 type: "object",
330 properties: {
331 email: {
332 type: "string",
333 format: "email",
334 },
335 },
336 required: ["email"],
337 additionalProperties: false,
338 },
339 },
340 }
341
342 it("should support formats with standalone code", () => {
343 const ajv = new _Ajv({code: {source: true}})
344 ajvFormats(ajv)
345 ajv.addSchema(schema)
346 const moduleCode = standaloneCode(ajv, {validateUser: "#/definitions/User"})
347 const {validateUser} = requireFromString(moduleCode)
348
349 assert(typeof validateUser == "function")
350 assert.strictEqual(validateUser({}), false)
351 assert.strictEqual(validateUser({email: "foo"}), false)
352 assert.strictEqual(validateUser({email: "[email protected]"}), true)
353 })
354 })
355
356 describe("standalone code with RegExp format", () => {
357 const schema = {
358 $schema: "http://json-schema.org/draft-07/schema#",
359 definitions: {
360 User: {
361 type: "object",
362 properties: {
363 username: {
364 type: "string",
365 format: "username",
366 },
367 },
368 required: ["username"],
369 additionalProperties: false,
370 },
371 },
372 }
373
374 it("should support RegExp format with standalone code", () => {
375 const ajv = new _Ajv({code: {source: true}})
376 ajv.addFormat("username", /[a-z][a-z0-9_]*/i)
377 ajv.addSchema(schema)
378 const moduleCode = standaloneCode(ajv, {validateUser: "#/definitions/User"})
379 const {validateUser} = requireFromString(moduleCode)
380
381 assert(typeof validateUser == "function")
382 assert.strictEqual(validateUser({}), false)
383 assert.strictEqual(validateUser({username: "foo_bar"}), true)
384 assert.strictEqual(validateUser({email: "foo bar"}), false)
385 })
386 })
387 })