Norm v0.2.0 Norm View Source
Norm provides a set of functions for specifying data.
Link to this section Summary
Functions
Choices between alternative predicates or patterns. The patterns must be tagged with an atom. When conforming data to this specification the data is returned as a tuple with the tag.
Verifies that the payload conforms to the specification
Verifies that the payload conforms to the specification or raises a Mismatch error
Creates a generator from a spec or predicate.
Creates a re-usable schema.
Specifies a selection of keys from a schema. This allows callsites to define what keys must be available from the input.
Creates a new spec. Specs can be created from any existing predicates or anonymous functions. Specs must return a boolean value.
Checks if the value conforms to the spec and returns a boolean.
Overwrites the default generator with a custom generator. The generator
can be any valid StreamData generator. This means you can either use Norms
built in gen/1
function or you can drop into StreamData directly.
Link to this section Functions
alt(specs) View Source
Choices between alternative predicates or patterns. The patterns must be tagged with an atom. When conforming data to this specification the data is returned as a tuple with the tag.
Examples
iex> conform!("foo", alt(s: spec(is_binary()), a: spec(is_atom())))
iex> conform!(:foo, alt(s: spec(is_binary()), a: spec(is_atom())))
iex> conform!(123, alt(num: spec(is_integer()), str: spec(is_binary())))
iex> conform!("foo", alt(num: spec(is_integer()), str: spec(is_binary())))
iex> conform(true, alt(num: spec(is_integer()), str: spec(is_binary())))
conform(input, spec) View Source
Verifies that the payload conforms to the specification
Examples:
iex> conform(42, spec(is_integer()))
iex> conform(42, spec(fn x -> x == 42 end))
iex> conform(42, spec(&(&1 >= 0)))
iex> conform(42, spec(&(&1 >= 100)))
iex> conform("foo", spec(is_integer()))
conform!(input, spec) View Source
Verifies that the payload conforms to the specification or raises a Mismatch error
gen(spec) View Source
Creates a generator from a spec or predicate.
iex> gen(spec(is_integer())) |> Enum.take(3) |> Enum.all?(&is_integer/1) true iex> gen(spec(is_binary())) |> Enum.take(3) |> Enum.all?(&is_binary/1) true iex> gen(spec(&(&1 > 0))) ** (Norm.GeneratorError) Unable to create a generator for: &(&1 > 0)
schema(input) View Source
Creates a re-usable schema.
selection(schema) View Source
Specifies a selection of keys from a schema. This allows callsites to define what keys must be available from the input.
selection(schema, path) View Source
spec(predicate) View Source (macro)
Creates a new spec. Specs can be created from any existing predicates or anonymous functions. Specs must return a boolean value.
Predicates can be arbitrarily composed using the and
and or
keywords.
Examples:
iex> conform!(21, spec(is_integer())) 21 iex> conform!(21, spec(is_integer() and &(&1 >= 21))) 21 iex> conform("21", spec(is_integer() and &(&1 >= 21)))
iex> conform!(:foo, spec(is_atom() or is_binary())) :foo iex> conform!("foo", spec(is_atom() or is_binary())) "foo" iex> conform(21, spec(is_atom() or is_binary()))
valid?(input, spec) View Source
Checks if the value conforms to the spec and returns a boolean.
iex> valid?(42, spec(is_integer())) true iex> valid?("foo", spec(is_integer())) false
with_gen(spec, generator) View Source
Overwrites the default generator with a custom generator. The generator
can be any valid StreamData generator. This means you can either use Norms
built in gen/1
function or you can drop into StreamData directly.
Examples
iex> Enum.take(gen(with_gen(spec(is_integer()), StreamData.constant("hello world"))), 3)
["hello world", "hello world", "hello world"]