source: trip-planner-front/node_modules/ajv/lib/vocabularies/validation/limitNumber.ts@ eed0bf8

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.3 KB
Line 
1import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types"
2import type {KeywordCxt} from "../../compile/validate"
3import {_, str, operators, Code} from "../../compile/codegen"
4
5const ops = operators
6
7type Kwd = "maximum" | "minimum" | "exclusiveMaximum" | "exclusiveMinimum"
8
9type Comparison = "<=" | ">=" | "<" | ">"
10
11const KWDs: {[K in Kwd]: {okStr: Comparison; ok: Code; fail: Code}} = {
12 maximum: {okStr: "<=", ok: ops.LTE, fail: ops.GT},
13 minimum: {okStr: ">=", ok: ops.GTE, fail: ops.LT},
14 exclusiveMaximum: {okStr: "<", ok: ops.LT, fail: ops.GTE},
15 exclusiveMinimum: {okStr: ">", ok: ops.GT, fail: ops.LTE},
16}
17
18export type LimitNumberError = ErrorObject<
19 Kwd,
20 {limit: number; comparison: Comparison},
21 number | {$data: string}
22>
23
24const error: KeywordErrorDefinition = {
25 message: ({keyword, schemaCode}) => str`must be ${KWDs[keyword as Kwd].okStr} ${schemaCode}`,
26 params: ({keyword, schemaCode}) =>
27 _`{comparison: ${KWDs[keyword as Kwd].okStr}, limit: ${schemaCode}}`,
28}
29
30const def: CodeKeywordDefinition = {
31 keyword: Object.keys(KWDs),
32 type: "number",
33 schemaType: "number",
34 $data: true,
35 error,
36 code(cxt: KeywordCxt) {
37 const {keyword, data, schemaCode} = cxt
38 cxt.fail$data(_`${data} ${KWDs[keyword as Kwd].fail} ${schemaCode} || isNaN(${data})`)
39 },
40}
41
42export default def
Note: See TracBrowser for help on using the repository browser.