Skip to content

[Feature Request] Allow for Prisma enums in validation rules #1211

Description

@nwidynski

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

Activity

  1. ymc9 commented on Apr 2, 2024

    @ymc9
    Member

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

  2. nwidynski commented on Apr 6, 2024

    @nwidynski
    Author

    @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)
    }
    
  3. ymc9 commented on Apr 9, 2024

    @ymc9
    Member

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

  4. nwidynski commented on Apr 9, 2024

    @nwidynski
    Author

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

    Object.values(PostStatus).includes(value)

  5. Eliav2 commented on Apr 25, 2024

    @Eliav2

    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.

  6. nwidynski commented on Apr 28, 2024

    @nwidynski
    Author

    @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.

  7. sanny-io commented on Aug 17, 2026

    @sanny-io
    Contributor

    2 years later

    The == comparison currently works with enums as long as you don't fully qualify the enum and instead reference the field directly.

    @@validate(status == PostStatus.ACTIVE) // ❌
    @@validate(status == ACTIVE) // ✅

    I've implemented support for the in operator in #2807

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions