WXL
3 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
/** @typedef {import("ajv").default} Ajv */
/** @typedef {import("ajv").Code} Code */
/** @typedef {import("ajv").Name} Name */
/** @typedef {import("ajv").KeywordErrorDefinition} KeywordErrorDefinition */
 
/**
 * @param {Ajv} ajv ajv
 * @returns {Ajv} ajv with limit keyword
 */
function addLimitKeyword(ajv) {
  const {
    _,
    str,
    KeywordCxt,
    nil,
    Name
  } = require("ajv");
 
  /**
   * @param {Code | Name} nameOrCode name or code
   * @returns {Code | Name} name or code
   */
  function par(nameOrCode) {
    return nameOrCode instanceof Name ? nameOrCode : _`(${nameOrCode})`;
  }
 
  /**
   * @param {Code} op op
   * @returns {(xValue: Code, yValue: Code) => Code} code
   */
  function mappend(op) {
    return (xValue, yValue) => xValue === nil ? yValue : yValue === nil ? xValue : _`${par(xValue)} ${op} ${par(yValue)}`;
  }
  const orCode = mappend(_`||`);
 
  // boolean OR (||) expression with the passed arguments
  /**
   * @param {...Code} args args
   * @returns {Code} code
   */
  function or(...args) {
    return args.reduce(orCode);
  }
 
  /**
   * @param {string | number} key key
   * @returns {Code} property
   */
  function getProperty(key) {
    return _`[${key}]`;
  }
  const keywords = {
    formatMaximum: {
      okStr: "<=",
      ok: _`<=`,
      fail: _`>`
    },
    formatMinimum: {
      okStr: ">=",
      ok: _`>=`,
      fail: _`<`
    },
    formatExclusiveMaximum: {
      okStr: "<",
      ok: _`<`,
      fail: _`>=`
    },
    formatExclusiveMinimum: {
      okStr: ">",
      ok: _`>`,
      fail: _`<=`
    }
  };
 
  /** @type {KeywordErrorDefinition} */
  const error = {
    message: ({
      keyword,
      schemaCode
    }) => str`should be ${keywords[(/** @type {keyof typeof keywords} */keyword)].okStr} ${schemaCode}`,
    params: ({
      keyword,
      schemaCode
    }) => _`{comparison: ${keywords[(/** @type {keyof typeof keywords} */keyword)].okStr}, limit: ${schemaCode}}`
  };
  for (const keyword of Object.keys(keywords)) {
    ajv.addKeyword({
      keyword,
      type: "string",
      schemaType: keyword.startsWith("formatExclusive") ? ["string", "boolean"] : ["string", "number"],
      $data: true,
      error,
      code(cxt) {
        const {
          gen,
          data,
          schemaCode,
          keyword,
          it
        } = cxt;
        const {
          opts,
          self
        } = it;
        if (!opts.validateFormats) return;
        const fCxt = new KeywordCxt(it,
        // eslint-disable-next-line jsdoc/no-restricted-syntax
        /** @type {any} */
        self.RULES.all.format.definition, "format");
 
        /**
         * @param {Name} fmt fmt
         * @returns {Code} code
         */
        function compareCode(fmt) {
          return _`${fmt}.compare(${data}, ${schemaCode}) ${keywords[(/** @type {keyof typeof keywords} */keyword)].fail} 0`;
        }
 
        /**
         * @returns {void}
         */
        function validate$DataFormat() {
          const fmts = gen.scopeValue("formats", {
            ref: self.formats,
            code: opts.code.formats
          });
          const fmt = gen.const("fmt", _`${fmts}[${fCxt.schemaCode}]`);
          cxt.fail$data(or(_`typeof ${fmt} != "object"`, _`${fmt} instanceof RegExp`, _`typeof ${fmt}.compare != "function"`, compareCode(fmt)));
        }
 
        /**
         * @returns {void}
         */
        function validateFormat() {
          const format = fCxt.schema;
          const fmtDef = self.formats[format];
          if (!fmtDef || fmtDef === true) {
            return;
          }
          if (typeof fmtDef !== "object" || fmtDef instanceof RegExp || typeof fmtDef.compare !== "function") {
            throw new Error(`"${keyword}": format "${format}" does not define "compare" function`);
          }
          const fmt = gen.scopeValue("formats", {
            key: format,
            ref: fmtDef,
            code: opts.code.formats ? _`${opts.code.formats}${getProperty(format)}` : undefined
          });
          cxt.fail$data(compareCode(fmt));
        }
        if (fCxt.$data) {
          validate$DataFormat();
        } else {
          validateFormat();
        }
      },
      dependencies: ["format"]
    });
  }
  return ajv;
}
var _default = exports.default = addLimitKeyword;