valentinpalkovic/prisma-json-schema-generator

Required properties field

Closed this issue · 5 comments

I would like the following option for the Schema generation:

When a field is not nullable in prisma schema, make it required in the JSON schema.
This prevents cases where the JSON schema validates JSON as valid, but prisma will not accept the JSON as valid data because a non-nullable field is required.

Of course, if there is a default option on the prisma schema, it should not be added to the required array.

Hey @nklisch,

Thank you for opening this issue!

There was already a similar issue open: #13 and it has some complications. Maybe you can read through the discussion to consider the outcomes of introducing this!

Especially the workaround mentioned here: #13 (comment) might help :)

@valentinpalkovic I see the other comments on my PR and I'll put in those fixes, but I wanted to respond to the prior discussion in that issue you highlighted.
Those were all valid points, but I think a piece that may have been missing is that by not including "required" you are losing information about the Prisma Schema, which is useful to have at runtime.
Regardless of whether you include the required properties or not, you will be required to write some form of middleware that adjusts the schema based on the route - but it is easier to remove or modify the required list than it is to add an unknown back.
And while the required list may not exactly match what is in the schema, it provides a reasonable starting point that can remove alot of work and effort.

In my case, I only need to remove the required properties array when I am issuing a patch request, as my id's come from my path. In my create requests, I would like to validate that the truly required fields exist.

Anyways, I think the option flag, plus the route by route changes provide the necessary flexibility to make this plug-in useful.

I am considering creating a generic middleware tool that can modify the provided schema in common ways, as currently, I needed to create a middleware to select the specific prisma table schema within the definitions for any given request.

I wanted to give an explicit example of why it is more work to not provide the required field then to remove/modify it when needed.

Imagine migrating the prisma schema to add a new field. This new field is required for create requests, but not patch requests.

In this case, if required was not included, and I wanted the validation before sending it to prisma, I would have to go through every route and add that new field to my required, which could both be time-consuming and error-prone, not to mention likely forgotten.

If the required property is included, then I can simply remove the required for my patch requests, and change nothing for my other requests. This solution is forward compatible regardless of what changes about the prisma schema.

Valid points! Especially, if we make the generation of required fields optional, everyone can decide how to deal with the generated JSON Schema. I have nothing against introducing this configurable feature!

🎉 This issue is fixed in version 3.0.0 🎉

The release is available on: