schema-utils
Advanced tools
Comparing version 4.1.0 to 4.2.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, | ||
}; |
@@ -26,30 +26,3 @@ export type JSONSchema4 = import("json-schema").JSONSchema4; | ||
}; | ||
/** @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 {string=} formatMinimum | ||
* @property {string=} formatMaximum | ||
* @property {string=} formatExclusiveMinimum | ||
* @property {string=} 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 | ||
@@ -65,3 +38,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,7 +5,13 @@ "use strict"; | ||
validate, | ||
ValidationError | ||
ValidationError, | ||
enableValidation, | ||
disableValidation, | ||
needValidate | ||
} = require("./validate"); | ||
module.exports = { | ||
validate, | ||
ValidationError | ||
ValidationError, | ||
enableValidation, | ||
disableValidation, | ||
needValidate | ||
}; |
@@ -12,30 +12,10 @@ "use strict"; | ||
}); | ||
exports.disableValidation = disableValidation; | ||
exports.enableValidation = enableValidation; | ||
exports.needValidate = needValidate; | ||
exports.validate = validate; | ||
var _absolutePath = _interopRequireDefault(require("./keywords/absolutePath")); | ||
var _undefinedAsNull = _interopRequireDefault(require("./keywords/undefinedAsNull")); | ||
var _ValidationError = _interopRequireDefault(require("./ValidationError")); | ||
var _memorize = _interopRequireDefault(require("./util/memorize")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @template T | ||
* @param fn {(function(): any) | undefined} | ||
* @returns {function(): T} | ||
*/ | ||
const memoize = fn => { | ||
let cache = false; | ||
/** @type {T} */ | ||
let result; | ||
return () => { | ||
if (cache) { | ||
return result; | ||
} | ||
result = /** @type {function(): any} */fn(); | ||
cache = true; | ||
// Allow to clean up memory for fn | ||
// and all dependent resources | ||
// eslint-disable-next-line no-undefined, no-param-reassign | ||
fn = undefined; | ||
return result; | ||
}; | ||
}; | ||
const getAjv = memoize(() => { | ||
const getAjv = (0, _memorize.default)(() => { | ||
// Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110). | ||
@@ -64,4 +44,9 @@ // eslint-disable-next-line global-require | ||
// Custom keywords | ||
(0, _absolutePath.default)(ajv); | ||
(0, _undefinedAsNull.default)(ajv); | ||
// eslint-disable-next-line global-require | ||
const addAbsolutePathKeyword = require("./keywords/absolutePath").default; | ||
addAbsolutePathKeyword(ajv); | ||
const addUndefinedAsNullKeyword = | ||
// eslint-disable-next-line global-require | ||
require("./keywords/undefinedAsNull").default; | ||
addUndefinedAsNullKeyword(ajv); | ||
return ajv; | ||
@@ -87,3 +72,3 @@ }); | ||
/** @typedef {ErrorObject & { children?: Array<ErrorObject>}} SchemaUtilErrorObject */ | ||
/** @typedef {ErrorObject & { children?: Array<ErrorObject> }} SchemaUtilErrorObject */ | ||
@@ -105,2 +90,55 @@ /** | ||
/** | ||
* @param {SchemaUtilErrorObject} error | ||
* @param {number} idx | ||
* @returns {SchemaUtilErrorObject} | ||
*/ | ||
function applyPrefix(error, idx) { | ||
// eslint-disable-next-line no-param-reassign | ||
error.instancePath = `[${idx}]${error.instancePath}`; | ||
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 | ||
@@ -112,23 +150,10 @@ * @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.instancePath = `[${idx}]${error.instancePath}`; | ||
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 +173,3 @@ errors = validateObject(schema, options); | ||
function validateObject(schema, options) { | ||
// Not need to cache, because `ajv@8` has built-in cache | ||
const compiledSchema = getAjv().compile(schema); | ||
@@ -150,0 +176,0 @@ const valid = compiledSchema(options); |
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.default = void 0; | ||
const { | ||
stringHints, | ||
numberHints | ||
} = require("./util/hints"); | ||
var _memorize = _interopRequireDefault(require("./util/memorize")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */ | ||
@@ -20,3 +17,2 @@ /** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */ | ||
/** @typedef {import("./validate").SchemaUtilErrorObject} SchemaUtilErrorObject */ | ||
/** @enum {number} */ | ||
@@ -319,2 +315,5 @@ const SPECIFICITY = { | ||
} | ||
const getUtilHints = (0, _memorize.default)(() => | ||
// eslint-disable-next-line global-require | ||
require("./util/hints")); | ||
@@ -328,5 +327,7 @@ /** | ||
if (likeNumber(schema) || likeInteger(schema)) { | ||
return numberHints(schema, logic); | ||
const util = getUtilHints(); | ||
return util.numberHints(schema, logic); | ||
} else if (likeString(schema)) { | ||
return stringHints(schema, logic); | ||
const util = getUtilHints(); | ||
return util.stringHints(schema, logic); | ||
} | ||
@@ -333,0 +334,0 @@ return []; |
{ | ||
"name": "schema-utils", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "webpack Validation Utils", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -266,2 +266,32 @@ <div align="center"> | ||
### Allow to disable and enable validation (the `validate` function do nothing) | ||
This can be useful when you don't want to do validation for `production` builds. | ||
```js | ||
import { disableValidation, enableValidation, validate } from "schema-utils"; | ||
// Disable validation | ||
disableValidation(); | ||
// Do nothing | ||
validate(schema, options); | ||
// Enable validation | ||
enableValidation(); | ||
// Will throw an error if schema is not valid | ||
validate(schema, options); | ||
// Allow to undestand do you need validation or not | ||
const need = needValidate(); | ||
console.log(need); | ||
``` | ||
Also you can enable/disable validation using the `process.env.SKIP_VALIDATION` env variable. | ||
Supported values (case insensitive): | ||
- `yes`/`y`/`true`/`1`/`on` | ||
- `no`/`n`/`false`/`0`/`off` | ||
## Contributing | ||
@@ -268,0 +298,0 @@ |
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
19
1825
318
71643
7