-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@jsii/spec): speed up assembly validation by 20x for large librar…
…ies (#3565)
- Loading branch information
Showing
3 changed files
with
35 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.