-
Notifications
You must be signed in to change notification settings - Fork 0
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
Introduce validation of lists #20
Comments
@dimitarvp I want to clarify the multiple tag occurrence. %{tag: "12", limit: 1..9999,
children: [%{tag: "16", limit: 1..1}, %{tag: "21", limit: 1..1}]} How the above controls the children limit? E.g. imagine the following instance: [
%{tag: "12", children: [%{tag: "16"}, %{tag: "21"}],
%{tag: "12", children: [%{tag: "16"}],
%{tag: "12", children: [%{tag: "21"}],
] Shall we imply the uniqueness of children only amongst the immediate parent, or amongst the whole set of children of the respective |
Uniqueness should be hierarchical. Every |
To clarify: the example you gave in your comment is invalid because none of the 3 records of type [
%{tag: "00"},
%{tag: "11"},
#...
%{tag: "12", children: [ %{tag: "16"}, %{tag: "21"}, %{tag: "26"} ] },
%{tag: "12", children: [ %{tag: "16"}, %{tag: "21"}, %{tag: "26"} ] },
%{tag: "12", children: [ %{tag: "16"}, %{tag: "21"}, %{tag: "26"} ] },
#...
%{tag: "99"}
] ☝️ This is valid if the schema is like in the issue description (updated to add tag |
@dimitarvp I think I get it. So, basically what we need is the ability to validate:
We already have
Then your example would be guarded somewhat as: use Exvalibur,
types: [
leaf_tag: %{guards: %{tag: tag in ["00", "11", "99"]},
conditions:%{tag: limit: 1..1}},
branch_leaf_tag: %{guards: %{tag: tag in ["16", "21", "26"]}
conditions:%{tag: limit: 1..1}},
branch_tag: %{guards: %{tag: tag in ["12"]},
conditions:%{tag: limit: 1..1},
children: %{tag: item(branch_leaf_tag())}}
],
rules: [
%{matches: %{tags: leaf_tag() or branch_tag()}}] Right? |
More or less yes, unless I am reading your format wrong: use Exvalibur,
types: [
leaf_tag: %{guards: %{tag: tag in ["00", "11", "99"]},
conditions:%{tag: limit: 1..1}},
branch_leaf_tag: %{guards: %{tag: tag in ["16", "21", "26"]}
conditions:%{tag: limit: 1..1}},
branch_tag: %{guards: %{tag: tag in ["12"]},
conditions:%{tag: limit: 1..9999},
children: %{tag: item(branch_leaf_tag())}}
],
rules: [
%{matches: %{tags: leaf_tag() or branch_tag()}}
] Note the The part that sucks the most is writing the recursive validation code in such a way that it can show you exactly where input data doesn't comply. I've tried 3 versions so far and they all did the validation job fine but the error messages remain unconquered territory for me so far. Considering making How's your library in terms of pinpointing and reporting exact places in the input where validation fails? |
@dimitarvp Yeah,
It’s uh planned: #13 Actually, I never pretended this to be a preferable solution for complex data, and I am using I am to approach this issue in any case and will report back if there might be a better concise way to describe this kind of data. |
Sure, that's my goal as well -- to see if there's a neutral(ish) approach to recursive validation. I quite like Ecto and reach for it in every single hobby project even when there's no actual persistence involved (Ecto 3.0 makes this even easier). The But since I am quite interested in strong typing lately, I am trying to find a way to encode complex validations. If I find it, the end goal is it to be not Elixir-specific. Feel free to bounce ideas with me here or on ElixirForum. |
I've found myself in a need to validate a list with the rough shape of:
So, something like this:
In this example, tags
00
and11
and99
can appear once and only once, while the tag12
can appear1..9999
times but its children must appear only once. There are other structs/maps that must be present but some of their children are optional (0..1
occurrences).I was thinking of devising my own code to validate such lists by having a schema like this:
...etc. The point being, have a hierarchical structure that:
%{tag: x} when is_binary(x) and x in ~w[00 11 12 16 21 99]
, andAs the author of the library pointed out in ElixirForum (https://elixirforum.com/t/exvalibur-smart-validation-in-elixir/17677/14), this is akin to XML Schema (XSD), although I'd like to be able to specify such a validation schema by hand as well.
I admit I haven't fully cleared all the specifics inside my head yet -- I tried several recursive approaches and some of them work but they irritated me by being too specialised. So I was wondering if we can generalise a solution inside this library.
The text was updated successfully, but these errors were encountered: