Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
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
|
---|
Note:
See
TracBrowser
for help on using the repository browser.