Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

schema-utils

Package Overview
Dependencies
Maintainers
2
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schema-utils - npm Package Compare versions

Comparing version
4.2.0
to
4.3.0
+14
declarations/keywords/limit.d.ts
export default addLimitKeyword;
export type Ajv = import("ajv").default;
export type Code = import("ajv").Code;
export type Name = import("ajv").Name;
export type KeywordErrorDefinition = import("ajv").KeywordErrorDefinition;
/** @typedef {import("ajv").default} Ajv */
/** @typedef {import("ajv").Code} Code */
/** @typedef {import("ajv").Name} Name */
/** @typedef {import("ajv").KeywordErrorDefinition} KeywordErrorDefinition */
/**
* @param {Ajv} ajv
* @returns {Ajv}
*/
declare function addLimitKeyword(ajv: Ajv): Ajv;
"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
* @returns {Ajv}
*/
function addLimitKeyword(ajv) {
// eslint-disable-next-line global-require
const {
_,
str,
KeywordCxt,
nil,
Name
} = require("ajv");
/**
* @param {Code | Name} x
* @returns {Code | Name}
*/
function par(x) {
return x instanceof Name ? x : _`(${x})`;
}
/**
* @param {Code} op
* @returns {function(Code, Code): Code}
*/
function mappend(op) {
return (x, y) => x === nil ? y : y === nil ? x : _`${par(x)} ${op} ${par(y)}`;
}
const orCode = mappend(_`||`);
// boolean OR (||) expression with the passed arguments
/**
* @param {...Code} args
* @returns {Code}
*/
function or(...args) {
return args.reduce(orCode);
}
/**
* @param {string | number} key
* @returns {Code}
*/
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, /** @type {any} */
self.RULES.all.format.definition, "format");
/**
* @param {Name} fmt
* @returns {Code}
*/
function compareCode(fmt) {
return _`${fmt}.compare(${data}, ${schemaCode}) ${keywords[(/** @type {keyof typeof keywords} */keyword)].fail} 0`;
}
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)));
}
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;
+4
-4

@@ -6,6 +6,6 @@ export type JSONSchema4 = import("json-schema").JSONSchema4;

export type Extend = {
formatMinimum?: string | undefined;
formatMaximum?: string | undefined;
formatExclusiveMinimum?: string | undefined;
formatExclusiveMaximum?: string | undefined;
formatMinimum?: (string | number) | undefined;
formatMaximum?: (string | number) | undefined;
formatExclusiveMinimum?: (string | boolean) | undefined;
formatExclusiveMaximum?: (string | boolean) | undefined;
link?: string | undefined;

@@ -12,0 +12,0 @@ undefinedAsNull?: boolean | undefined;

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

}
var _default = addAbsolutePathKeyword;
exports.default = _default;
var _default = exports.default = addAbsolutePathKeyword;

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

}
var _default = addUndefinedAsNullKeyword;
exports.default = _default;
var _default = exports.default = addUndefinedAsNullKeyword;

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

};
var _default = memoize;
exports.default = _default;
var _default = exports.default = memoize;

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

