-
-
Notifications
You must be signed in to change notification settings - Fork 532
Throw error if required property is not present #331
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
Comments
Thanks for the suggestion! I think this isn’t really outlined in the README well, but one goal of this project is for it to not be a validator for your schema; we assume you’ve handled that elsewhere. So basically in order to do this, we’d have to add validation to schemas, which could possibly slow down generation times. But that said, if this provides utility, I’m not opposed to adding it. Maybe it could be opt-in? Also is there a particular validator that seems to work better for OpenAPI schemas (personally I’ve dabbled with a few, but I don’t know if any of them would catch a missing reference like this)? |
I get that - I was thinking of it less as validation of the schema and more as throwing on invalid type generation. If you say a property is required, but there's no description of the type, a type can't be generated to match the description. I won't argue if you consider it out of scope, but I think it definitely walks a line. I'm certainly meaning to come at it from the perspective of generating valid types rather than making sure swagger is correct. |
Oh also - I wouldn't implement by validating the entire schema. I would just have a check in place for this particular step (when |
That makes perfect sense; I don’t see why this couldn’t be added easily. For something this minimal, I’m thinking it’s on-by-default, but maybe we could add an opt-out flag ( |
I'm happy in either direction, but I agree with your suggestion. |
Update: it seems that #620 seems to address this by appending the schema, rather than throwing an error. Perhaps this is a good solution to this? Or is it still expected that |
It's certainly better than nothing - it works well when you receive a value of the response because interface UserResponse {
email: unknown
firstName?: string
lastName: string
}
// Error on the client - Type 'unknown' is not assignable to type 'string'
function getEmail(res: UserResponse): string {
return res.email
}
// No error on the server
ctx.body: UserResponse = {
email: 5,
lastName: ''
} To me, it comes down to the use-case - it could be considered a feature in development to create your response properties before you really settle on shape of the values. But if you are just setting up an API, it doesn't really offer much. |
Also, I think this presents a pretty good argument for an |
Update on this: a lot of improvements are landing in v6 (#968), and these behaviors have been improved. But a note has been added to the README that a validator is a requirement to use this library, and it should be run before this library is run for best results. |
We had a response typecheck that should not have because we made a typo in the properties list, as below:
We were expecting the response typechecking to fail if organization wasn't present, but instead it was only looking for an optional
account
value. It would be nice to have this throw an error on build - something like "Required property "organization" is not defined in the "properties" at [path.to.this.schema]".The text was updated successfully, but these errors were encountered: