avo
avo copied to clipboard
Automatically determine required fields from validations
Feature
Right now, to mark a field as required you have to explicitly add the required: true to the field declaration in the resource class. Simple form does this automatically by looking at the validations on the model (see the code here). It would be great if avo could do this too (if required: is not set in the resource)
Current workarounds
The only way to do this right now is to manually keep the validations in the model and resource definition in sync, but it is a lot of work and I'm not sure that anyone could do that reliably
Screenshots
Additional context
Here is the code that simple form uses:
- https://github.com/heartcombo/simple_form/blob/86429bceb950096df3c29616f31bd5a5ce706c06/lib/simple_form/helpers/required.rb
- https://github.com/heartcombo/simple_form/blob/86429bceb950096df3c29616f31bd5a5ce706c06/lib/simple_form/helpers/validators.rb
Great idea. I'll have a look over that snippet and add it to the roadmap.
I was pointed to this issue on the discord server. I think it'd be fantastic for required to be inferred based on the model/validations, but I think the required field should also continue to be overridable such that we can force it to be required (or not) in Avo.
Also, this feels like a nice first issue so @iainbeeston, if you want to take a stab, that'd be super cool!
https://github.com/avo-hq/avo/pull/955 might help get you started in seeing where to hook in and make this happen.