Skip to content
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

WhereUniqueInputSchema validation behaviour #68

Closed
binary-birthday opened this issue Feb 3, 2023 · 6 comments
Closed

WhereUniqueInputSchema validation behaviour #68

binary-birthday opened this issue Feb 3, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request todo Needs to be implemented

Comments

@binary-birthday
Copy link

binary-birthday commented Feb 3, 2023

I have a User model with two unique fields, id and phoneNumber.

When validating the input using the UserWhereUniqueInputSchema, validation succeeds in the scenario where:

phoneNumber = undefined

UserWhereUniqueInputSchema.parse({
      phoneNumber,
});

I would expect that validating using the WhereUniqueInputSchema should only succeed if at least one of the required unique fields is a valid input.

@binary-birthday binary-birthday changed the title WhereInputSchema validation behaviour WhereUniqueInputSchema validation behaviour Feb 3, 2023
@chrishoermann chrishoermann self-assigned this Feb 3, 2023
@chrishoermann chrishoermann added enhancement New feature or request todo Needs to be implemented labels Feb 3, 2023
@chrishoermann
Copy link
Owner

@binary-birthday Since the generator just takes the information that is present in the prisma dmmf it creates the schemas according to the prisma types. In these types all unique keys are optional.

model User {
  id         String      @id @default(cuid()) 
  email      String      @unique 
  //...rest
}
// Type in prisma index.d.ts

  export type UserWhereUniqueInput = {
    id?: string
    email?: string
  }

// created schema

export const UserWhereUniqueInputSchema: z.ZodType<Prisma.UserWhereUniqueInput> =
  z
    .object({
      id: z.string().optional(),
      email: z.string().optional(),
    })
    .strict();

export default UserWhereUniqueInputSchema;

So I think currenly you need to check if the fields are undefined before hitting the schema. Another way could be to add a custom refine to the Input schema like

// I'm sure this check can be refined a bit :)
UserWhereUniqueInputSchema.refine((data) => {
  return Object.entries(data).some(([_key, value]) => {
    return value !== undefined;
  });
});

This is something that could also be generated automatically by the generator, since all the information should be there, but I would suggest one of the manual options from above until I can look into this in more detail.

@chrishoermann
Copy link
Owner

chrishoermann commented Feb 3, 2023

I couldn't get it off my mind so I implemented it straight away. Should work now in the latest version as you described it

@binary-birthday
Copy link
Author

Oh awesome. I'll check it out now. For a workaround I thinking about using:

UserSchema.pick({ phoneNumber: true}).parse({phoneNumber})

But this will be much better.

@chrishoermann
Copy link
Owner

closing since it's now implemented

@binary-birthday
Copy link
Author

@chrishoermann I've encountered an issue with this feature:

when UserWhereUniqueInputSchema is used in a connectOrCreate block the original behavior is the expected behavior and I think it makes this a bug rather than a feature.

I apologise for not recognising this earlier.

@chrishoermann chrishoermann reopened this Feb 9, 2023
@chrishoermann
Copy link
Owner

Thanks for pointing this out and no problem @binary-birthday. I'll revert back to the previous implementation in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request todo Needs to be implemented
Projects
Development

No branches or pull requests

2 participants