-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use pre-compiled ajv validators at runtime (#141)
this builds on the speed up from memorizing the validator in #140, further improving the speed of the zod spec file from `~1s` -> `~200ms` (a lot better than the original `~6s`!) (though note, it's a bit of a wash for time in CI given it re-compiles there) there's no longer any need to memorize the validator, since the require cache effectively does this for us now. the compiled validation function is pretty large, but committing it will keep me honest and prove reproducibility thanks to the CI check for uncommitted changes after running the build / tests.
- Loading branch information
Showing
13 changed files
with
19,275 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Pre-compiled ajv validators generated by ./scripts/generate-ajv-validator.js | ||
# Any uncommitted changes will be rejected by CI using ./scripts/assert-clean-working-directory.sh | ||
packages/openapi-code-generater/src/core/schemas/openapi-3.0-specification-validator.js binary | ||
packages/openapi-code-generater/src/core/schemas/openapi-3.1-specification-validator.js binary |
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
56 changes: 56 additions & 0 deletions
56
packages/openapi-code-generator/src/core/openapi-validator.spec.ts
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {describe, expect} from "@jest/globals" | ||
import {OpenapiValidator} from "./openapi-validator" | ||
|
||
describe("core/openapi-validator", () => { | ||
describe("openapi 3.0", () => { | ||
it("should accept a valid specification", async () => { | ||
const validator = await OpenapiValidator.create() | ||
await expect( | ||
validator.validate( | ||
"valid-spec.yaml", | ||
{ | ||
openapi: "3.0.0", | ||
info: { | ||
title: "Valid Specification", | ||
version: "1.0.0", | ||
}, | ||
paths: { | ||
"/something": { | ||
get: { | ||
responses: {default: {description: "whatever"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
true, | ||
), | ||
).resolves.toBeUndefined() | ||
}) | ||
|
||
it("should reject an invalid specification", async () => { | ||
const validator = await OpenapiValidator.create() | ||
await expect( | ||
validator.validate( | ||
"invalid-spec.yaml", | ||
{ | ||
openapi: "3.0.0", | ||
info: { | ||
title: "Valid Specification", | ||
version: "1.0.0", | ||
}, | ||
paths: { | ||
"/something": { | ||
get: { | ||
responses: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
true, | ||
), | ||
).rejects.toThrow( | ||
"Validation failed: -> must NOT have fewer than 1 properties at path '/paths/~1something/get/responses'", | ||
) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.