ash icon indicating copy to clipboard operation
ash copied to clipboard

Add in required validation, replicating Ecto's validate_required functionality

Open allenwyma opened this issue 11 months ago • 2 comments

Is your feature request related to a problem? Please describe. Default validation of attributes marked with allow_nil?: false, will separately mark the error as "required", which is quite clear for users when using a form and not filling in a value.

The closest we have is present/2, which has 2 issues:

  • default error message is it needs to be "present", which if directly displayed on the form, can be a bit unclear to the user of what they need to do. this requires us to override the default error message
  • If you need to mark more than 1 field as required, then you need to separately use present/2 for each attribute. If you pass in a list, then each attribute, on a form, will be marked as invalid if only 1 doesn't have a value.

Describe the solution you'd like Create a require/2 function that replicates the behavior we have in Ecto's validate_required

Describe alternatives you've considered Only thing that can be done at this moment is separetly list out each attribute in it's own present function AND override each default error message.

Express the feature either with a change to resource syntax, or with a change to the resource interface

For example

validates do
  validate required([:field1, :field2, :field3, :field4])
end

allenwyma avatar Feb 06 '25 05:02 allenwyma

Looks like a good plan to me :) I won't be able to do this in the near future, so PRs welcome!

zachdaniel avatar Feb 06 '25 12:02 zachdaniel

@zachdaniel feel free to assign to me if you'd like. I'll probably get to this pretty quickly

allenwyma avatar Feb 06 '25 12:02 allenwyma