Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache schemas by explicit ID in AJV8Validator.isValid #3721

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
should change the heading of the (upcoming) version to include a major version bump.

-->
# 5.8.2

## @rjsf/validator-ajv8

- Explicitly cache schemas by their hash when checking data is valid to avoid multiple compilations for schemas without IDs leading to poor performance [#3721](https://github.com/rjsf-team/react-jsonschema-form/pull/3721)

# 5.8.1

Expand Down
13 changes: 9 additions & 4 deletions packages/validator-ajv8/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ValidationData,
ValidatorType,
withIdRefPrefix,
hashForSchema,
} from '@rjsf/utils';

import { CustomValidatorOptionsType, Localizer } from './types';
Expand Down Expand Up @@ -137,12 +138,16 @@ export default class AJV8Validator<T = any, S extends StrictRJSFSchema = RJSFSch
this.ajv.addSchema(rootSchema, rootSchemaId);
}
const schemaWithIdRefPrefix = withIdRefPrefix<S>(schema) as S;
const schemaId = schemaWithIdRefPrefix[ID_KEY] ?? hashForSchema(schemaWithIdRefPrefix);
let compiledValidator: ValidateFunction | undefined;
if (schemaWithIdRefPrefix[ID_KEY]) {
compiledValidator = this.ajv.getSchema(schemaWithIdRefPrefix[ID_KEY]);
}
compiledValidator = this.ajv.getSchema(schemaId);
if (compiledValidator === undefined) {
compiledValidator = this.ajv.compile(schemaWithIdRefPrefix);
// Add schema by an explicit ID so it can be fetched later
// Fall back to using compile if necessary
// https://ajv.js.org/guide/managing-schemas.html#pre-adding-all-schemas-vs-adding-on-demand
compiledValidator =
this.ajv.addSchema(schemaWithIdRefPrefix, schemaId).getSchema(schemaId) ||
this.ajv.compile(schemaWithIdRefPrefix);
}
const result = compiledValidator(formData);
return result as boolean;
Expand Down
67 changes: 60 additions & 7 deletions packages/validator-ajv8/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,58 @@ describe('AJV8Validator', () => {
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const compileSpy = jest.spyOn(validator.ajv, 'compile');
const addSchemaSpy = jest.spyOn(validator.ajv, 'addSchema');
addSchemaSpy.mockClear();

// Call isValid twice with the same schema
validator.isValid(schema, formData, rootSchema);
validator.isValid(schema, formData, rootSchema);

// Root schema is added twice
expect(addSchemaSpy).toHaveBeenCalledTimes(3);
expect(addSchemaSpy).toHaveBeenNthCalledWith(1, expect.objectContaining(rootSchema), rootSchema.$id);
expect(addSchemaSpy).toHaveBeenNthCalledWith(2, expect.objectContaining(schema), schema.$id);
expect(addSchemaSpy).toHaveBeenLastCalledWith(expect.objectContaining(rootSchema), rootSchema.$id);
});
it('should fallback to using compile', () => {
const schema: RJSFSchema = {
$id: 'schema-id-2',
};

const rootSchema: RJSFSchema = {
$id: 'root-schema-id',
type: 'object',
properties: {
name: {
type: 'string',
},
},
};
const formData = {
name: 'John Doe',
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const ajvInstance = validator.ajv;
const originalGetSchema = ajvInstance.getSchema.bind(ajvInstance);
const getSchemaSpy = jest.spyOn(ajvInstance, 'getSchema');
getSchemaSpy.mockClear();
getSchemaSpy.mockImplementation((schemaId) => {
if (schemaId === schema.$id) {
return undefined;
}

return originalGetSchema(schemaId);
});

const compileSpy = jest.spyOn(ajvInstance, 'compile');
compileSpy.mockClear();

// Call isValid twice with the same schema
validator.isValid(schema, formData, rootSchema);
validator.isValid(schema, formData, rootSchema);

getSchemaSpy.mockRestore();
expect(compileSpy).toHaveBeenCalledTimes(1);
});
});
Expand Down Expand Up @@ -546,14 +591,18 @@ describe('AJV8Validator', () => {
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const compileSpy = jest.spyOn(validator.ajv, 'compile');
compileSpy.mockClear();
const addSchemaSpy = jest.spyOn(validator.ajv, 'addSchema');
addSchemaSpy.mockClear();

// Call isValid twice with the same schema
validator.isValid(schema, formData, rootSchema);
validator.isValid(schema, formData, rootSchema);

expect(compileSpy).toHaveBeenCalledTimes(1);
// Root schema is added twice
expect(addSchemaSpy).toHaveBeenCalledTimes(3);
expect(addSchemaSpy).toHaveBeenNthCalledWith(1, expect.objectContaining(rootSchema), rootSchema.$id);
expect(addSchemaSpy).toHaveBeenNthCalledWith(2, expect.objectContaining(schema), schema.$id);
expect(addSchemaSpy).toHaveBeenLastCalledWith(expect.objectContaining(rootSchema), rootSchema.$id);
});
});
describe('validator.toErrorList()', () => {
Expand Down Expand Up @@ -1000,14 +1049,18 @@ describe('AJV8Validator', () => {
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const compileSpy = jest.spyOn(validator.ajv, 'compile');
compileSpy.mockClear();
const addSchemaSpy = jest.spyOn(validator.ajv, 'addSchema');
addSchemaSpy.mockClear();

// Call isValid twice with the same schema
validator.isValid(schema, formData, rootSchema);
validator.isValid(schema, formData, rootSchema);

expect(compileSpy).toHaveBeenCalledTimes(1);
// Root schema is added twice
expect(addSchemaSpy).toHaveBeenCalledTimes(3);
expect(addSchemaSpy).toHaveBeenNthCalledWith(1, expect.objectContaining(rootSchema), rootSchema.$id);
expect(addSchemaSpy).toHaveBeenNthCalledWith(2, expect.objectContaining(schema), schema.$id);
expect(addSchemaSpy).toHaveBeenLastCalledWith(expect.objectContaining(rootSchema), rootSchema.$id);
});
});
describe('validator.toErrorList()', () => {
Expand Down