Socket
Socket
Sign inDemoInstall

ajv

Package Overview
Dependencies
Maintainers
2
Versions
355
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv - npm Package Compare versions

Comparing version 8.4.0 to 8.5.0

2

dist/compile/index.d.ts

@@ -47,2 +47,3 @@ import type { AnySchema, AnySchemaObject, AnyValidateFunction, EvaluatedProperties, EvaluatedItems } from "../types";

readonly schema: AnySchema;
readonly schemaId?: "$id" | "id";
readonly root?: SchemaEnv;

@@ -56,2 +57,3 @@ readonly baseId?: string;

readonly schema: AnySchema;
readonly schemaId?: "$id" | "id";
readonly root: SchemaEnv;

@@ -58,0 +60,0 @@ baseId: string;

22

dist/compile/index.js

@@ -20,4 +20,5 @@ "use strict";

this.schema = env.schema;
this.schemaId = env.schemaId;
this.root = env.root || this;
this.baseId = (_a = env.baseId) !== null && _a !== void 0 ? _a : resolve_1.normalizeId(schema === null || schema === void 0 ? void 0 : schema.$id);
this.baseId = (_a = env.baseId) !== null && _a !== void 0 ? _a : resolve_1.normalizeId(schema === null || schema === void 0 ? void 0 : schema[env.schemaId || "$id"]);
this.schemaPath = env.schemaPath;

@@ -137,4 +138,5 @@ this.localRefs = env.localRefs;

const schema = (_a = root.localRefs) === null || _a === void 0 ? void 0 : _a[ref]; // TODO maybe localRefs should hold SchemaEnv
const { schemaId } = this.opts;
if (schema)
_sch = new SchemaEnv({ schema, root, baseId });
_sch = new SchemaEnv({ schema, schemaId, root, baseId });
}

@@ -197,5 +199,7 @@ if (_sch === undefined)

const { schema } = schOrRef;
if (schema.$id)
baseId = resolve_1.resolveUrl(baseId, schema.$id);
return new SchemaEnv({ schema, root, baseId });
const { schemaId } = this.opts;
const schId = schema[schemaId];
if (schId)
baseId = resolve_1.resolveUrl(baseId, schId);
return new SchemaEnv({ schema, schemaId, root, baseId });
}

@@ -223,4 +227,5 @@ return getJsonPointer.call(this, p, schOrRef);

// TODO PREVENT_SCOPE_CHANGE could be defined in keyword def?
if (!PREVENT_SCOPE_CHANGE.has(part) && typeof schema == "object" && schema.$id) {
baseId = resolve_1.resolveUrl(baseId, schema.$id);
const schId = typeof schema == "object" && schema[this.opts.schemaId];
if (!PREVENT_SCOPE_CHANGE.has(part) && schId) {
baseId = resolve_1.resolveUrl(baseId, schId);
}

@@ -235,3 +240,4 @@ }

// so that compileAsync loads missing schema.
env = env || new SchemaEnv({ schema, root, baseId });
const { schemaId } = this.opts;
env = env || new SchemaEnv({ schema, schemaId, root, baseId });
if (env.schema !== env.root.schema)

@@ -238,0 +244,0 @@ return env;

@@ -97,5 +97,6 @@ "use strict";

return {};
const schemaId = normalizeId(schema.$id);
const baseIds = { "": schemaId };
const pathPrefix = getFullPath(schemaId, false);
const { schemaId } = this.opts;
const schId = normalizeId(schema[schemaId]);
const baseIds = { "": schId };
const pathPrefix = getFullPath(schId, false);
const localRefs = {};

@@ -108,4 +109,4 @@ const schemaRefs = new Set();

let baseId = baseIds[parentJsonPtr];
if (typeof sch.$id == "string")
baseId = addRef.call(this, sch.$id);
if (typeof sch[schemaId] == "string")
baseId = addRef.call(this, sch[schemaId]);
addAnchor.call(this, sch.$anchor);

@@ -112,0 +113,0 @@ addAnchor.call(this, sch.$dynamicAnchor);

@@ -83,5 +83,4 @@ "use strict";

function funcSourceUrl(schema, opts) {
return typeof schema == "object" && schema.$id && (opts.code.source || opts.code.process)
? codegen_1._ `/*# sourceURL=${schema.$id} */`
: codegen_1.nil;
const schId = typeof schema == "object" && schema[opts.schemaId];
return schId && (opts.code.source || opts.code.process) ? codegen_1._ `/*# sourceURL=${schId} */` : codegen_1.nil;
}

@@ -145,4 +144,5 @@ // schema compilation - this function is used recursively to generate code for sub-schemas

