1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.formatLimitDefinition = void 0;
|
---|
4 | const ajv_1 = require("ajv");
|
---|
5 | const codegen_1 = require("ajv/dist/compile/codegen");
|
---|
6 | const ops = codegen_1.operators;
|
---|
7 | const KWDs = {
|
---|
8 | formatMaximum: { okStr: "<=", ok: ops.LTE, fail: ops.GT },
|
---|
9 | formatMinimum: { okStr: ">=", ok: ops.GTE, fail: ops.LT },
|
---|
10 | formatExclusiveMaximum: { okStr: "<", ok: ops.LT, fail: ops.GTE },
|
---|
11 | formatExclusiveMinimum: { okStr: ">", ok: ops.GT, fail: ops.LTE },
|
---|
12 | };
|
---|
13 | const error = {
|
---|
14 | message: ({ keyword, schemaCode }) => codegen_1.str `should be ${KWDs[keyword].okStr} ${schemaCode}`,
|
---|
15 | params: ({ keyword, schemaCode }) => codegen_1._ `{comparison: ${KWDs[keyword].okStr}, limit: ${schemaCode}}`,
|
---|
16 | };
|
---|
17 | exports.formatLimitDefinition = {
|
---|
18 | keyword: Object.keys(KWDs),
|
---|
19 | type: "string",
|
---|
20 | schemaType: "string",
|
---|
21 | $data: true,
|
---|
22 | error,
|
---|
23 | code(cxt) {
|
---|
24 | const { gen, data, schemaCode, keyword, it } = cxt;
|
---|
25 | const { opts, self } = it;
|
---|
26 | if (!opts.validateFormats)
|
---|
27 | return;
|
---|
28 | const fCxt = new ajv_1.KeywordCxt(it, self.RULES.all.format.definition, "format");
|
---|
29 | if (fCxt.$data)
|
---|
30 | validate$DataFormat();
|
---|
31 | else
|
---|
32 | validateFormat();
|
---|
33 | function validate$DataFormat() {
|
---|
34 | const fmts = gen.scopeValue("formats", {
|
---|
35 | ref: self.formats,
|
---|
36 | code: opts.code.formats,
|
---|
37 | });
|
---|
38 | const fmt = gen.const("fmt", codegen_1._ `${fmts}[${fCxt.schemaCode}]`);
|
---|
39 | cxt.fail$data(codegen_1.or(codegen_1._ `typeof ${fmt} != "object"`, codegen_1._ `${fmt} instanceof RegExp`, codegen_1._ `typeof ${fmt}.compare != "function"`, compareCode(fmt)));
|
---|
40 | }
|
---|
41 | function validateFormat() {
|
---|
42 | const format = fCxt.schema;
|
---|
43 | const fmtDef = self.formats[format];
|
---|
44 | if (!fmtDef || fmtDef === true)
|
---|
45 | return;
|
---|
46 | if (typeof fmtDef != "object" ||
|
---|
47 | fmtDef instanceof RegExp ||
|
---|
48 | typeof fmtDef.compare != "function") {
|
---|
49 | throw new Error(`"${keyword}": format "${format}" does not define "compare" function`);
|
---|
50 | }
|
---|
51 | const fmt = gen.scopeValue("formats", {
|
---|
52 | key: format,
|
---|
53 | ref: fmtDef,
|
---|
54 | code: opts.code.formats ? codegen_1._ `${opts.code.formats}${codegen_1.getProperty(format)}` : undefined,
|
---|
55 | });
|
---|
56 | cxt.fail$data(compareCode(fmt));
|
---|
57 | }
|
---|
58 | function compareCode(fmt) {
|
---|
59 | return codegen_1._ `${fmt}.compare(${data}, ${schemaCode}) ${KWDs[keyword].fail} 0`;
|
---|
60 | }
|
---|
61 | },
|
---|
62 | dependencies: ["format"],
|
---|
63 | };
|
---|
64 | const formatLimitPlugin = (ajv) => {
|
---|
65 | ajv.addKeyword(exports.formatLimitDefinition);
|
---|
66 | return ajv;
|
---|
67 | };
|
---|
68 | exports.default = formatLimitPlugin;
|
---|
69 | //# sourceMappingURL=limit.js.map |
---|