const dataType_1 = require("./compile/validate/dataType");
const util_1 = require("./compile/util");
const $dataRefSchema = require("./refs/data.json");
+const defaultRegExp = (str, flags) => new RegExp(str, flags);
+defaultRegExp.code = "new RegExp";
const META_IGNORE_OPTIONS = ["removeAdditional", "useDefaults", "coerceTypes"];
const EXT_SCOPE_NAMES = new Set([
"validate",
missingRefs: "Pass empty schema with $id that should be ignored to ajv.addSchema.",
processCode: "Use option `code: {process: (code, schemaEnv: object) => string}`",
sourceCode: "Use option `code: {source: true}`",
- schemaId: "JSON Schema draft-04 is not supported in Ajv v7/8.",
strictDefaults: "It is default now, see option `strict`.",
strictKeywords: "It is default now, see option `strict`.",
uniqueItems: '"uniqueItems" keyword is always validated.',
unknownFormats: "Disable strict mode or pass `true` to `ajv.addFormat` (or `formats` option).",
cache: "Map is used as cache, schema object as key.",
serialize: "Map is used as cache, schema object as key.",
- ajvErrors: "It is default now, see option `strict`.",
+ ajvErrors: "It is default now.",
};
const deprecatedOptions = {
ignoreKeywordsWithRef: "",
const MAX_EXPRESSION = 200;
// eslint-disable-next-line complexity
function requiredOptions(o) {
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
const s = o.strict;
const _optz = (_a = o.code) === null || _a === void 0 ? void 0 : _a.optimize;
const optimize = _optz === true || _optz === undefined ? 1 : _optz || 0;
+ const regExp = (_c = (_b = o.code) === null || _b === void 0 ? void 0 : _b.regExp) !== null && _c !== void 0 ? _c : defaultRegExp;
return {
- strictSchema: (_c = (_b = o.strictSchema) !== null && _b !== void 0 ? _b : s) !== null && _c !== void 0 ? _c : true,
- strictNumbers: (_e = (_d = o.strictNumbers) !== null && _d !== void 0 ? _d : s) !== null && _e !== void 0 ? _e : true,
- strictTypes: (_g = (_f = o.strictTypes) !== null && _f !== void 0 ? _f : s) !== null && _g !== void 0 ? _g : "log",
- strictTuples: (_j = (_h = o.strictTuples) !== null && _h !== void 0 ? _h : s) !== null && _j !== void 0 ? _j : "log",
- strictRequired: (_l = (_k = o.strictRequired) !== null && _k !== void 0 ? _k : s) !== null && _l !== void 0 ? _l : false,
- code: o.code ? { ...o.code, optimize } : { optimize },
- loopRequired: (_m = o.loopRequired) !== null && _m !== void 0 ? _m : MAX_EXPRESSION,
- loopEnum: (_o = o.loopEnum) !== null && _o !== void 0 ? _o : MAX_EXPRESSION,
- meta: (_p = o.meta) !== null && _p !== void 0 ? _p : true,
- messages: (_q = o.messages) !== null && _q !== void 0 ? _q : true,
- inlineRefs: (_r = o.inlineRefs) !== null && _r !== void 0 ? _r : true,
- addUsedSchema: (_s = o.addUsedSchema) !== null && _s !== void 0 ? _s : true,
- validateSchema: (_t = o.validateSchema) !== null && _t !== void 0 ? _t : true,
- validateFormats: (_u = o.validateFormats) !== null && _u !== void 0 ? _u : true,
+ strictSchema: (_e = (_d = o.strictSchema) !== null && _d !== void 0 ? _d : s) !== null && _e !== void 0 ? _e : true,
+ strictNumbers: (_g = (_f = o.strictNumbers) !== null && _f !== void 0 ? _f : s) !== null && _g !== void 0 ? _g : true,
+ strictTypes: (_j = (_h = o.strictTypes) !== null && _h !== void 0 ? _h : s) !== null && _j !== void 0 ? _j : "log",
+ strictTuples: (_l = (_k = o.strictTuples) !== null && _k !== void 0 ? _k : s) !== null && _l !== void 0 ? _l : "log",
+ strictRequired: (_o = (_m = o.strictRequired) !== null && _m !== void 0 ? _m : s) !== null && _o !== void 0 ? _o : false,
+ code: o.code ? { ...o.code, optimize, regExp } : { optimize, regExp },
+ loopRequired: (_p = o.loopRequired) !== null && _p !== void 0 ? _p : MAX_EXPRESSION,
+ loopEnum: (_q = o.loopEnum) !== null && _q !== void 0 ? _q : MAX_EXPRESSION,
+ meta: (_r = o.meta) !== null && _r !== void 0 ? _r : true,
+ messages: (_s = o.messages) !== null && _s !== void 0 ? _s : true,
+ inlineRefs: (_t = o.inlineRefs) !== null && _t !== void 0 ? _t : true,
+ schemaId: (_u = o.schemaId) !== null && _u !== void 0 ? _u : "$id",
+ addUsedSchema: (_v = o.addUsedSchema) !== null && _v !== void 0 ? _v : true,
+ validateSchema: (_w = o.validateSchema) !== null && _w !== void 0 ? _w : true,
+ validateFormats: (_x = o.validateFormats) !== null && _x !== void 0 ? _x : true,
+ unicodeRegExp: (_y = o.unicodeRegExp) !== null && _y !== void 0 ? _y : true,
+ int32range: (_z = o.int32range) !== null && _z !== void 0 ? _z : true,
};
}
class Ajv {
this.logger = getLogger(opts.logger);
const formatOpt = opts.validateFormats;
opts.validateFormats = false;
- this.RULES = rules_1.getRules();
+ this.RULES = (0, rules_1.getRules)();
checkOptions.call(this, removedOptions, opts, "NOT SUPPORTED");
checkOptions.call(this, deprecatedOptions, opts, "DEPRECATED", "warn");
this._metaOpts = getMetaSchemaOptions.call(this);
this.addKeyword("$async");
}
_addDefaultMetaSchema() {
- const { $data, meta } = this.opts;
+ const { $data, meta, schemaId } = this.opts;
+ let _dataRefSchema = $dataRefSchema;
+ if (schemaId === "id") {
+ _dataRefSchema = { ...$dataRefSchema };
+ _dataRefSchema.id = _dataRefSchema.$id;
+ delete _dataRefSchema.$id;
+ }
if (meta && $data)
- this.addMetaSchema($dataRefSchema, $dataRefSchema.$id, false);
+ this.addMetaSchema(_dataRefSchema, _dataRefSchema[schemaId], false);
}
defaultMeta() {
- const { meta } = this.opts;
- return (this.opts.defaultMeta = typeof meta == "object" ? meta.$id || meta : undefined);
+ const { meta, schemaId } = this.opts;
+ return (this.opts.defaultMeta = typeof meta == "object" ? meta[schemaId] || meta : undefined);
}
validate(schemaKeyRef, // key, ref or schema object
data // to be validated
}
let id;
if (typeof schema === "object") {
- id = schema.$id;
- if (id !== undefined && typeof id != "string")
- throw new Error("schema $id must be string");
+ const { schemaId } = this.opts;
+ id = schema[schemaId];
+ if (id !== undefined && typeof id != "string") {
+ throw new Error(`schema ${schemaId} must be string`);
+ }
}
- key = resolve_1.normalizeId(key || id);
+ key = (0, resolve_1.normalizeId)(key || id);
this._checkUnique(key);
this.schemas[key] = this._addSchema(schema, _meta, key, _validateSchema, true);
return this;
while (typeof (sch = getSchEnv.call(this, keyRef)) == "string")
keyRef = sch;
if (sch === undefined) {
- const root = new compile_1.SchemaEnv({ schema: {} });
+ const { schemaId } = this.opts;
+ const root = new compile_1.SchemaEnv({ schema: {}, schemaId });
sch = compile_1.resolveSchema.call(this, root, keyRef);
if (!sch)
return;
case "object": {
const cacheKey = schemaKeyRef;
this._cache.delete(cacheKey);
- let id = schemaKeyRef.$id;
+ let id = schemaKeyRef[this.opts.schemaId];
if (id) {
- id = resolve_1.normalizeId(id);
+ id = (0, resolve_1.normalizeId)(id);
delete this.schemas[id];
delete this.refs[id];
}
}
checkKeyword.call(this, keyword, def);
if (!def) {
- util_1.eachItem(keyword, (kwd) => addRule.call(this, kwd));
+ (0, util_1.eachItem)(keyword, (kwd) => addRule.call(this, kwd));
return this;
}
keywordMetaschema.call(this, def);
const definition = {
...def,
- type: dataType_1.getJSONTypes(def.type),
- schemaType: dataType_1.getJSONTypes(def.schemaType),
+ type: (0, dataType_1.getJSONTypes)(def.type),
+ schemaType: (0, dataType_1.getJSONTypes)(def.schemaType),
};
- util_1.eachItem(keyword, definition.type.length === 0
+ (0, util_1.eachItem)(keyword, definition.type.length === 0
? (k) => addRule.call(this, k, definition)
: (k) => definition.type.forEach((t) => addRule.call(this, k, definition, t)));
return this;
}
_addSchema(schema, meta, baseId, validateSchema = this.opts.validateSchema, addSchema = this.opts.addUsedSchema) {
let id;
+ const { schemaId } = this.opts;
if (typeof schema == "object") {
- id = schema.$id;
+ id = schema[schemaId];
}
else {
if (this.opts.jtd)
let sch = this._cache.get(schema);
if (sch !== undefined)
return sch;
- const localRefs = resolve_1.getSchemaRefs.call(this, schema);
- baseId = resolve_1.normalizeId(id || baseId);
- sch = new compile_1.SchemaEnv({ schema, meta, baseId, localRefs });
+ baseId = (0, resolve_1.normalizeId)(id || baseId);
+ const localRefs = resolve_1.getSchemaRefs.call(this, schema, baseId);
+ sch = new compile_1.SchemaEnv({ schema, schemaId, meta, baseId, localRefs });
this._cache.set(sch.schema, sch);
if (addSchema && !baseId.startsWith("#")) {
// TODO atm it is allowed to overwrite schemas without id (instead of not adding them)
}
}
function getSchEnv(keyRef) {
- keyRef = resolve_1.normalizeId(keyRef); // TODO tests fail without this line
+ keyRef = (0, resolve_1.normalizeId)(keyRef); // TODO tests fail without this line
return this.schemas[keyRef] || this.refs[keyRef];
}
function addInitialSchemas() {
const KEYWORD_NAME = /^[a-z_$][a-z0-9_$:-]*$/i;
function checkKeyword(keyword, def) {
const { RULES } = this;
- util_1.eachItem(keyword, (kwd) => {
+ (0, util_1.eachItem)(keyword, (kwd) => {
if (RULES.keywords[kwd])
throw new Error(`Keyword ${kwd} is already defined`);
if (!KEYWORD_NAME.test(kwd))
keyword,
definition: {
...definition,
- type: dataType_1.getJSONTypes(definition.type),
- schemaType: dataType_1.getJSONTypes(definition.schemaType),
+ type: (0, dataType_1.getJSONTypes)(definition.type),
+ schemaType: (0, dataType_1.getJSONTypes)(definition.schemaType),
},
};
if (definition.before)