zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[Feature Request] Allow customizing required message

Open sarioglu opened this issue 1 year ago • 3 comments

Firstly, I appreciate the work you put on this library to make prisma way more easier to work with on frontend side. I'm currently trying to refactor an existing app using zenstack and I encountered following issue that makes it harder to depend solely on zenstack generated schemas.

Is your feature request related to a problem? Please describe. My app uses zod schemas to validate forms and I'm replacing them using schemas create by zenstack. It's very helpful to put validation alongside of database schemas. However, I cannot customize error message for required fields.

For example, one of my forms have some string and enum fields. I am able to use @length(min: 1, message: "Some custom error message") validation function instead of showing required error. But there's no option to make it work for enums. Therefore when I validate this form using zenstack-generated schema, it only says Required.

Describe the solution you'd like I suggest to add @required or @required_message function to add a custom error message for any zod field type. I'd prefer @required_message because adding @required to a field to customize error message may imply that other non-nullable fields are not actually required. Also zod itself uses required_message property so aligning with it makes more sense.

Describe alternatives you've considered I can use @lenght for string fields but there's no alternative for enums. I tried to customize zod enums generated by zenstack but they are not exported individually.

Additional context Let me know if you need a sandbox, etc. to make my request clearer.

sarioglu avatar Feb 16 '24 10:02 sarioglu

Hi @sarioglu, thanks for filing the issue. Yes, I see there's currently no easy way to provide a custom message for missing fields. I think a possible workaround is to use the model-level validation. Like:

model Model {
  ...
  requiredField String

  @@validate(requiredField != null, "my message here")
}

Yes, a @requiredMessage attribute would be ideal (I'm changing it to camel-case for better consistency).

ymc9 avatar Feb 17 '24 07:02 ymc9

Hi @ymc9, thank you for your quick response. I tried to use @@validate but I hit against following shortcomings:

  1. If I make a field required and validate using @@validate field-level validation takes precedence and therefore error message still becomes Required. To overcome this, I need to mark field as nullable and rely solely on @@validate. I don't think this is a good practice in terms of data consistency as database won't know about it.
  2. When I use @@validate, error message is not associated with that field. So I cannot show it below the related input field. zod's .refine function has an option to set path but it's not available through .zmodel language.

If you think that implementing @requiredMessage is easy enough to be done by a newcomer and help me to find where to look at, I can try to create a PR for it.

sarioglu avatar Feb 18 '24 10:02 sarioglu

Hi @ymc9, thank you for your quick response. I tried to use @@validate but I hit against following shortcomings:

  1. If I make a field required and validate using @@validate field-level validation takes precedence and therefore error message still becomes Required. To overcome this, I need to mark field as nullable and rely solely on @@validate. I don't think this is a good practice in terms of data consistency as database won't know about it.
  2. When I use @@validate, error message is not associated with that field. So I cannot show it below the related input field. zod's .refine function has an option to set path but it's not available through .zmodel language.

If you think that implementing @requiredMessage is easy enough to be done by a newcomer and help me to find where to look at, I can try to create a PR for it.

Got it. Both are valid points! Yes, a PR is highly appreciated. Thanks!

ymc9 avatar Feb 23 '24 17:02 ymc9