-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add what validation failed to NimbleOptions.ValidationError
#131
Comments
Okay, to do this we would need a possibly significant overhaul of how we represent errors internally in nimble_options. Right now, errors are free-form strings. This can be problematic for things like #132 for example, because the string is unstructured. Instead we could:
This is a poor implementation of exceptions and If you want, you could give this implementation a try—and ask any questions if you have them. However, I’m not so sure it will make sense in nimble_options as it has to keep one property: nimbleness 😄 I would like to maintain this lib on the smaller side. @josevalim btw any thoughts on this? I’m open to the idea above because I think it could make the lib easier to maintain and contribute to in the future (plus, types!). |
It feels we could tackle this problem by adding some metadata to the ValidationError struct without changing the error message? The only issue is that validation errors can be nested (for example, a nested keyword, and tracking this whole "path" may be complicated - although still doable with a list). |
Adding metadata to the error struct and leaving the error message alone is another valid approach yeah but I think it would end up being more work than turning the reason into a structured type and then converting that into a message when needed? |
Another option is to allow them to be MFAs or {&fun/1, metadata}, and then folks can match on the metadata bits they find interesting. |
Ah yeah you are correct that the predefined error reason ( So I think we have two possible directions:
Preferences? |
@anthonator which kind of validations do you want to know about? If it was required or a type violation? @whatyouhide Are people supposed to use NimbleOptions for anything else? In an ideal world, this project is dead once the type system lands (either because it is not needed or because it is part of core). |
The freeform metadata idea was something I was actually going to suggest in the issue description. Even with the Regarding 1, I'm not exactly sure what you mean by "type system". I'm not sure if you mean the new Elixir type system or maybe something you've been discussing internally for NimbleOptions. So I can't really comment on that approach. However, you seem excited about it and that kind of gets me excited about it 😄 |
@josevalim for my specific use case I really just want to know if the error was due to It might also be useful to add a |
That sounds straight-forward to me and probably how I would tackle this (i.e. solving this specific problem), but once again, it gets trickier once we consider nesting. |
Why does this feature need to worry about nesting given that |
I forgot about keys path. So adding a field that has value of either :required or {:type, type} is good enough to me. |
Agreed, let's solve the specific problem for now without going nuts. 😄 @anthonator wanna make a PR? |
Heck yeah! I won't be able to start on this until after #133 is resolved. |
I'd like to be able to perform some additional processing when validation fails for specific reasons. This is difficult to do considering there's no way to know what caused the validation error beyond the error message.
I could see a
:validation
field onNimbleOptions.ValidationError
that would have a value of:required
,{:type, :string}
, etc.The text was updated successfully, but these errors were encountered: