schema-utils
Advanced tools
Comparing version 3.2.0 to 3.3.0
@@ -5,2 +5,14 @@ # Changelog | ||
## [3.3.0](https://github.com/webpack/schema-utils/compare/v3.2.0...v3.3.0) (2023-06-14) | ||
### Features | ||
* added API to disable and enable validation ([#183](https://github.com/webpack/schema-utils/issues/183)) ([d4d334f](https://github.com/webpack/schema-utils/commit/d4d334f0ba22eb6b6564b1119e8f3ea439e1f2bb)) | ||
### Bug Fixes | ||
* **perf:** cache compiled schema ([#182](https://github.com/webpack/schema-utils/issues/182)) ([02aa068](https://github.com/webpack/schema-utils/commit/02aa068df80d99cc576a5ed385f947eb5204c5db)) | ||
## [3.2.0](https://github.com/webpack/schema-utils/compare/v3.1.2...v3.2.0) (2023-06-07) | ||
@@ -7,0 +19,0 @@ |
import { validate } from "./validate"; | ||
import { ValidationError } from "./validate"; | ||
export { validate, ValidationError }; | ||
import { enableValidation } from "./validate"; | ||
import { disableValidation } from "./validate"; | ||
import { needValidate } from "./validate"; | ||
export { | ||
validate, | ||
ValidationError, | ||
enableValidation, | ||
disableValidation, | ||
needValidate, | ||
}; |
@@ -5,2 +5,3 @@ export type JSONSchema4 = import("json-schema").JSONSchema4; | ||
export type ErrorObject = import("ajv").ErrorObject; | ||
export type ValidateFunction = import("ajv").ValidateFunction; | ||
export type Extend = { | ||
@@ -27,30 +28,3 @@ formatMinimum?: number | undefined; | ||
}; | ||
/** @typedef {import("json-schema").JSONSchema4} JSONSchema4 */ | ||
/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */ | ||
/** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */ | ||
/** @typedef {import("ajv").ErrorObject} ErrorObject */ | ||
/** | ||
* @typedef {Object} Extend | ||
* @property {number=} formatMinimum | ||
* @property {number=} formatMaximum | ||
* @property {boolean=} formatExclusiveMinimum | ||
* @property {boolean=} formatExclusiveMaximum | ||
* @property {string=} link | ||
* @property {boolean=} undefinedAsNull | ||
*/ | ||
/** @typedef {(JSONSchema4 | JSONSchema6 | JSONSchema7) & Extend} Schema */ | ||
/** @typedef {ErrorObject & { children?: Array<ErrorObject>}} SchemaUtilErrorObject */ | ||
/** | ||
* @callback PostFormatter | ||
* @param {string} formattedError | ||
* @param {SchemaUtilErrorObject} error | ||
* @returns {string} | ||
*/ | ||
/** | ||
* @typedef {Object} ValidationErrorConfiguration | ||
* @property {string=} name | ||
* @property {string=} baseDataPath | ||
* @property {PostFormatter=} postFormatter | ||
*/ | ||
/** | ||
* @param {Schema} schema | ||
@@ -66,3 +40,6 @@ * @param {Array<object> | object} options | ||
): void; | ||
export function enableValidation(): void; | ||
export function disableValidation(): void; | ||
export function needValidate(): boolean; | ||
import ValidationError from "./ValidationError"; | ||
export { ValidationError }; |
@@ -5,3 +5,6 @@ "use strict"; | ||
validate, | ||
ValidationError | ||
ValidationError, | ||
enableValidation, | ||
disableValidation, | ||
needValidate | ||
} = require("./validate"); | ||
@@ -11,3 +14,6 @@ | ||
validate, | ||
ValidationError | ||
ValidationError, | ||
enableValidation, | ||
disableValidation, | ||
needValidate | ||
}; |
@@ -7,2 +7,5 @@ "use strict"; | ||
exports.validate = validate; | ||
exports.enableValidation = enableValidation; | ||
exports.disableValidation = disableValidation; | ||
exports.needValidate = needValidate; | ||
Object.defineProperty(exports, "ValidationError", { | ||
@@ -77,2 +80,4 @@ enumerable: true, | ||
/** @typedef {import("ajv").ValidateFunction} ValidateFunction */ | ||
/** | ||
@@ -107,2 +112,60 @@ * @typedef {Object} Extend | ||
/** | ||
* @param {SchemaUtilErrorObject} error | ||
* @param {number} idx | ||
* @returns {SchemaUtilErrorObject} | ||
*/ | ||
function applyPrefix(error, idx) { | ||
// eslint-disable-next-line no-param-reassign | ||
error.dataPath = `[${idx}]${error.dataPath}`; | ||
if (error.children) { | ||
error.children.forEach(err => applyPrefix(err, idx)); | ||
} | ||
return error; | ||
} | ||
let skipValidation = false; // We use `process.env.SKIP_VALIDATION` because you can have multiple `schema-utils` with different version, | ||
// so we want to disable it globally, `process.env` doesn't supported by browsers, so we have the local `skipValidation` variables | ||
// Enable validation | ||
function enableValidation() { | ||
skipValidation = false; // Disable validation for any versions | ||
if (process && process.env) { | ||
process.env.SKIP_VALIDATION = "n"; | ||
} | ||
} // Disable validation | ||
function disableValidation() { | ||
skipValidation = true; | ||
if (process && process.env) { | ||
process.env.SKIP_VALIDATION = "y"; | ||
} | ||
} // Check if we need to confirm | ||
function needValidate() { | ||
if (skipValidation) { | ||
return false; | ||
} | ||
if (process && process.env && process.env.SKIP_VALIDATION) { | ||
const value = process.env.SKIP_VALIDATION.trim(); | ||
if (/^(?:y|yes|true|1|on)$/i.test(value)) { | ||
return false; | ||
} | ||
if (/^(?:n|no|false|0|off)$/i.test(value)) { | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* @param {Schema} schema | ||
@@ -114,27 +177,14 @@ * @param {Array<object> | object} options | ||
function validate(schema, options, configuration) { | ||
if (!needValidate()) { | ||
return; | ||
} | ||
let errors = []; | ||
if (Array.isArray(options)) { | ||
errors = Array.from(options, nestedOptions => validateObject(schema, nestedOptions)); | ||
errors.forEach((list, idx) => { | ||
const applyPrefix = | ||
/** | ||
* @param {SchemaUtilErrorObject} error | ||
*/ | ||
error => { | ||
// eslint-disable-next-line no-param-reassign | ||
error.dataPath = `[${idx}]${error.dataPath}`; | ||
if (error.children) { | ||
error.children.forEach(applyPrefix); | ||
} | ||
}; | ||
list.forEach(applyPrefix); | ||
}); | ||
errors = errors.reduce((arr, items) => { | ||
arr.push(...items); | ||
return arr; | ||
}, []); | ||
for (let i = 0; i <= options.length - 1; i++) { | ||
errors.push(...validateObject(schema, options[i]).map(err => applyPrefix(err, i))); | ||
} | ||
} else { | ||
@@ -148,2 +198,6 @@ errors = validateObject(schema, options); | ||
} | ||
/** @typedef {WeakMap<Schema, ValidateFunction>} */ | ||
const schemaCache = new WeakMap(); | ||
/** | ||
@@ -155,5 +209,10 @@ * @param {Schema} schema | ||
function validateObject(schema, options) { | ||
let compiledSchema = schemaCache.get(schema); | ||
function validateObject(schema, options) { | ||
const compiledSchema = getAjv().compile(schema); | ||
if (!compiledSchema) { | ||
compiledSchema = getAjv().compile(schema); | ||
schemaCache.set(schema, compiledSchema); | ||
} | ||
const valid = compiledSchema(options); | ||
@@ -160,0 +219,0 @@ if (valid) return []; |
{ | ||
"name": "schema-utils", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "webpack Validation Utils", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
84831
1870
7