| 1 |
import getAjvAsyncInstances from "./ajv_async_instances" |
| 2 |
import * as jsonSchemaTest from "json-schema-test" |
| 3 |
import {afterError} from "./after_test" |
| 4 |
import type Ajv from ".." |
| 5 |
import _Ajv from "./ajv" |
| 6 |
import chai from "./chai" |
| 7 |
|
| 8 |
const instances = getAjvAsyncInstances({$data: true}) |
| 9 |
|
| 10 |
instances.forEach(addAsyncFormatsAndKeywords) |
| 11 |
|
| 12 |
jsonSchemaTest(instances, { |
| 13 |
description: |
| 14 |
"asynchronous schemas tests of " + instances.length + " ajv instances with different options", |
| 15 |
suites: {"async schemas": require("./_json/async")}, |
| 16 |
async: true, |
| 17 |
asyncValid: "data", |
| 18 |
assert: chai.assert, |
| 19 |
afterError, |
| 20 |
// afterEach: after.each, |
| 21 |
cwd: __dirname, |
| 22 |
hideFolder: "async/", |
| 23 |
timeout: 10000, |
| 24 |
}) |
| 25 |
|
| 26 |
function addAsyncFormatsAndKeywords(ajv: Ajv) { |
| 27 |
ajv.addFormat("date", /^\d\d\d\d-[0-1]\d-[0-3]\d$/) |
| 28 |
|
| 29 |
ajv.addFormat("english_word", { |
| 30 |
async: true, |
| 31 |
validate: checkWordOnServer, |
| 32 |
}) |
| 33 |
|
| 34 |
ajv.addKeyword({ |
| 35 |
keyword: "idExists", |
| 36 |
async: true, |
| 37 |
type: "number", |
| 38 |
validate: checkIdExists, |
| 39 |
errors: false, |
| 40 |
}) |
| 41 |
|
| 42 |
ajv.addKeyword({ |
| 43 |
keyword: "idExistsWithError", |
| 44 |
async: true, |
| 45 |
type: "number", |
| 46 |
validate: checkIdExistsWithError, |
| 47 |
errors: true, |
| 48 |
}) |
| 49 |
|
| 50 |
ajv.addKeyword({ |
| 51 |
keyword: "idExistsCompiled", |
| 52 |
async: true, |
| 53 |
type: "number", |
| 54 |
compile: compileCheckIdExists, |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
function checkWordOnServer(str: string): Promise<boolean> { |
| 59 |
return str === "tomorrow" |
| 60 |
? Promise.resolve(true) |
| 61 |
: str === "manana" |
| 62 |
? Promise.resolve(false) |
| 63 |
: Promise.reject(new Error("unknown word")) |
| 64 |
} |
| 65 |
|
| 66 |
function checkIdExists(schema: {table: string}, data: number): Promise<boolean> { |
| 67 |
switch (schema.table) { |
| 68 |
case "users": |
| 69 |
return check([1, 5, 8]) |
| 70 |
case "posts": |
| 71 |
return check([21, 25, 28]) |
| 72 |
default: |
| 73 |
throw new Error("no such table") |
| 74 |
} |
| 75 |
|
| 76 |
function check(IDs: number[]): Promise<boolean> { |
| 77 |
return Promise.resolve(IDs.includes(data)) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
function checkIdExistsWithError(schema: {table: string}, data: number): Promise<boolean> { |
| 82 |
const {table} = schema |
| 83 |
switch (table) { |
| 84 |
case "users": |
| 85 |
return check(table, [1, 5, 8]) |
| 86 |
case "posts": |
| 87 |
return check(table, [21, 25, 28]) |
| 88 |
default: |
| 89 |
throw new Error("no such table") |
| 90 |
} |
| 91 |
|
| 92 |
function check(_table: string, IDs: number[]): Promise<boolean> { |
| 93 |
if (IDs.includes(data)) return Promise.resolve(true) |
| 94 |
|
| 95 |
const error = { |
| 96 |
keyword: "idExistsWithError", |
| 97 |
message: "id not found in table " + _table, |
| 98 |
} |
| 99 |
return Promise.reject(new _Ajv.ValidationError([error])) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
function compileCheckIdExists(schema: {table: string}): (data: number) => Promise<boolean> { |
| 104 |
switch (schema.table) { |
| 105 |
case "users": |
| 106 |
return compileCheck([1, 5, 8]) |
| 107 |
case "posts": |
| 108 |
return compileCheck([21, 25, 28]) |
| 109 |
default: |
| 110 |
throw new Error("no such table") |
| 111 |
} |
| 112 |
|
| 113 |
function compileCheck(IDs: number[]): (data: number) => Promise<boolean> { |
| 114 |
return (data) => Promise.resolve(IDs.includes(data)) |
| 115 |
} |
| 116 |
} |