-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Spike: Constrain the OpenAPI schema emitted for a model [DO NOT MERGE] #2646
Conversation
includeRelations?: boolean; | ||
|
||
/// List of properties to exclude from the schema | ||
exclude?: (keyof T)[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type construct allows TypeScript to verify that exclude
is using only existing model properties.
Consider the following incorrect usage:
{
schema: getModelSchemaRef(TodoList, {exclude: ['id', 'foobar']}),
}
The compiler prints the following error:
Argument of type '{ exclude: ("id" | "foobar")[]; }' is not assignable to parameter of type 'JsonSchemaOptions<TodoList>'.
Types of property 'exclude' are incompatible.
Type '("id" | "foobar")[]' is not assignable to type '("id" | "title" | "color" | "todos" | "image" | "getId" | "getIdObject" | "toJSON" | "toObject")[]'.
Type '"id" | "foobar"' is not assignable to type '"id" | "title" | "color" | "todos" | "image" | "getId" | "getIdObject" | "toJSON" | "toObject"'.
Type '"foobar"' is not assignable to type '"id" | "title" | "color" | "todos" | "image" | "getId" | "getIdObject" | "toJSON" | "toObject"'.
}, | ||
}, | ||
}) | ||
obj: Pick<TodoList, Exclude<keyof TodoList, 'id'>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe define a type alias:
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to define a type alias.
We must carefully choose a name that will be different enough from Exclude
. Few options that come to my mind:
ExcludeProperties<T, K>
ExcludeKeys<T, K>
OmitProperties<T, K>
OmitKeys<T, K>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The design that customizes the schema by options LGTM 👍
887ae84
to
f7db169
Compare
Created the following stories:
Closing this spike as done. |
This pull request offers a PoC implementation demonstrating how to modify the schema generated from a LB4 model depending on configurable options. In particular, the following two use cases are covered:
The first commit imports code from #2592 and should be ignored.
The second commit contains PoC and SPIKE notes with a high-level overview.
Related stories:
Partial
#1179Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated