diff --git a/backend/lib/validator/api.js b/backend/lib/validator/api.js index 6c738d5097..9dfa0e62d6 100644 --- a/backend/lib/validator/api.js +++ b/backend/lib/validator/api.js @@ -9,6 +9,8 @@ const ajv = new Ajv({ coerceTypes: true, }); +const compiledSchemas = new WeakMap(); + /** * @param {Object} schema * @param {Object} payload @@ -25,7 +27,11 @@ const apiValidator = async (schema, payload /*, description*/) => { } - const validate = ajv.compile(schema); + let validate = compiledSchemas.get(schema); + if (!validate) { + validate = ajv.compile(schema); + compiledSchemas.set(schema, validate); + } const valid = validate(payload); diff --git a/backend/lib/validator/index.js b/backend/lib/validator/index.js index 5f2586fd67..19eb9a3c80 100644 --- a/backend/lib/validator/index.js +++ b/backend/lib/validator/index.js @@ -14,6 +14,8 @@ const ajv = new Ajv({ schemas: [commonDefinitions], }); +const compiledSchemas = new WeakMap(); + /** * * @param {Object} schema @@ -26,7 +28,11 @@ const validator = (schema, payload) => { reject(new errs.InternalValidationError("Payload is falsy")); } else { try { - const validate = ajv.compile(schema); + let validate = compiledSchemas.get(schema); + if (!validate) { + validate = ajv.compile(schema); + compiledSchemas.set(schema, validate); + } const valid = validate(payload); if (valid && !validate.errors) {