source: imaps-frontend/node_modules/ajv-keywords/src/definitions/select.ts

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import type {KeywordDefinition, KeywordErrorDefinition, KeywordCxt, ErrorObject} from "ajv"
2import {_, str, nil, Name} from "ajv/dist/compile/codegen"
3import type {DefinitionOptions} from "./_types"
4import {metaSchemaRef} from "./_util"
5
6export type SelectError = ErrorObject<"select", {failingCase?: string; failingDefault?: true}>
7
8const error: KeywordErrorDefinition = {
9 message: ({params: {schemaProp}}) =>
10 schemaProp
11 ? str`should match case "${schemaProp}" schema`
12 : str`should match default case schema`,
13 params: ({params: {schemaProp}}) =>
14 schemaProp ? _`{failingCase: ${schemaProp}}` : _`{failingDefault: true}`,
15}
16
17export default function getDef(opts?: DefinitionOptions): KeywordDefinition[] {
18 const metaSchema = metaSchemaRef(opts)
19
20 return [
21 {
22 keyword: "select",
23 schemaType: ["string", "number", "boolean", "null"],
24 $data: true,
25 error,
26 dependencies: ["selectCases"],
27 code(cxt: KeywordCxt) {
28 const {gen, schemaCode, parentSchema} = cxt
29 cxt.block$data(nil, () => {
30 const valid = gen.let("valid", true)
31 const schValid = gen.name("_valid")
32 const value = gen.const("value", _`${schemaCode} === null ? "null" : ${schemaCode}`)
33 gen.if(false) // optimizer should remove it from generated code
34 for (const schemaProp in parentSchema.selectCases) {
35 cxt.setParams({schemaProp})
36 gen.elseIf(_`"" + ${value} == ${schemaProp}`) // intentional ==, to match numbers and booleans
37 const schCxt = cxt.subschema({keyword: "selectCases", schemaProp}, schValid)
38 cxt.mergeEvaluated(schCxt, Name)
39 gen.assign(valid, schValid)
40 }
41 gen.else()
42 if (parentSchema.selectDefault !== undefined) {
43 cxt.setParams({schemaProp: undefined})
44 const schCxt = cxt.subschema({keyword: "selectDefault"}, schValid)
45 cxt.mergeEvaluated(schCxt, Name)
46 gen.assign(valid, schValid)
47 }
48 gen.endIf()
49 cxt.pass(valid)
50 })
51 },
52 },
53 {
54 keyword: "selectCases",
55 dependencies: ["select"],
56 metaSchema: {
57 type: "object",
58 additionalProperties: metaSchema,
59 },
60 },
61 {
62 keyword: "selectDefault",
63 dependencies: ["select", "selectCases"],
64 metaSchema,
65 },
66 ]
67}
68
69module.exports = getDef
Note: See TracBrowser for help on using the repository browser.