CargoSense / vex

Data Validation for Elixir
MIT License
595 stars 60 forks source link

Validation errors for nested structs #39

Open chrisjowen opened 7 years ago

chrisjowen commented 7 years ago

Hey, nice library. Im wondering if its possible to expose the errors for nested structs currently I am using the Vex.Struct module and defining a custom function for validating a nested structure i.e.

validates :nested, &NestedVexStruct.valid?/1

This works great and gives me the ability to run the nested validations but if it fails the error I get is only associated with this top level validator and not the actual causes in the sub validator. Any ideas around this?

dwhelan commented 6 years ago

@chrisjowen were you able to resolve this?

dkushner commented 6 years ago

@dwhelan I'm ssuming that since this issue is about two years old, I don't have much hope of a resolution in this case, huh?

larskluge commented 4 years ago

I realize that since this question has been asked hoverboards have been invented.. however, the following seems to work well for me (so far):

validates :nested, &Vex.validate/1

.validate instead of .valid? to propagate the error message. This works for me since my nested struct implements use Vex.Struct. Otherwise I noticed nested fields are somewhat supported: https://github.com/CargoSense/vex/blob/e92330d2ef2cdaa80b9abdc00c8db867b42dc971/test/validations/nested_test.exs#L33