Skip to content
forked from erlangbureau/liver

Lightweight Erlang validator based on LIVR specification

License

Notifications You must be signed in to change notification settings

tigertext/liver

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Liver

Build Status Coverage Status

Summary

Logo

Liver is a lightweight Erlang validator based on LIVR Specification (See http://livr-spec.org for details)

Table of Contents

Description

LIVR specification features:

  1. Rules are declarative and language independent
  2. Any number of rules for each field
  3. Validator should return together errors for all fields
  4. Exclude all fields that do not have validation rules described
  5. Possibility to validate complex hierarchical structures
  6. Easy to describe and understand validation
  7. Returns understandable error codes (neither error messages nor numeric codes)
  8. Easy to implement own rules (usually you will have several in every project)
  9. Rules should be able to change results output ("trim", "nested_object", for example)
  10. Multipurpose (user input validation, configs validation, contracts programming etc)
  11. Unicode support

This implementation specific features:

  1. Strict Mode (returns errors on all fields that do not have validation rules)
  2. Support on-the-fly conversion from proplist to map and vice versa
  3. Support list as root element, what is missing in the original specification
  4. Support on-the-fly conversion keys from string to atom
  5. Ability to return custom error codes
  6. Additional set of strict rules (without implicit conversion of types)

Geting Started

  1. Add as a dependency in your project:
  • For rebar add to rebar.config
{deps, [
  {liver, ".*",
      {git, "https://github.com/erlangbureau/liver.git", {branch, "0.9.0"}}
  }
]}.
  • For erlang.mk add to make file:
DEPS = liver
dep_liver = git https://github.com/erlangbureau/liver.git 0.9.0
  1. Add liver in your_project.app.src file in tuple applications:
    {applications, [
        kernel,
        stdlib,
        liver
    ]},
  1. Thats all, now you can validate data, register your own rules or add aliases for built-in rules.

Usage Examples

Simple validation example:

1> Schema1 = [{<<"first_name">>,[{length_between,[4,6]}]}].

2> Input1 =  [{<<"first_name">>,<<"Vasya">>}].

3> liver:validate(Schema1, Input1).
{ok, [{<<"first_name">>,<<"Vasya">>}]}

4> Schema2 = [{<<"number1">>,integer}].

5> Input2 =  [{<<"number1">>,-1.12}].

6> liver:validate(Schema2, Input2).
{error,[{<<"number1">>,<<"NOT_INTEGER">>}]}

More complex validation example:

7> Schema = #{
    <<"address">> => [required, {nested_object, #{
        <<"country">> => [required,{one_of,[[<<"Ukraine">>,<<"USA">>]]}],
        <<"zip">> => positive_integer,
        <<"street">> => required,
        <<"building">> => [required,positive_integer]
    }}]
}.

8> Input = #{
    <<"address">> => #{
        <<"country">> => <<"Ukraine">>,
        <<"zip">> => <<"12345">>,
        <<"street">> => <<"10">>,
        <<"building">> => <<"10">>,
        <<"extra_field">> => <<"will be removed">>
    },
    <<"extra_field">> => <<"will be removed">>
}.

9> liver:validate(Schema, Input).
{ok,#{<<"address">> => #{<<"building">> => 10,
        <<"country">> => <<"Ukraine">>,
        <<"street">> => <<"10">>,
        <<"zip">> => 12345}}}

Strict validation example:

10> liver:validate(Schema, Input, [{strict, true}]).
{error,#{<<"address">> => #{<<"extra_field">> => <<"UNKNOWN_FIELD">>},
         <<"extra_field">> => <<"UNKNOWN_FIELD">>}}

Exports

validate/2

validate(Schema, Input) -> {ok, Output} | {error, ErrorList}

  Schema, Input, Output, ErrorList = proplist() | map()

Equivalent to validate(Schema, Input, []).

validate/3

validate(Schema, Input, Opts) -> {ok, Output} | {error, ErrorList}

  Schema, Input, Output, ErrorList, Opts = proplist() | map()

Parameter Opts is a proplist or map that specifies return type and validation strictness. Default values are used for omitted options. This means that not specifying any options ([]) is the same as specifying [{return, as_is}, {strict, false}].

{return, ReturnType}

If set to as_is the type of Output wiil be the same as type of Input. If set to map the Output will be map. If set to proplist the Output will be proplist. Defaults to as_is

{strict, boolean()}

If set to false deletes from Input all fields that not defined in Schema. Or if set to true and Input has fields that not defined in Schema returns error. Defaults to false

License

Liver is released under the MIT License

About

Lightweight Erlang validator based on LIVR specification

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Erlang 99.4%
  • Makefile 0.6%