var _memorize = _interopRequireDefault(require("./util/memorize"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const getAjv = (0, _memorize.default)(() => {

@@ -39,4 +39,5 @@ // Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110).

ajvKeywords(ajv, ["instanceof", "patternRequired"]);
// TODO set `{ keywords: true }` for the next major release and remove `keywords/limit.js`
addFormats(ajv, {
keywords: true
keywords: false
});

@@ -48,2 +49,6 @@

addAbsolutePathKeyword(ajv);
// eslint-disable-next-line global-require
const addLimitKeyword = require("./keywords/limit").default;
addLimitKeyword(ajv);
const addUndefinedAsNullKeyword =

@@ -63,6 +68,6 @@ // eslint-disable-next-line global-require

* @typedef {Object} Extend
* @property {string=} formatMinimum
* @property {string=} formatMaximum
* @property {string=} formatExclusiveMinimum
* @property {string=} formatExclusiveMaximum
* @property {(string | number)=} formatMinimum
* @property {(string | number)=} formatMaximum
* @property {(string | boolean)=} formatExclusiveMinimum
* @property {(string | boolean)=} formatExclusiveMaximum
* @property {string=} link

@@ -186,3 +191,3 @@ * @property {boolean=} undefinedAsNull

let newErrors = [];
for (const error of /** @type {Array<SchemaUtilErrorObject>} */errors) {
for (const error of (/** @type {Array<SchemaUtilErrorObject>} */errors)) {
const {

@@ -189,0 +194,0 @@ instancePath

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

var _memorize = _interopRequireDefault(require("./util/memorize"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */

@@ -17,2 +17,3 @@ /** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */

/** @typedef {import("./validate").SchemaUtilErrorObject} SchemaUtilErrorObject */
/** @enum {number} */

@@ -92,3 +93,3 @@ const SPECIFICITY = {

*/
error => SPECIFICITY[/** @type {keyof typeof SPECIFICITY} */error.keyword] || 2);
error => SPECIFICITY[(/** @type {keyof typeof SPECIFICITY} */error.keyword)] || 2);
return newChildren;

@@ -214,3 +215,3 @@ }

function isObject(maybeObj) {
return typeof maybeObj === "object" && maybeObj !== null;
return typeof maybeObj === "object" && !Array.isArray(maybeObj) && maybeObj !== null;
}

@@ -387,3 +388,3 @@

for (let i = 1; i < newPath.length; i++) {
const inner = schemaPart[/** @type {keyof Schema} */newPath[i]];
const inner = schemaPart[(/** @type {keyof Schema} */newPath[i])];
if (!inner) {

@@ -431,3 +432,3 @@ break;

}
if ( /** @type {Schema & {instanceof: string | Array<string>}} */schema.instanceof) {
if (/** @type {Schema & {instanceof: string | Array<string>}} */schema.instanceof) {
const {

@@ -457,14 +458,11 @@ instanceof: value

if (schema.oneOf) {
return (/** @type {Array<Schema>} */schema.oneOf.map(item => formatInnerSchema(item, true)).join(" | ")
);
return /** @type {Array<Schema>} */schema.oneOf.map(item => formatInnerSchema(item, true)).join(" | ");
}
if (schema.anyOf) {
return (/** @type {Array<Schema>} */schema.anyOf.map(item => formatInnerSchema(item, true)).join(" | ")
);
return /** @type {Array<Schema>} */schema.anyOf.map(item => formatInnerSchema(item, true)).join(" | ");
}
if (schema.allOf) {
return (/** @type {Array<Schema>} */schema.allOf.map(item => formatInnerSchema(item, true)).join(" & ")
);
return /** @type {Array<Schema>} */schema.allOf.map(item => formatInnerSchema(item, true)).join(" & ");
}
if ( /** @type {JSONSchema7} */schema.if) {
if (/** @type {JSONSchema7} */schema.if) {
const {

@@ -510,4 +508,3 @@ if: ifValue,

if (Array.isArray(schema.items) && schema.items.length > 0) {
items = `${
/** @type {Array<Schema>} */schema.items.map(item => formatInnerSchema(item)).join(", ")}`;
items = `${/** @type {Array<Schema>} */schema.items.map(item => formatInnerSchema(item)).join(", ")}`;
if (hasAdditionalItems) {

@@ -552,3 +549,3 @@ if (schema.additionalItems && isObject(schema.additionalItems) && Object.keys(schema.additionalItems).length > 0) {

const required = schema.required ? schema.required : [];
const allProperties = [...new Set( /** @type {Array<string>} */[].concat(required).concat(properties))];
const allProperties = [...new Set(/** @type {Array<string>} */[].concat(required).concat(properties))];
const objectStructure = allProperties.map(property => {

@@ -617,3 +614,3 @@ const isRequired = required.includes(property);

/** @type {Schema | undefined} */
const inner = schemaPart[/** @type {keyof Schema} */additionalPath[i]];
const inner = schemaPart[(/** @type {keyof Schema} */additionalPath[i])];
if (inner) {

@@ -777,3 +774,3 @@ // eslint-disable-next-line no-param-reassign

} = params;
const [, ...hints] = getHints( /** @type {Schema} */parentSchema, true);
const [, ...hints] = getHints(/** @type {Schema} */parentSchema, true);
if (hints.length === 0) {

@@ -892,4 +889,4 @@ hints.push(`should be ${comparison} ${limit}`);

} = params;
return `${instancePath} should not contain the item '${
/** @type {{ data: Array<any> }} **/error.data[i]}' twice${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
return `${instancePath} should not contain the item '${/** @type {{ data: Array<any> }} **/
error.data[i]}' twice${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
}

@@ -921,3 +918,3 @@ case "additionalItems":

const missingProperty = params.missingProperty.replace(/^\./, "");
const hasProperty = parentSchema && Boolean( /** @type {Schema} */
const hasProperty = parentSchema && Boolean(/** @type {Schema} */
parentSchema.properties && /** @type {Schema} */

@@ -989,3 +986,3 @@ parentSchema.properties[missingProperty]);

{
const postfix = likeObject( /** @type {Schema} */error.parentSchema) ? `\n${this.getSchemaPartText(error.parentSchema)}` : "";
const postfix = likeObject(/** @type {Schema} */error.parentSchema) ? `\n${this.getSchemaPartText(error.parentSchema)}` : "";
const schemaOutput = this.getSchemaPartText(error.schema, false, false, false);

@@ -1080,3 +1077,2 @@ if (canApplyNot(error.schema)) {

}
var _default = ValidationError;
exports.default = _default;
var _default = exports.default = ValidationError;
{
"name": "schema-utils",
"version": "4.2.0",
"version": "4.3.0",
"description": "webpack Validation Utils",

@@ -17,3 +17,3 @@ "license": "MIT",

"engines": {
"node": ">= 12.13.0"
"node": ">= 10.13.0"
},

@@ -75,3 +75,3 @@ "scripts": {

"typescript": "^4.9.5",
"webpack": "^5.68.0"
"webpack": "^5.97.1"
},

@@ -78,0 +78,0 @@ "keywords": [