core.js 23 KB

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