Skip to content

Commit

Permalink
perf: initial start time (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 15, 2023
1 parent e90a2fc commit 052b275
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 22 deletions.
26 changes: 26 additions & 0 deletions declarations/validate.d.ts
Expand Up @@ -22,6 +22,32 @@ export type ValidationErrorConfiguration = {
baseDataPath?: string | undefined;
postFormatter?: PostFormatter | 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 {string=} formatMinimum
* @property {string=} formatMaximum
* @property {string=} formatExclusiveMinimum
* @property {string=} 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
* @param {Array<object> | object} options
Expand Down
74 changes: 52 additions & 22 deletions src/validate.js
Expand Up @@ -2,10 +2,57 @@ import addAbsolutePathKeyword from "./keywords/absolutePath";

import ValidationError from "./ValidationError";

// Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110).
const Ajv = require("ajv").default;
const ajvKeywords = require("ajv-keywords").default;
const addFormats = require("ajv-formats").default;
/**
* @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(() => {
// Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110).
// eslint-disable-next-line global-require
const Ajv = require("ajv").default;
// eslint-disable-next-line global-require
const ajvKeywords = require("ajv-keywords").default;
// eslint-disable-next-line global-require
const addFormats = require("ajv-formats").default;

/**
* @type {Ajv}
*/
const ajv = new Ajv({
strict: false,
allErrors: true,
verbose: true,
$data: true,
});

ajvKeywords(ajv, ["instanceof", "patternRequired"]);
addFormats(ajv, { keywords: true });
// Custom keywords
addAbsolutePathKeyword(ajv);

return ajv;
});

/** @typedef {import("json-schema").JSONSchema4} JSONSchema4 */
/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
Expand Down Expand Up @@ -39,23 +86,6 @@ const addFormats = require("ajv-formats").default;
* @property {PostFormatter=} postFormatter
*/

/**
* @type {Ajv}
*/
const ajv = new Ajv({
strict: false,
allErrors: true,
verbose: true,
$data: true,
});

ajvKeywords(ajv, ["instanceof", "patternRequired"]);

addFormats(ajv, { keywords: true });

// Custom keywords
addAbsolutePathKeyword(ajv);

/**
* @param {Schema} schema
* @param {Array<object> | object} options
Expand Down Expand Up @@ -106,7 +136,7 @@ function validate(schema, options, configuration) {
* @returns {Array<SchemaUtilErrorObject>}
*/
function validateObject(schema, options) {
const compiledSchema = ajv.compile(schema);
const compiledSchema = getAjv().compile(schema);
const valid = compiledSchema(options);

if (valid) return [];
Expand Down

0 comments on commit 052b275

Please sign in to comment.