function updateContext(it) {
if (it.schema.$id)
it.baseId = resolve_1.resolveUrl(it.baseId, it.schema.$id);
const schId = it.schema[it.opts.schemaId];
if (schId)
it.baseId = resolve_1.resolveUrl(it.baseId, schId);
}

@@ -149,0 +149,0 @@ function checkAsyncSchema(it) {

@@ -56,2 +56,3 @@ export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, AnyValidateFunction, ErrorObject, ErrorNoParams, } from "./types";

dynamicRef?: boolean;
schemaId?: "id" | "$id";
jtd?: boolean;

@@ -91,3 +92,3 @@ meta?: SchemaObject | boolean;

declare type RequiredInstanceOptions = {
[K in "strictSchema" | "strictNumbers" | "strictTypes" | "strictTuples" | "strictRequired" | "inlineRefs" | "loopRequired" | "loopEnum" | "meta" | "messages" | "addUsedSchema" | "validateSchema" | "validateFormats" | "unicodeRegExp"]: NonNullable<Options[K]>;
[K in "strictSchema" | "strictNumbers" | "strictTypes" | "strictTuples" | "strictRequired" | "inlineRefs" | "loopRequired" | "loopEnum" | "meta" | "messages" | "schemaId" | "addUsedSchema" | "validateSchema" | "validateFormats" | "unicodeRegExp"]: NonNullable<Options[K]>;
} & {

@@ -94,0 +95,0 @@ code: InstanceCodeOptions;

@@ -47,3 +47,2 @@ "use strict";

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`.",

@@ -65,3 +64,3 @@ strictKeywords: "It is default now, see option `strict`.",

function requiredOptions(o) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
const s = o.strict;

@@ -82,6 +81,7 @@ const _optz = (_a = o.code) === null || _a === void 0 ? void 0 : _a.optimize;

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,
unicodeRegExp: (_v = o.unicodeRegExp) !== null && _v !== void 0 ? _v : true,
schemaId: (_s = o.schemaId) !== null && _s !== void 0 ? _s : "$id",
addUsedSchema: (_t = o.addUsedSchema) !== null && _t !== void 0 ? _t : true,
validateSchema: (_u = o.validateSchema) !== null && _u !== void 0 ? _u : true,
validateFormats: (_v = o.validateFormats) !== null && _v !== void 0 ? _v : true,
unicodeRegExp: (_w = o.unicodeRegExp) !== null && _w !== void 0 ? _w : true,
};

@@ -122,9 +122,15 @@ }

_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);
}

@@ -217,5 +223,7 @@ validate(schemaKeyRef, // key, ref or schema object

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`);
}
}

@@ -267,3 +275,4 @@ key = resolve_1.normalizeId(key || id);

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);

@@ -303,3 +312,3 @@ if (!sch)

