zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[Feature Request] Allow for Prisma enums in validation rules

Open nwidynski opened this issue 10 months ago • 6 comments

Is your feature request related to a problem? Please describe. While Zenstack is already offering great validation capabilities, it would be nice if it were possible to use Prisma enums inside a validate block, for example with an in operation. This would allow for an always-in-sync validation instead of relying on manual updates in both codebase and validation rules.

One might ask why not set the field as the enum? The answer to that being that working with primitives is easier to migrate and also better supported across third party vendors, like Hasura GraphQL.

Describe the solution you'd like Allow for Prisma enums to be referenced in validations rules, especially with the in operator.

Describe alternatives you've considered

Additional context

nwidynski avatar Apr 02 '24 19:04 nwidynski

Hey @nwidynski , thanks for filing this. Do you mind providing a ZModel snippet showing how you want to use it? Thanks!

ymc9 avatar Apr 02 '24 20:04 ymc9

@ymc9 This would be a short example with some options for the validate API.

enum PostStatus {
  DRAFT
  ACTIVE
  CANCELLED
}

model Post {
  id String @id
  status String

  @@validate(status in PostStatus)
  @@validate(status == PostStatus.ACTIVE)
}

nwidynski avatar Apr 06 '24 18:04 nwidynski

Got it. Yes, string and enum can be compatible for comparison.

ymc9 avatar Apr 09 '24 06:04 ymc9

We‘re especially looking for the in operator which would probably translate to:

Object.values(PostStatus).includes(value)

nwidynski avatar Apr 09 '24 08:04 nwidynski

I actually don't get why this is necessary. Making status of type PostStatus instead of String would already validate the type natively in prisma. No need for in operator in zenstack in this case.

Eliav2 avatar Apr 25 '24 23:04 Eliav2

@Eliav2 It's mentioned at the bottom of the issue description. Also both the @@validate and the in operator exist already, just without compatibility for enum fields. No new operator would be introduced into the Zenstack language.

nwidynski avatar Apr 28 '24 13:04 nwidynski