| 1 | import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types" |
| 2 | import type {KeywordCxt} from "../../compile/validate" |
| 3 | import {_, str, operators} from "../../compile/codegen" |
| 4 | |
| 5 | const error: KeywordErrorDefinition = { |
| 6 | message({keyword, schemaCode}) { |
| 7 | const comp = keyword === "maxItems" ? "more" : "fewer" |
| 8 | return str`must NOT have ${comp} than ${schemaCode} items` |
| 9 | }, |
| 10 | params: ({schemaCode}) => _`{limit: ${schemaCode}}`, |
| 11 | } |
| 12 | |
| 13 | const def: CodeKeywordDefinition = { |
| 14 | keyword: ["maxItems", "minItems"], |
| 15 | type: "array", |
| 16 | schemaType: "number", |
| 17 | $data: true, |
| 18 | error, |
| 19 | code(cxt: KeywordCxt) { |
| 20 | const {keyword, data, schemaCode} = cxt |
| 21 | const op = keyword === "maxItems" ? operators.GT : operators.LT |
| 22 | cxt.fail$data(_`${data}.length ${op} ${schemaCode}`) |
| 23 | }, |
| 24 | } |
| 25 | |
| 26 | export default def |