this._cache.delete(cacheKey);
let id = schemaKeyRef.$id;
let id = schemaKeyRef[this.opts.schemaId];
if (id) {

@@ -427,4 +436,5 @@ id = resolve_1.normalizeId(id);

let id;
const { schemaId } = this.opts;
if (typeof schema == "object") {
id = schema.$id;
id = schema[schemaId];
}

@@ -442,3 +452,3 @@ else {

baseId = resolve_1.normalizeId(id || baseId);
sch = new compile_1.SchemaEnv({ schema, meta, baseId, localRefs });
sch = new compile_1.SchemaEnv({ schema, schemaId, meta, baseId, localRefs });
this._cache.set(sch.schema, sch);

@@ -445,0 +455,0 @@ if (addSchema && !baseId.startsWith("#")) {

@@ -7,2 +7,3 @@ import type { CodeGen, Code, Name, ScopeValueSets, ValueScopeName } from "../compile/codegen";

interface _SchemaObject {
id?: string;
$id?: string;

@@ -13,2 +14,3 @@ $schema?: string;

export interface SchemaObject extends _SchemaObject {
id?: string;
$id?: string;

@@ -15,0 +17,0 @@ $schema?: string;

@@ -24,3 +24,4 @@ "use strict";

const { root, baseId, localRefs, meta } = schemaEnv.root;
const sch = new compile_1.SchemaEnv({ schema, root, baseId, localRefs, meta });
const { schemaId } = self.opts;
const sch = new compile_1.SchemaEnv({ schema, schemaId, root, baseId, localRefs, meta });
compile_1.compileSchema.call(self, sch);

@@ -27,0 +28,0 @@ return ref_1.getValidate(cxt, sch);

@@ -15,5 +15,9 @@ "use strict";

code(cxt) {
const { gen, data, schemaCode } = cxt;
// TODO optimize for scalar values in schema
cxt.fail$data(codegen_1._ `!${util_1.useFunc(gen, equal_1.default)}(${data}, ${schemaCode})`);
const { gen, data, $data, schemaCode, schema } = cxt;
if ($data || (schema && typeof schema == "object")) {
cxt.fail$data(codegen_1._ `!${util_1.useFunc(gen, equal_1.default)}(${data}, ${schemaCode})`);
}
else {
cxt.fail(codegen_1._ `${schema} !== ${data}`);
}
},

@@ -20,0 +24,0 @@ };

@@ -40,3 +40,3 @@ "use strict";

const sch = schema[i];
return sch && typeof sch === "object"
return typeof sch === "object" && sch !== null
? codegen_1._ `${eql}(${data}, ${vSchema}[${i}])`

@@ -43,0 +43,0 @@ : codegen_1._ `${data} === ${sch}`;

@@ -66,2 +66,3 @@ import type {

readonly schema: AnySchema
readonly schemaId?: "$id" | "id"
readonly root?: SchemaEnv

@@ -76,2 +77,3 @@ readonly baseId?: string

readonly schema: AnySchema
readonly schemaId?: "$id" | "id"
readonly root: SchemaEnv

@@ -96,4 +98,5 @@ baseId: string // TODO possibly, it should be readonly

this.schema = env.schema
this.schemaId = env.schemaId
this.root = env.root || this
this.baseId = env.baseId ?? normalizeId(schema?.$id)
this.baseId = env.baseId ?? normalizeId(schema?.[env.schemaId || "$id"])
this.schemaPath = env.schemaPath

@@ -218,3 +221,4 @@ this.localRefs = env.localRefs

const schema = root.localRefs?.[ref] // TODO maybe localRefs should hold SchemaEnv
if (schema) _sch = new SchemaEnv({schema, root, baseId})
const {schemaId} = this.opts
if (schema) _sch = new SchemaEnv({schema, schemaId, root, baseId})
}

@@ -280,4 +284,6 @@

const {schema} = schOrRef
if (schema.$id) baseId = resolveUrl(baseId, schema.$id)
return new SchemaEnv({schema, root, baseId})
const {schemaId} = this.opts
const schId = schema[schemaId]
if (schId) baseId = resolveUrl(baseId, schId)
return new SchemaEnv({schema, schemaId, root, baseId})
}

@@ -306,4 +312,5 @@ return getJsonPointer.call(this, p, schOrRef)

// TODO PREVENT_SCOPE_CHANGE could be defined in keyword def?
if (!PREVENT_SCOPE_CHANGE.has(part) && typeof schema == "object" && schema.$id) {
baseId = resolveUrl(baseId, schema.$id)
const schId = typeof schema == "object" && schema[this.opts.schemaId]
if (!PREVENT_SCOPE_CHANGE.has(part) && schId) {
baseId = resolveUrl(baseId, schId)
}

@@ -318,5 +325,6 @@ }

// so that compileAsync loads missing schema.
env = env || new SchemaEnv({schema, root, baseId})
const {schemaId} = this.opts
env = env || new SchemaEnv({schema, schemaId, root, baseId})
if (env.schema !== env.root.schema) return env
return undefined
}

@@ -94,5 +94,6 @@ import type {AnySchema, AnySchemaObject} from "../types"

if (typeof schema == "boolean") return {}
const schemaId = normalizeId(schema.$id)
const baseIds: {[JsonPtr in string]?: string} = {"": schemaId}
const pathPrefix = getFullPath(schemaId, false)
const {schemaId} = this.opts
const schId = normalizeId(schema[schemaId])
const baseIds: {[JsonPtr in string]?: string} = {"": schId}
const pathPrefix = getFullPath(schId, false)
const localRefs: LocalRefs = {}

@@ -105,3 +106,3 @@ const schemaRefs: Set<string> = new Set()

let baseId = baseIds[parentJsonPtr]
if (typeof sch.$id == "string") baseId = addRef.call(this, sch.$id)
if (typeof sch[schemaId] == "string") baseId = addRef.call(this, sch[schemaId])
addAnchor.call(this, sch.$anchor)

@@ -108,0 +109,0 @@ addAnchor.call(this, sch.$dynamicAnchor)

@@ -115,5 +115,4 @@ import type {

function funcSourceUrl(schema: AnySchema, opts: InstanceOptions): Code {
return typeof schema == "object" && schema.$id && (opts.code.source || opts.code.process)
? _`/*# sourceURL=${schema.$id} */`
: nil
const schId = typeof schema == "object" && schema[opts.schemaId]
return schId && (opts.code.source || opts.code.process) ? _`/*# sourceURL=${schId} */` : nil
}

@@ -181,3 +180,4 @@

function updateContext(it: SchemaObjCxt): void {
if (it.schema.$id) it.baseId = resolveUrl(it.baseId, it.schema.$id)
const schId = it.schema[it.opts.schemaId]
if (schId) it.baseId = resolveUrl(it.baseId, schId)
}

@@ -184,0 +184,0 @@

@@ -120,2 +120,3 @@ export {

dynamicRef?: boolean // NEW
schemaId?: "id" | "$id"
jtd?: boolean // NEW

@@ -167,3 +168,2 @@ meta?: SchemaObject | boolean

sourceCode?: boolean
schemaId?: string
strictDefaults?: boolean

@@ -191,3 +191,2 @@ strictKeywords?: boolean

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`.",

@@ -220,2 +219,3 @@ strictKeywords: "It is default now, see option `strict`.",

| "messages"
| "schemaId"
| "addUsedSchema"

@@ -248,2 +248,3 @@ | "validateSchema"

inlineRefs: o.inlineRefs ?? true,
schemaId: o.schemaId ?? "$id",
addUsedSchema: o.addUsedSchema ?? true,

@@ -307,9 +308,15 @@ validateSchema: o.validateSchema ?? true,

_addDefaultMetaSchema(): void {
const {$data, meta} = this.opts
if (meta && $data) this.addMetaSchema($dataRefSchema, $dataRefSchema.$id, false)
const {$data, meta, schemaId} = this.opts
let _dataRefSchema: SchemaObject = $dataRefSchema
if (schemaId === "id") {
_dataRefSchema = {...$dataRefSchema}
_dataRefSchema.id = _dataRefSchema.$id
delete _dataRefSchema.$id
}
if (meta && $data) this.addMetaSchema(_dataRefSchema, _dataRefSchema[schemaId], false)
}
defaultMeta(): string | AnySchemaObject | undefined {
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)
}

@@ -459,4 +466,7 @@

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`)
}
}

@@ -509,3 +519,4 @@ key = normalizeId(key || id)

if (sch === undefined) {
const root = new SchemaEnv({schema: {}})
const {schemaId} = this.opts
const root = new SchemaEnv({schema: {}, schemaId})
sch = resolveSchema.call(this, root, keyRef)

@@ -544,3 +555,3 @@ if (!sch) return

this._cache.delete(cacheKey)
let id = schemaKeyRef.$id
let id = schemaKeyRef[this.opts.schemaId]
if (id) {

@@ -682,4 +693,5 @@ id = normalizeId(id)

let id: string | undefined
const {schemaId} = this.opts
if (typeof schema == "object") {
id = schema.$id
id = schema[schemaId]
} else {

@@ -694,3 +706,3 @@ if (this.opts.jtd) throw new Error("schema must be object")

baseId = normalizeId(id || baseId)
sch = new SchemaEnv({schema, meta, baseId, localRefs})
sch = new SchemaEnv({schema, schemaId, meta, baseId, localRefs})
this._cache.set(sch.schema, sch)

@@ -697,0 +709,0 @@ if (addSchema && !baseId.startsWith("#")) {

@@ -8,2 +8,3 @@ import type {CodeGen, Code, Name, ScopeValueSets, ValueScopeName} from "../compile/codegen"

interface _SchemaObject {
id?: string
$id?: string

@@ -15,2 +16,3 @@ $schema?: string

export interface SchemaObject extends _SchemaObject {
id?: string
$id?: string

@@ -17,0 +19,0 @@ $schema?: string

@@ -25,3 +25,4 @@ import type {CodeKeywordDefinition} from "../../types"

const {root, baseId, localRefs, meta} = schemaEnv.root
const sch = new SchemaEnv({schema, root, baseId, localRefs, meta})
const {schemaId} = self.opts
const sch = new SchemaEnv({schema, schemaId, root, baseId, localRefs, meta})
compileSchema.call(self, sch)

@@ -28,0 +29,0 @@ return getValidate(cxt, sch)

@@ -19,5 +19,8 @@ import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types"

code(cxt: KeywordCxt) {
const {gen, data, schemaCode} = cxt
// TODO optimize for scalar values in schema
cxt.fail$data(_`!${useFunc(gen, equal)}(${data}, ${schemaCode})`)
const {gen, data, $data, schemaCode, schema} = cxt
if ($data || (schema && typeof schema == "object")) {
cxt.fail$data(_`!${useFunc(gen, equal)}(${data}, ${schemaCode})`)
} else {
cxt.fail(_`${schema} !== ${data}`)
}
},

@@ -24,0 +27,0 @@ }

@@ -45,3 +45,3 @@ import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types"

const sch = schema[i]
return sch && typeof sch === "object"
return typeof sch === "object" && sch !== null
? _`${eql}(${data}, ${vSchema}[${i}])`

@@ -48,0 +48,0 @@ : _`${data} === ${sch}`

{
"name": "ajv",
"version": "8.4.0",
"version": "8.5.0",
"description": "Another JSON Schema Validator",

@@ -5,0 +5,0 @@ "main": "dist/ajv.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc