schema-utils
Advanced tools
Comparing version 3.1.1 to 3.1.2
@@ -25,3 +25,29 @@ 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 {number=} formatMinimum | ||
* @property {number=} formatMaximum | ||
* @property {boolean=} formatExclusiveMinimum | ||
* @property {boolean=} formatExclusiveMaximum | ||
* @property {string=} link | ||
*/ | ||
/** @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 | ||
@@ -28,0 +54,0 @@ * @param {Array<object> | object} options |
@@ -20,6 +20,45 @@ "use strict"; | ||
// Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110). | ||
const Ajv = require("ajv"); | ||
/** | ||
* @template T | ||
* @param fn {(function(): any) | undefined} | ||
* @returns {function(): T} | ||
*/ | ||
const memoize = fn => { | ||
let cache = false; | ||
/** @type {T} */ | ||
const ajvKeywords = require("ajv-keywords"); | ||
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(() => { | ||
// Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110). | ||
const Ajv = require("ajv"); | ||
const ajvKeywords = require("ajv-keywords"); | ||
const ajv = new Ajv({ | ||
allErrors: true, | ||
verbose: true, | ||
$data: true | ||
}); | ||
ajvKeywords(ajv, ["instanceof", "formatMinimum", "formatMaximum", "patternRequired"]); // Custom keywords | ||
(0, _absolutePath.default)(ajv); | ||
return ajv; | ||
}); | ||
/** @typedef {import("json-schema").JSONSchema4} JSONSchema4 */ | ||
@@ -60,11 +99,2 @@ | ||
const ajv = new Ajv({ | ||
allErrors: true, | ||
verbose: true, | ||
$data: true | ||
}); | ||
ajvKeywords(ajv, ["instanceof", "formatMinimum", "formatMaximum", "patternRequired"]); // Custom keywords | ||
(0, _absolutePath.default)(ajv); | ||
/** | ||
@@ -118,3 +148,3 @@ * @param {Schema} schema | ||
function validateObject(schema, options) { | ||
const compiledSchema = ajv.compile(schema); | ||
const compiledSchema = getAjv().compile(schema); | ||
const valid = compiledSchema(options); | ||
@@ -121,0 +151,0 @@ if (valid) return []; |
{ | ||
"name": "schema-utils", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"description": "webpack Validation Utils", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
80291
16
1740