Giter VIP home page Giter VIP logo

Comments (6)

CarterGrimmeisen avatar CarterGrimmeisen commented on May 27, 2024 10

No, you are understanding it correctly. I definitely agree this needs to be incorporated in some way. I'm currently brainstorming a more all-encompassing rework that should handle this, #93, and #90. So I'll update this issue when that becomes available! Thanks!

from zod-prisma.

davguij avatar davguij commented on May 27, 2024 3

Hey @CarterGrimmeisen has there been any progress on this? Is this still on your radar? It would be an improvement I could surely use :)

from zod-prisma.

testerez avatar testerez commented on May 27, 2024 2

Same issue here. I would be fine with a /// @zod.ignore special comment to remove any specific field from the generated schema. Also, a global ignore configuration would be nice to ignore fields for the whole schema. Something like:

generator zod {
  provider      = "zod-prisma"
  ignore = ["createdAt", "updatedAt"]
}

Would that make sense?

from zod-prisma.

testerez avatar testerez commented on May 27, 2024 2

In case anyone is interested I added a feature on my fork to omit any field: https://github.com/testerez/zod-prisma#omit

You can install my fork with: yarn add zod-prisma@testerez/zod-prisma

I'd be happy to open a PR here but it looks like this project is not maintained anymore.

from zod-prisma.

abenhamdine avatar abenhamdine commented on May 27, 2024

It doesn't make sense to me that a field could not be optional and have a default value.

Fields that have a default value (denoted by @default) should be optional. This could be solved by making the field itself optional with ?. However, I would like to disallow explicitly setting null, but allow implicitly accepting the default value by omitting the field.

Why would you want to disallow null and allow omitting the property, given than the final result will be the same (applying the default value in db) ? I fail to understand.

from zod-prisma.

MartinMuzatko avatar MartinMuzatko commented on May 27, 2024

I think you can forget about what I said about null. If a field has a default value (but is not optional by nature) it should be, as convention, use the default value if omitted.

I can think of at least two scenarios where I want to use a validation schema in a basic CRUD application.

  1. Create - all required fields have to be required
  2. Update - no fields are required

Case 1 - Create
How should fields behave that are required but have a default value? Are they really required? Should I be able to configure that?
Should the validator swallow a null?
Example:

model Notification {
  id        Int      @id @default(autoincrement())
  subject   String
  trigger   String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

createdAt and updatedAt are required fields as defined in the prisma schema, but they have a default value (updatedAt has implicitly a default value, but not denoted by @default)
prisma.notification.create({ subject: 'whatever', }) will also not complain, if you omit these fields.
However, the zod validator does.

If you create a Notification, you wouldn't want to validate for createdAt or updatedAt at best it should be disallowed to be set.

Case 2 - Update
createdAt and updatedAt is swollowed by prisma here anyway. Not allowed to overwrite.
On update, every field should be optional. So validation errors can only happen for provided fields that are of wrong type + other limitations like min/max etc.

from zod-prisma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.