source: trip-planner-front/node_modules/ajv/lib/vocabularies/applicator/contains.ts@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1import type {
2 CodeKeywordDefinition,
3 KeywordErrorDefinition,
4 ErrorObject,
5 AnySchema,
6} from "../../types"
7import type {KeywordCxt} from "../../compile/validate"
8import {_, str, Name} from "../../compile/codegen"
9import {alwaysValidSchema, checkStrictMode, Type} from "../../compile/util"
10
11export type ContainsError = ErrorObject<
12 "contains",
13 {minContains: number; maxContains?: number},
14 AnySchema
15>
16
17const error: KeywordErrorDefinition = {
18 message: ({params: {min, max}}) =>
19 max === undefined
20 ? str`must contain at least ${min} valid item(s)`
21 : str`must contain at least ${min} and no more than ${max} valid item(s)`,
22 params: ({params: {min, max}}) =>
23 max === undefined ? _`{minContains: ${min}}` : _`{minContains: ${min}, maxContains: ${max}}`,
24}
25
26const def: CodeKeywordDefinition = {
27 keyword: "contains",
28 type: "array",
29 schemaType: ["object", "boolean"],
30 before: "uniqueItems",
31 trackErrors: true,
32 error,
33 code(cxt: KeywordCxt) {
34 const {gen, schema, parentSchema, data, it} = cxt
35 let min: number
36 let max: number | undefined
37 const {minContains, maxContains} = parentSchema
38 if (it.opts.next) {
39 min = minContains === undefined ? 1 : minContains
40 max = maxContains
41 } else {
42 min = 1
43 }
44 const len = gen.const("len", _`${data}.length`)
45 cxt.setParams({min, max})
46 if (max === undefined && min === 0) {
47 checkStrictMode(it, `"minContains" == 0 without "maxContains": "contains" keyword ignored`)
48 return
49 }
50 if (max !== undefined && min > max) {
51 checkStrictMode(it, `"minContains" > "maxContains" is always invalid`)
52 cxt.fail()
53 return
54 }
55 if (alwaysValidSchema(it, schema)) {
56 let cond = _`${len} >= ${min}`
57 if (max !== undefined) cond = _`${cond} && ${len} <= ${max}`
58 cxt.pass(cond)
59 return
60 }
61
62 it.items = true
63 const valid = gen.name("valid")
64 if (max === undefined && min === 1) {
65 validateItems(valid, () => gen.if(valid, () => gen.break()))
66 } else {
67 gen.let(valid, false)
68 const schValid = gen.name("_valid")
69 const count = gen.let("count", 0)
70 validateItems(schValid, () => gen.if(schValid, () => checkLimits(count)))
71 }
72 cxt.result(valid, () => cxt.reset())
73
74 function validateItems(_valid: Name, block: () => void): void {
75 gen.forRange("i", 0, len, (i) => {
76 cxt.subschema(
77 {
78 keyword: "contains",
79 dataProp: i,
80 dataPropType: Type.Num,
81 compositeRule: true,
82 },
83 _valid
84 )
85 block()
86 })
87 }
88
89 function checkLimits(count: Name): void {
90 gen.code(_`${count}++`)
91 if (max === undefined) {
92 gen.if(_`${count} >= ${min}`, () => gen.assign(valid, true).break())
93 } else {
94 gen.if(_`${count} > ${max}`, () => gen.assign(valid, false).break())
95 if (min === 1) gen.assign(valid, true)
96 else gen.if(_`${count} >= ${min}`, () => gen.assign(valid, true))
97 }
98 }
99 },
100}
101
102export default def
Note: See TracBrowser for help on using the repository browser.