am-kantox / exvalibur Goto Github PK
View Code? Open in Web Editor NEWElixir Validator Generator
Elixir Validator Generator
Add transform: {module(), atom(), list()}
MFA to allow inplace transformations of validated values.
use Exvalibur, rules: [%{num: [integer(), binary()]}]
Allow a flag passed to validator!
like flow: true
to process huge maps using Flows.
Make a property test for the arbitrary set of conditions: generate a rule and then explicitly evaluate the result, asserting it’s the same as the one returned by the validator.
I've found myself in a need to validate a list with the rough shape of:
So, something like this:
[
%{tag: "00", ...},
%{tag: "11", ...},
%{tag: "12", ..., children: [%{tag: "16", ...}, %{tag: "21", ...}]},
# ...A lot more recursive rules like these follow...
%{tag, "99", ...}
]
In this example, tags 00
and 11
and 99
can appear once and only once, while the tag 12
can appear 1..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:
[
%{tag: "00", limit: 1..1},
%{tag: "11", limit: 1..1},
%{tag: "12", limit: 1..9999, children: [%{tag: "16", limit: 1..1}, %{tag: "21", limit: 1..1}, %{tag: "26", limit: 1..1}]},
%{tag: "99", limit: 1..1},
]
...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.
@behaviour Exvalibur.Validatable
@callback custom_validate(data :: term()) :: {:ok, term()} | :error
and defoverridable custom_validate: 1
.
Probably use a DynamicSupervisor
with restart: :transient
.
Now it results in :error
for everything.
Make it possible to read rules from CSV file.
The current implementation uses only direct matches to maintain the internal rules map, what makes it impossible to have two rules/clauses with the same matches and different guards/conditions.
Use term_to_binary
as a key instead.
Context: https://elixirforum.com/t/exvalibur-smart-validation-in-elixir/17677/9
v0.9.0
: introduce validate/1
, doing exactly what valid?/1
does now; issue a warning from valid?/1
about it’ll return boolean as of v1.0
.
On validation error return extended information instead of plain dry :error
.
Resulting in generated:
def valid?(%{foo: <<"FOO", rest :: binary()>>}) when bitstring(rest) < 42
Additional questions:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.