Skip to content

Commit

Permalink
fix(@jsii/spec): speed up assembly validation by 20x for large librar…
Browse files Browse the repository at this point in the history
…ies (#3565)
  • Loading branch information
Chriscbr committed May 31, 2022
1 parent d9ba233 commit c40f26c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
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.

0 comments on commit c40f26c

Please sign in to comment.