Merge pull request #13787 from mitchhentgesspotify/mhentges/defer-schema-compilation

Defer schema validation to improve test performance
This commit is contained in:
Patrik Oldsberg
2022-09-21 17:26:07 +02:00
committed by GitHub
2 changed files with 9 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/catalog-model': patch
---
Defer `ajv` compilation of schema validators to improve module-import performance
+4 -1
View File
@@ -22,9 +22,12 @@ import { KindValidator } from './types';
// exported kind validators have the `KindValidator` signature which is
// different. So let's postpone that change until a later time.
export function ajvCompiledJsonSchemaValidator(schema: unknown): KindValidator {
const validator = entityKindSchemaValidator(schema);
let validator: undefined | ((data: unknown) => any);
return {
async check(data) {
if (!validator) {
validator = entityKindSchemaValidator(schema);
}
return validator(data) === data;
},
};