1 | import type {
|
2 | CodeKeywordDefinition,
|
3 | KeywordErrorDefinition,
|
4 | ErrorObject,
|
5 | AnySchema,
|
6 | } from "../../types"
|
7 | import type {KeywordCxt} from "../../compile/validate"
|
8 | import {_, str, Name} from "../../compile/codegen"
|
9 | import {alwaysValidSchema, checkStrictMode, Type} from "../../compile/util"
|
10 |
|
11 | export type ContainsError = ErrorObject<
|
12 | "contains",
|
13 | {minContains: number; maxContains?: number},
|
14 | AnySchema
|
15 | >
|
16 |
|
17 | const 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 |
|
26 | const 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 if (min === 0) {
|
67 | gen.let(valid, true)
|
68 | if (max !== undefined) gen.if(_`${data}.length > 0`, validateItemsWithCount)
|
69 | } else {
|
70 | gen.let(valid, false)
|
71 | validateItemsWithCount()
|
72 | }
|
73 | cxt.result(valid, () => cxt.reset())
|
74 |
|
75 | function validateItemsWithCount(): void {
|
76 | const schValid = gen.name("_valid")
|
77 | const count = gen.let("count", 0)
|
78 | validateItems(schValid, () => gen.if(schValid, () => checkLimits(count)))
|
79 | }
|
80 |
|
81 | function validateItems(_valid: Name, block: () => void): void {
|
82 | gen.forRange("i", 0, len, (i) => {
|
83 | cxt.subschema(
|
84 | {
|
85 | keyword: "contains",
|
86 | dataProp: i,
|
87 | dataPropType: Type.Num,
|
88 | compositeRule: true,
|
89 | },
|
90 | _valid
|
91 | )
|
92 | block()
|
93 | })
|
94 | }
|
95 |
|
96 | function checkLimits(count: Name): void {
|
97 | gen.code(_`${count}++`)
|
98 | if (max === undefined) {
|
99 | gen.if(_`${count} >= ${min}`, () => gen.assign(valid, true).break())
|
100 | } else {
|
101 | gen.if(_`${count} > ${max}`, () => gen.assign(valid, false).break())
|
102 | if (min === 1) gen.assign(valid, true)
|
103 | else gen.if(_`${count} >= ${min}`, () => gen.assign(valid, true))
|
104 | }
|
105 | }
|
106 | },
|
107 | }
|
108 |
|
109 | export default def
|