chore: apiSpec may be const literal #854
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
馃惛 Problem to Solve
The middleware may be instantiated with a
string
(path-on-disk to schema document) or with an object literal of the schema.In Typescript, it is possible to import
.json
files as modules; however the types of their fields are "loose" (eg; the file['foo', bar']
would be typed asstring[]
In order to get stronger typing on imported schemas, they can also be re-emitted as Typescript variables with an
as const
declaration.In this case, the
json
const is typed as its literal valuereadonly ['foo', 'bar']
.This is great, and can be really helpful both when developing, and at run-time.
However, the express-openapi-validator middleware is unhappy with such
as const
schemas, since theOpenAPIV3.Document
type has many mutable fields.馃憫 Proposed Solution
Through the type system, promise that the
express-openapi-validator
won't mangle youras const
schema.The middleware is already gentle with the schema, so this is a type-only change.
The change was motivated by the discovery that passing the schema object to other utilities (eg; openapi-typescript) may cause it to be mutated.
The implementation of
DeepImmutable
is lifted directly from this comment on the TypeScript repo.