Giter VIP home page Giter VIP logo

exvalibur's Introduction

exvalibur's People

Contributors

am-kantox avatar mudasobwa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

exvalibur's Issues

Allow custom validation

@behaviour Exvalibur.Validatable

@callback custom_validate(data :: term()) :: {:ok, term()} | :error

and defoverridable custom_validate: 1.

Property-test the validator generated

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.

Make a key in rules map as term_to_binary

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.

Accept both match _and_ condition for the key

Resulting in generated:

def valid?(%{foo: <<"FOO", rest :: binary()>>}) when bitstring(rest) < 42

Additional questions:

  • invent a smart way to handle patterns in parameters to rules (simple macro?)
  • do we need this?

Introduce validation of lists

I've found myself in a need to validate a list with the rough shape of:

  • The list contains structs/maps of different types.
  • Each of the types has a minimum and maximum amount of occurrences allowed (specified by an external non-machine-readable schema).
  • Some of the structs/maps embed others in a tree-like children list attribute.

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:

  • Gives you a pattern to match on -- in this case it's %{tag: x} when is_binary(x) and x in ~w[00 11 12 16 21 99], and
  • Assert on how many times the given pattern can occur.

As 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.