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

fix(@jsii/spec): speed up assembly validation by 20x for large libraries #3565

Merged
merged 1 commit into from
May 31, 2022
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
2 changes: 1 addition & 1 deletion packages/@jsii/spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"package": "package-js"
},
"dependencies": {
"jsonschema": "^1.4.1"
"ajv": "^8.11.0"
},
"devDependencies": {
"jsii-build-tools": "^0.0.0",
Expand Down
22 changes: 14 additions & 8 deletions packages/@jsii/spec/src/validate-assembly.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Schema, Validator } from 'jsonschema';
import Ajv from 'ajv';

import { Assembly } from './assembly';

// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
export const schema: Schema = require('../schema/jsii-spec.schema.json');
export const schema = require('../schema/jsii-spec.schema.json');

export function validateAssembly(obj: any): Assembly {
const validator = new Validator();
validator.addSchema(schema); // For definitions
const result = validator.validate(obj, schema, { nestedErrors: true });
if (result.valid) {
return obj;
const ajv = new Ajv();
const validate = ajv.compile(schema);
validate(obj);

if (validate.errors) {
throw new Error(
`Invalid assembly:\n${validate.errors
.map((e) => ` * ${e.message}`)
.join('\n')
.toString()}`,
);
}
throw new Error(`Invalid assembly:\n${result.toString()}`);
return obj;
}
25 changes: 20 additions & 5 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.