Debian Codegraph @

lib/vocabularies/validation/limitLength.ts

raw symbols SCIP
1 import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {_, str, operators} from "../../compile/codegen"
4 import {useFunc} from "../../compile/util"
5 import ucs2length from "../../runtime/ucs2length"
6
7 const error: KeywordErrorDefinition = {
8 message({keyword, schemaCode}) {
9 const comp = keyword === "maxLength" ? "more" : "fewer"
10 return str`must NOT have ${comp} than ${schemaCode} characters`
11 },
12 params: ({schemaCode}) => _`{limit: ${schemaCode}}`,
13 }
14
15 const def: CodeKeywordDefinition = {
16 keyword: ["maxLength", "minLength"],
17 type: "string",
18 schemaType: "number",
19 $data: true,
20 error,
21 code(cxt: KeywordCxt) {
22 const {keyword, data, schemaCode, it} = cxt
23 const op = keyword === "maxLength" ? operators.GT : operators.LT
24 const len =
25 it.opts.unicode === false ? _`${data}.length` : _`${useFunc(cxt.gen, ucs2length)}(${data})`
26 cxt.fail$data(_`${len} ${op} ${schemaCode}`)
27 },
28 }
29
30 export default def