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

Use built-in "validateSchema" instead of custom implementation #400

Open
artem-zakharchenko opened this issue Jan 15, 2020 · 0 comments
Open

Comments

@artem-zakharchenko
Copy link
Contributor

artem-zakharchenko commented Jan 15, 2020

Currently JSON Schema validator class implements its own validateSchema method using AJV under the hood:

/**
* Validates the schema against its version specification.
* @return {boolean}
*/
validateSchema() {
const { jsonSchemaVersion, jsonSchema } = this;
const ajv = new Ajv();
const metaSchema = META_SCHEMA[jsonSchemaVersion];
ajv.addMetaSchema(metaSchema, 'meta');
const isSchemaValid = ajv.validateSchema(jsonSchema);
// Clean up the added meta schema
ajv.removeSchema('meta');
return isSchemaValid;
}

With the same method implemented on the JsonSchemaLegacy class using TV4:

validateSchema() {
const { jsonSchema, jsonMetaSchema } = this;
// In case schema version is unidentified,
// assume JSON Schema Draft V3.
const metaSchema = jsonMetaSchema || META_SCHEMA.draftV3;
tv4.reset();
tv4.addSchema('', metaSchema);
tv4.addSchema(metaSchema.$schema, metaSchema);
const validationResult = tv4.validateResult(jsonSchema, metaSchema);
return validationResult.valid;
}

I believe it's retained due to historical reasons and should be replaced with the build-in validateSchema option of AJV:

const ajv = new Ajv({
// Disable adding JSON Schema Draft 7 meta schema by default.
// Allows to always add a meta schema depending on the schema version.
meta: false,
// No need to validate schema again, already validated
// in "validateSchema()" method.
validateSchema: false,
jsonPointers: true
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant