core.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CodeGen = exports.Name = exports.nil = exports.stringify = exports.str = exports._ = exports.KeywordCxt = void 0;
  4. const context_1 = require("./compile/context");
  5. exports.KeywordCxt = context_1.default;
  6. var codegen_1 = require("./compile/codegen");
  7. Object.defineProperty(exports, "_", { enumerable: true, get: function () { return codegen_1._; } });
  8. Object.defineProperty(exports, "str", { enumerable: true, get: function () { return codegen_1.str; } });
  9. Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return codegen_1.stringify; } });
  10. Object.defineProperty(exports, "nil", { enumerable: true, get: function () { return codegen_1.nil; } });
  11. Object.defineProperty(exports, "Name", { enumerable: true, get: function () { return codegen_1.Name; } });
  12. Object.defineProperty(exports, "CodeGen", { enumerable: true, get: function () { return codegen_1.CodeGen; } });
  13. const error_classes_1 = require("./compile/error_classes");
  14. const rules_1 = require("./compile/rules");
  15. const compile_1 = require("./compile");
  16. const codegen_2 = require("./compile/codegen");
  17. const resolve_1 = require("./compile/resolve");
  18. const dataType_1 = require("./compile/validate/dataType");
  19. const util_1 = require("./compile/util");
  20. const $dataRefSchema = require("./refs/data.json");
  21. const META_IGNORE_OPTIONS = ["removeAdditional", "useDefaults", "coerceTypes"];
  22. const EXT_SCOPE_NAMES = new Set([
  23. "validate",
  24. "serialize",
  25. "parse",
  26. "wrapper",
  27. "root",
  28. "schema",
  29. "keyword",
  30. "pattern",
  31. "formats",
  32. "validate$data",
  33. "func",
  34. "obj",
  35. "Error",
  36. ]);
  37. const removedOptions = {
  38. errorDataPath: "",
  39. format: "`validateFormats: false` can be used instead.",
  40. nullable: '"nullable" keyword is supported by default.',
  41. jsonPointers: "Deprecated jsPropertySyntax can be used instead.",
  42. extendRefs: "Deprecated ignoreKeywordsWithRef can be used instead.",
  43. missingRefs: "Pass empty schema with $id that should be ignored to ajv.addSchema.",
  44. processCode: "Use option `code: {process: (code, schemaEnv: object) => string}`",
  45. sourceCode: "Use option `code: {source: true}`",
  46. schemaId: "JSON Schema draft-04 is not supported in Ajv v7.",
  47. strictDefaults: "It is default now, see option `strict`.",
  48. strictKeywords: "It is default now, see option `strict`.",
  49. strictNumbers: "It is default now, see option `strict`.",
  50. uniqueItems: '"uniqueItems" keyword is always validated.',
  51. unknownFormats: "Disable strict mode or pass `true` to `ajv.addFormat` (or `formats` option).",
  52. cache: "Map is used as cache, schema object as key.",
  53. serialize: "Map is used as cache, schema object as key.",
  54. };
  55. const deprecatedOptions = {
  56. ignoreKeywordsWithRef: "",
  57. jsPropertySyntax: "",
  58. unicode: '"minLength"/"maxLength" account for unicode characters by default.',
  59. };
  60. function requiredOptions(o) {
  61. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
  62. const strict = (_a = o.strict) !== null && _a !== void 0 ? _a : true;
  63. const strictLog = strict ? "log" : false;
  64. const _optz = (_b = o.code) === null || _b === void 0 ? void 0 : _b.optimize;
  65. const optimize = _optz === true || _optz === undefined ? 1 : _optz || 0;
  66. return {
  67. strict,
  68. strictTypes: (_c = o.strictTypes) !== null && _c !== void 0 ? _c : strictLog,
  69. strictTuples: (_d = o.strictTuples) !== null && _d !== void 0 ? _d : strictLog,
  70. code: o.code ? { ...o.code, optimize } : { optimize },
  71. loopRequired: (_e = o.loopRequired) !== null && _e !== void 0 ? _e : Infinity,
  72. loopEnum: (_f = o.loopEnum) !== null && _f !== void 0 ? _f : Infinity,
  73. meta: (_g = o.meta) !== null && _g !== void 0 ? _g : true,
  74. messages: (_h = o.messages) !== null && _h !== void 0 ? _h : true,
  75. inlineRefs: (_j = o.inlineRefs) !== null && _j !== void 0 ? _j : true,
  76. addUsedSchema: (_k = o.addUsedSchema) !== null && _k !== void 0 ? _k : true,
  77. validateSchema: (_l = o.validateSchema) !== null && _l !== void 0 ? _l : true,
  78. validateFormats: (_m = o.validateFormats) !== null && _m !== void 0 ? _m : true,
  79. };
  80. }
  81. class Ajv {
  82. constructor(opts = {}) {
  83. this.schemas = {};
  84. this.refs = {};
  85. this.formats = {};
  86. this._compilations = new Set();
  87. this._loading = {};
  88. this._cache = new Map();
  89. opts = this.opts = { ...opts, ...requiredOptions(opts) };
  90. const { es5, lines } = this.opts.code;
  91. this.scope = new codegen_2.ValueScope({ scope: {}, prefixes: EXT_SCOPE_NAMES, es5, lines });
  92. this.logger = getLogger(opts.logger);
  93. const formatOpt = opts.validateFormats;
  94. opts.validateFormats = false;
  95. this.RULES = rules_1.getRules();
  96. checkOptions.call(this, removedOptions, opts, "NOT SUPPORTED");
  97. checkOptions.call(this, deprecatedOptions, opts, "DEPRECATED", "warn");
  98. this._metaOpts = getMetaSchemaOptions.call(this);
  99. if (opts.formats)
  100. addInitialFormats.call(this);
  101. this._addVocabularies();
  102. this._addDefaultMetaSchema();
  103. if (opts.keywords)
  104. addInitialKeywords.call(this, opts.keywords);
  105. if (typeof opts.meta == "object")
  106. this.addMetaSchema(opts.meta);
  107. addInitialSchemas.call(this);
  108. opts.validateFormats = formatOpt;
  109. }
  110. _addVocabularies() {
  111. this.addKeyword("$async");
  112. }
  113. _addDefaultMetaSchema() {
  114. const { $data, meta } = this.opts;
  115. if (meta && $data)
  116. this.addMetaSchema($dataRefSchema, $dataRefSchema.$id, false);
  117. }
  118. defaultMeta() {
  119. const { meta } = this.opts;
  120. return (this.opts.defaultMeta = typeof meta == "object" ? meta.$id || meta : undefined);
  121. }
  122. validate(schemaKeyRef, // key, ref or schema object
  123. data // to be validated
  124. ) {
  125. let v;
  126. if (typeof schemaKeyRef == "string") {
  127. v = this.getSchema(schemaKeyRef);
  128. if (!v)
  129. throw new Error(`no schema with key or ref "${schemaKeyRef}"`);
  130. }
  131. else {
  132. v = this.compile(schemaKeyRef);
  133. }
  134. const valid = v(data);
  135. if (!("$async" in v))
  136. this.errors = v.errors;
  137. return valid;
  138. }
  139. compile(schema, _meta) {
  140. const sch = this._addSchema(schema, _meta);
  141. return (sch.validate || this._compileSchemaEnv(sch));
  142. }
  143. compileAsync(schema, meta) {
  144. if (typeof this.opts.loadSchema != "function") {
  145. throw new Error("options.loadSchema should be a function");
  146. }
  147. const { loadSchema } = this.opts;
  148. return runCompileAsync.call(this, schema, meta);
  149. async function runCompileAsync(_schema, _meta) {
  150. await loadMetaSchema.call(this, _schema.$schema);
  151. const sch = this._addSchema(_schema, _meta);
  152. return sch.validate || _compileAsync.call(this, sch);
  153. }
  154. async function loadMetaSchema($ref) {
  155. if ($ref && !this.getSchema($ref)) {
  156. await runCompileAsync.call(this, { $ref }, true);
  157. }
  158. }
  159. async function _compileAsync(sch) {
  160. try {
  161. return this._compileSchemaEnv(sch);
  162. }
  163. catch (e) {
  164. if (!(e instanceof error_classes_1.MissingRefError))
  165. throw e;
  166. checkLoaded.call(this, e);
  167. await loadMissingSchema.call(this, e.missingSchema);
  168. return _compileAsync.call(this, sch);
  169. }
  170. }
  171. function checkLoaded({ missingSchema: ref, missingRef }) {
  172. if (this.refs[ref]) {
  173. throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`);
  174. }
  175. }
  176. async function loadMissingSchema(ref) {
  177. const _schema = await _loadSchema.call(this, ref);
  178. if (!this.refs[ref])
  179. await loadMetaSchema.call(this, _schema.$schema);
  180. if (!this.refs[ref])
  181. this.addSchema(_schema, ref, meta);
  182. }
  183. async function _loadSchema(ref) {
  184. const p = this._loading[ref];
  185. if (p)
  186. return p;
  187. try {
  188. return await (this._loading[ref] = loadSchema(ref));
  189. }
  190. finally {
  191. delete this._loading[ref];
  192. }
  193. }
  194. }
  195. // Adds schema to the instance
  196. addSchema(schema, // If array is passed, `key` will be ignored
  197. key, // Optional schema key. Can be passed to `validate` method instead of schema object or id/ref. One schema per instance can have empty `id` and `key`.
  198. _meta, // true if schema is a meta-schema. Used internally, addMetaSchema should be used instead.
  199. _validateSchema = this.opts.validateSchema // false to skip schema validation. Used internally, option validateSchema should be used instead.
  200. ) {
  201. if (Array.isArray(schema)) {
  202. for (const sch of schema)
  203. this.addSchema(sch, undefined, _meta, _validateSchema);
  204. return this;
  205. }
  206. let id;
  207. if (typeof schema === "object") {
  208. id = schema.$id;
  209. if (id !== undefined && typeof id != "string")
  210. throw new Error("schema id must be string");
  211. }
  212. key = resolve_1.normalizeId(key || id);
  213. this._checkUnique(key);
  214. this.schemas[key] = this._addSchema(schema, _meta, _validateSchema, true);
  215. return this;
  216. }
  217. // Add schema that will be used to validate other schemas
  218. // options in META_IGNORE_OPTIONS are alway set to false
  219. addMetaSchema(schema, key, // schema key
  220. _validateSchema = this.opts.validateSchema // false to skip schema validation, can be used to override validateSchema option for meta-schema
  221. ) {
  222. this.addSchema(schema, key, true, _validateSchema);
  223. return this;
  224. }
  225. // Validate schema against its meta-schema
  226. validateSchema(schema, throwOrLogError) {
  227. if (typeof schema == "boolean")
  228. return true;
  229. let $schema;
  230. $schema = schema.$schema;
  231. if ($schema !== undefined && typeof $schema != "string") {
  232. throw new Error("$schema must be a string");
  233. }
  234. $schema = $schema || this.opts.defaultMeta || this.defaultMeta();
  235. if (!$schema) {
  236. this.logger.warn("meta-schema not available");
  237. this.errors = null;
  238. return true;
  239. }
  240. const valid = this.validate($schema, schema);
  241. if (!valid && throwOrLogError) {
  242. const message = "schema is invalid: " + this.errorsText();
  243. if (this.opts.validateSchema === "log")
  244. this.logger.error(message);
  245. else
  246. throw new Error(message);
  247. }
  248. return valid;
  249. }
  250. // Get compiled schema by `key` or `ref`.
  251. // (`key` that was passed to `addSchema` or full schema reference - `schema.$id` or resolved id)
  252. getSchema(keyRef) {
  253. let sch;
  254. while (typeof (sch = getSchEnv.call(this, keyRef)) == "string")
  255. keyRef = sch;
  256. if (sch === undefined) {
  257. const root = new compile_1.SchemaEnv({ schema: {} });
  258. sch = compile_1.resolveSchema.call(this, root, keyRef);
  259. if (!sch)
  260. return;
  261. this.refs[keyRef] = sch;
  262. }
  263. return (sch.validate || this._compileSchemaEnv(sch));
  264. }
  265. // Remove cached schema(s).
  266. // If no parameter is passed all schemas but meta-schemas are removed.
  267. // If RegExp is passed all schemas with key/id matching pattern but meta-schemas are removed.
  268. // Even if schema is referenced by other schemas it still can be removed as other schemas have local references.
  269. removeSchema(schemaKeyRef) {
  270. if (schemaKeyRef instanceof RegExp) {
  271. this._removeAllSchemas(this.schemas, schemaKeyRef);
  272. this._removeAllSchemas(this.refs, schemaKeyRef);
  273. return this;
  274. }
  275. switch (typeof schemaKeyRef) {
  276. case "undefined":
  277. this._removeAllSchemas(this.schemas);
  278. this._removeAllSchemas(this.refs);
  279. this._cache.clear();
  280. return this;
  281. case "string": {
  282. const sch = getSchEnv.call(this, schemaKeyRef);
  283. if (typeof sch == "object")
  284. this._cache.delete(sch.schema);
  285. delete this.schemas[schemaKeyRef];
  286. delete this.refs[schemaKeyRef];
  287. return this;
  288. }
  289. case "object": {
  290. const cacheKey = schemaKeyRef;
  291. this._cache.delete(cacheKey);
  292. let id = schemaKeyRef.$id;
  293. if (id) {
  294. id = resolve_1.normalizeId(id);
  295. delete this.schemas[id];
  296. delete this.refs[id];
  297. }
  298. return this;
  299. }
  300. default:
  301. throw new Error("ajv.removeSchema: invalid parameter");
  302. }
  303. }
  304. // add "vocabulary" - a collection of keywords
  305. addVocabulary(definitions) {
  306. for (const def of definitions)
  307. this.addKeyword(def);
  308. return this;
  309. }
  310. addKeyword(kwdOrDef, def // deprecated
  311. ) {
  312. let keyword;
  313. if (typeof kwdOrDef == "string") {
  314. keyword = kwdOrDef;
  315. if (typeof def == "object") {
  316. this.logger.warn("these parameters are deprecated, see docs for addKeyword");
  317. def.keyword = keyword;
  318. }
  319. }
  320. else if (typeof kwdOrDef == "object" && def === undefined) {
  321. def = kwdOrDef;
  322. keyword = def.keyword;
  323. if (Array.isArray(keyword) && !keyword.length) {
  324. throw new Error("addKeywords: keyword must be string or non-empty array");
  325. }
  326. }
  327. else {
  328. throw new Error("invalid addKeywords parameters");
  329. }
  330. checkKeyword.call(this, keyword, def);
  331. if (!def) {
  332. util_1.eachItem(keyword, (kwd) => addRule.call(this, kwd));
  333. return this;
  334. }
  335. keywordMetaschema.call(this, def);
  336. const definition = {
  337. ...def,
  338. type: dataType_1.getJSONTypes(def.type),
  339. schemaType: dataType_1.getJSONTypes(def.schemaType),
  340. };
  341. util_1.eachItem(keyword, definition.type.length === 0
  342. ? (k) => addRule.call(this, k, definition)
  343. : (k) => definition.type.forEach((t) => addRule.call(this, k, definition, t)));
  344. return this;
  345. }
  346. getKeyword(keyword) {
  347. const rule = this.RULES.all[keyword];
  348. return typeof rule == "object" ? rule.definition : !!rule;
  349. }
  350. // Remove keyword
  351. removeKeyword(keyword) {
  352. // TODO return type should be Ajv
  353. const { RULES } = this;
  354. delete RULES.keywords[keyword];
  355. delete RULES.all[keyword];
  356. for (const group of RULES.rules) {
  357. const i = group.rules.findIndex((rule) => rule.keyword === keyword);
  358. if (i >= 0)
  359. group.rules.splice(i, 1);
  360. }
  361. return this;
  362. }
  363. // Add format
  364. addFormat(name, format) {
  365. if (typeof format == "string")
  366. format = new RegExp(format);
  367. this.formats[name] = format;
  368. return this;
  369. }
  370. errorsText(errors = this.errors, // optional array of validation errors
  371. { separator = ", ", dataVar = "data" } = {} // optional options with properties `separator` and `dataVar`
  372. ) {
  373. if (!errors || errors.length === 0)
  374. return "No errors";
  375. return errors
  376. .map((e) => `${dataVar}${e.dataPath} ${e.message}`)
  377. .reduce((text, msg) => text + separator + msg);
  378. }
  379. $dataMetaSchema(metaSchema, keywordsJsonPointers) {
  380. const rules = this.RULES.all;
  381. metaSchema = JSON.parse(JSON.stringify(metaSchema));
  382. for (const jsonPointer of keywordsJsonPointers) {
  383. const segments = jsonPointer.split("/").slice(1); // first segment is an empty string
  384. let keywords = metaSchema;
  385. for (const seg of segments)
  386. keywords = keywords[seg];
  387. for (const key in rules) {
  388. const rule = rules[key];
  389. if (typeof rule != "object")
  390. continue;
  391. const { $data } = rule.definition;
  392. const schema = keywords[key];
  393. if ($data && schema)
  394. keywords[key] = schemaOrData(schema);
  395. }
  396. }
  397. return metaSchema;
  398. }
  399. _removeAllSchemas(schemas, regex) {
  400. for (const keyRef in schemas) {
  401. const sch = schemas[keyRef];
  402. if (!regex || regex.test(keyRef)) {
  403. if (typeof sch == "string") {
  404. delete schemas[keyRef];
  405. }
  406. else if (sch && !sch.meta) {
  407. this._cache.delete(sch.schema);
  408. delete schemas[keyRef];
  409. }
  410. }
  411. }
  412. }
  413. _addSchema(schema, meta, validateSchema = this.opts.validateSchema, addSchema = this.opts.addUsedSchema) {
  414. if (typeof schema != "object") {
  415. if (this.opts.jtd)
  416. throw new Error("schema must be object");
  417. else if (typeof schema != "boolean")
  418. throw new Error("schema must be object or boolean");
  419. }
  420. let sch = this._cache.get(schema);
  421. if (sch !== undefined)
  422. return sch;
  423. const localRefs = resolve_1.getSchemaRefs.call(this, schema);
  424. sch = new compile_1.SchemaEnv({ schema, meta, localRefs });
  425. this._cache.set(sch.schema, sch);
  426. const id = sch.baseId;
  427. if (addSchema && !id.startsWith("#")) {
  428. // TODO atm it is allowed to overwrite schemas without id (instead of not adding them)
  429. if (id)
  430. this._checkUnique(id);
  431. this.refs[id] = sch;
  432. }
  433. if (validateSchema)
  434. this.validateSchema(schema, true);
  435. return sch;
  436. }
  437. _checkUnique(id) {
  438. if (this.schemas[id] || this.refs[id]) {
  439. throw new Error(`schema with key or id "${id}" already exists`);
  440. }
  441. }
  442. _compileSchemaEnv(sch) {
  443. if (sch.meta)
  444. this._compileMetaSchema(sch);
  445. else
  446. compile_1.compileSchema.call(this, sch);
  447. /* istanbul ignore if */
  448. if (!sch.validate)
  449. throw new Error("ajv implementation error");
  450. return sch.validate;
  451. }
  452. _compileMetaSchema(sch) {
  453. const currentOpts = this.opts;
  454. this.opts = this._metaOpts;
  455. try {
  456. compile_1.compileSchema.call(this, sch);
  457. }
  458. finally {
  459. this.opts = currentOpts;
  460. }
  461. }
  462. }
  463. exports.default = Ajv;
  464. Ajv.ValidationError = error_classes_1.ValidationError;
  465. Ajv.MissingRefError = error_classes_1.MissingRefError;
  466. function checkOptions(checkOpts, options, msg, log = "error") {
  467. for (const key in checkOpts) {
  468. const opt = key;
  469. if (opt in options)
  470. this.logger[log](`${msg}: option ${key}. ${checkOpts[opt]}`);
  471. }
  472. }
  473. function getSchEnv(keyRef) {
  474. keyRef = resolve_1.normalizeId(keyRef); // TODO tests fail without this line
  475. return this.schemas[keyRef] || this.refs[keyRef];
  476. }
  477. function addInitialSchemas() {
  478. const optsSchemas = this.opts.schemas;
  479. if (!optsSchemas)
  480. return;
  481. if (Array.isArray(optsSchemas))
  482. this.addSchema(optsSchemas);
  483. else
  484. for (const key in optsSchemas)
  485. this.addSchema(optsSchemas[key], key);
  486. }
  487. function addInitialFormats() {
  488. for (const name in this.opts.formats) {
  489. const format = this.opts.formats[name];
  490. if (format)
  491. this.addFormat(name, format);
  492. }
  493. }
  494. function addInitialKeywords(defs) {
  495. if (Array.isArray(defs)) {
  496. this.addVocabulary(defs);
  497. return;
  498. }
  499. this.logger.warn("keywords option as map is deprecated, pass array");
  500. for (const keyword in defs) {
  501. const def = defs[keyword];
  502. if (!def.keyword)
  503. def.keyword = keyword;
  504. this.addKeyword(def);
  505. }
  506. }
  507. function getMetaSchemaOptions() {
  508. const metaOpts = { ...this.opts };
  509. for (const opt of META_IGNORE_OPTIONS)
  510. delete metaOpts[opt];
  511. return metaOpts;
  512. }
  513. const noLogs = { log() { }, warn() { }, error() { } };
  514. function getLogger(logger) {
  515. if (logger === false)
  516. return noLogs;
  517. if (logger === undefined)
  518. return console;
  519. if (logger.log && logger.warn && logger.error)
  520. return logger;
  521. throw new Error("logger must implement log, warn and error methods");
  522. }
  523. const KEYWORD_NAME = /^[a-z_$][a-z0-9_$:-]*$/i;
  524. function checkKeyword(keyword, def) {
  525. const { RULES } = this;
  526. util_1.eachItem(keyword, (kwd) => {
  527. if (RULES.keywords[kwd])
  528. throw new Error(`Keyword ${kwd} is already defined`);
  529. if (!KEYWORD_NAME.test(kwd))
  530. throw new Error(`Keyword ${kwd} has invalid name`);
  531. });
  532. if (!def)
  533. return;
  534. if (def.$data && !("code" in def || "validate" in def)) {
  535. throw new Error('$data keyword must have "code" or "validate" function');
  536. }
  537. }
  538. function addRule(keyword, definition, dataType) {
  539. var _a;
  540. const post = definition === null || definition === void 0 ? void 0 : definition.post;
  541. if (dataType && post)
  542. throw new Error('keyword with "post" flag cannot have "type"');
  543. const { RULES } = this;
  544. let ruleGroup = post ? RULES.post : RULES.rules.find(({ type: t }) => t === dataType);
  545. if (!ruleGroup) {
  546. ruleGroup = { type: dataType, rules: [] };
  547. RULES.rules.push(ruleGroup);
  548. }
  549. RULES.keywords[keyword] = true;
  550. if (!definition)
  551. return;
  552. const rule = {
  553. keyword,
  554. definition: {
  555. ...definition,
  556. type: dataType_1.getJSONTypes(definition.type),
  557. schemaType: dataType_1.getJSONTypes(definition.schemaType),
  558. },
  559. };
  560. if (definition.before)
  561. addBeforeRule.call(this, ruleGroup, rule, definition.before);
  562. else
  563. ruleGroup.rules.push(rule);
  564. RULES.all[keyword] = rule;
  565. (_a = definition.implements) === null || _a === void 0 ? void 0 : _a.forEach((kwd) => this.addKeyword(kwd));
  566. }
  567. function addBeforeRule(ruleGroup, rule, before) {
  568. const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before);
  569. if (i >= 0) {
  570. ruleGroup.rules.splice(i, 0, rule);
  571. }
  572. else {
  573. ruleGroup.rules.push(rule);
  574. this.logger.warn(`rule ${before} is not defined`);
  575. }
  576. }
  577. function keywordMetaschema(def) {
  578. let { metaSchema } = def;
  579. if (metaSchema === undefined)
  580. return;
  581. if (def.$data && this.opts.$data)
  582. metaSchema = schemaOrData(metaSchema);
  583. def.validateSchema = this.compile(metaSchema, true);
  584. }
  585. const $dataRef = {
  586. $ref: "https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json#",
  587. };
  588. function schemaOrData(schema) {
  589. return { anyOf: [schema, $dataRef] };
  590. }
  591. //# sourceMappingURL=core.js.map