-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Question - Using generated schemas with a tool like ajv #458
Comments
I think so! We need to find out what else people use these schemas for, but that sounds like a reasonable feature. However, since these schemas may create a pretty deeply nested tree, we would need to think about the impact this change could have on generation performance, object size, etc |
Yes nesting needs to be handled carefully. If you are open to contributions, I can help draft a PR. |
Totally @maulik13, but would love to discuss the approach first. Do you already know how you'd go about implementation? |
Sure! Let me go through the code a bit more and I can create a simple proposal to see how this can be solved. |
I am actually looking at AJV myself to take the OpenAPI / JSON Schema functionality to its optimal level (auto sdk + validation).
You potentially don't need to nest the schemas and explore that complexity of recursive types, AJV can handle looking up the references provided - see here: https://ajv.js.org/guide/combining-schemas.html#combining-schemas-with-ref. It just needs its I have tried this myself and it works well. The issue I encountered is that the For 3.1 adding all the individual schemas to AJV as long as an For 3.0, it might be that the |
Hey @samvaughton don't see your contact on your profile but found you on LinkedIn, would love to chat more about this |
@samvaughton That is a very good suggestion, but unfortunately it will not work in my case. I am using |
If you have any proposals how to fix this, we can plan a solution. I definitely intend to get around to this, just might take a while as there are many other feature requests as you can imagine |
Yes it seem the only way is a custom resolver (on the react forms side) https://medium.com/@vinjenks/react-custom-validation-resolver-with-react-hook-form-7a7c3ec96827 |
In order to find a solution, the main question here is how the schema objects will be used and what version of JSON schema specs are we targetting. For my use case, library would not need any changes when OpenApi.Net library supports OpenApi version 3.1 and $id field is properly populated (which it should) by openapi-ts. Then, I can potentially use a custom resolver with But, I do see a need to resolve |
Since dereferencing schemas would introduce a great deal of complexity, my vote would be for bringing pre-3.1 schemas to 3.1, i.e. JSON Schema 2020-12. While this too can become complex, this complexity would be limited to the codegen itself and not the output. We could also ask people to upgrade to OpenAPI 3.1, though not everyone might be able to do that. Thoughts? |
Sorry for inactivity from my side, due to time constraint I decided not to use the schema with ajv. I am using zod and just using the maxLength and minLength from the schema. But, in general I agree that pre-3.1 schemas can be brought to 3.1 given that it does not break anyone's usecase replying on 3.0 OpenApi version output. I don't know how that can be determined though. One way to solve this could be to provide an option that can be set to upgrade the schema automatically for pre-3.1 OpenApis. |
@maulik13 Are you using anything to generate Zod types/schemas? |
I am generating zod schemas by hand right now. But, would be awesome to generate them :) However, one challenge is that the actual UI schemas have some special needs. Like, mapping lists (e.g. users) to selection boxes, have custom validations on the UI side (to provide more detailed feedback). So for that reason, I have settled in generating them manually. I have been evaluating different libraries for generating zod, but none have landed properly with me yet. But this library is providing me with services and types for calling services and that has been working great so far. And I am excitingly waiting for the React Query integration! |
We would like to do the same thing to validate the schema against the user input. Now we have to define the zod schema. Would be nice if we could use schema.gen.ts with ajv. |
Hi, I am really interested in the schema generation part since it includes the shape and contraints on the fields. My first thought was to plug in AJV together with the schema objects that openapi-ts generates. Which is proving to be a challenge since the schema includes JSON references (e.g.
$ref: "#/components/schemas/CategoryTypeDto"
) from my OpenApi specs.Here is the generated schema JavaScript object by
openapi-ts
which includes JSON references.And here is the snippet from my open API spec under "components" -> "schemas" ->
Should
openapi-ts
resolve these references when creating JavaScript objects? AJV cannot resolve these references from theschemas.gen.ts
file.The text was updated successfully, but these errors were encountered: