Spaces:
Sleeping
Sleeping
File size: 2,408 Bytes
1df763a |
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 |
import type {KeywordDefinition, KeywordErrorDefinition, KeywordCxt, ErrorObject} from "ajv"
import {_, str, nil, Name} from "ajv/dist/compile/codegen"
import type {DefinitionOptions} from "./_types"
import {metaSchemaRef} from "./_util"
export type SelectError = ErrorObject<"select", {failingCase?: string; failingDefault?: true}>
const error: KeywordErrorDefinition = {
message: ({params: {schemaProp}}) =>
schemaProp
? str`should match case "${schemaProp}" schema`
: str`should match default case schema`,
params: ({params: {schemaProp}}) =>
schemaProp ? _`{failingCase: ${schemaProp}}` : _`{failingDefault: true}`,
}
export default function getDef(opts?: DefinitionOptions): KeywordDefinition[] {
const metaSchema = metaSchemaRef(opts)
return [
{
keyword: "select",
schemaType: ["string", "number", "boolean", "null"],
$data: true,
error,
dependencies: ["selectCases"],
code(cxt: KeywordCxt) {
const {gen, schemaCode, parentSchema} = cxt
cxt.block$data(nil, () => {
const valid = gen.let("valid", true)
const schValid = gen.name("_valid")
const value = gen.const("value", _`${schemaCode} === null ? "null" : ${schemaCode}`)
gen.if(false) // optimizer should remove it from generated code
for (const schemaProp in parentSchema.selectCases) {
cxt.setParams({schemaProp})
gen.elseIf(_`"" + ${value} == ${schemaProp}`) // intentional ==, to match numbers and booleans
const schCxt = cxt.subschema({keyword: "selectCases", schemaProp}, schValid)
cxt.mergeEvaluated(schCxt, Name)
gen.assign(valid, schValid)
}
gen.else()
if (parentSchema.selectDefault !== undefined) {
cxt.setParams({schemaProp: undefined})
const schCxt = cxt.subschema({keyword: "selectDefault"}, schValid)
cxt.mergeEvaluated(schCxt, Name)
gen.assign(valid, schValid)
}
gen.endIf()
cxt.pass(valid)
})
},
},
{
keyword: "selectCases",
dependencies: ["select"],
metaSchema: {
type: "object",
additionalProperties: metaSchema,
},
},
{
keyword: "selectDefault",
dependencies: ["select", "selectCases"],
metaSchema,
},
]
}
module.exports = getDef
|