GithubHelp home page GithubHelp logo

deputy's Introduction

Deputy

Deputy is a declarative data conversion and validation library for Erlang.

It is meant to be useful for handling user input and reporting reasonably useful messages back to a user.

For awesome -type based checking (which can certainly be used with this) checkout github.com/extend/sheriff.

Simple Value Usage

Rules = [{{convert, float}, <<"Must be a floating point number">>}, 
         {{'<', 10.0}, <<"Must be less than 10.0">>}],
{ok, 5.5} = deputy:check(<<"5.5">>, Rules),
{error, [<<"Must be less than 10.0">>]} =
    deputy:check(<<"40.0">>, Rules),
{error, [<<"Must be a floating point number">>]} =
    deputy:check(<<"abcd">>, Rules),

Proplist Usage

Rules = [{<<"name">> , [{{regexp, <<"[a-zA-Z0-9]+">>}, <<"Must contain only alphanumerical characters">>}]}],
Attributes = [{<<"name">>, <<"##BatMan##">>}],
{error, [{<<"name">>, [<<"Must...">>]}]} = deputy:check_proplist(Attributes, Rules, []),

Addon Type Conversion and Validation

DateConvert = fun(Date) ->
    DateStr = binary_to_list(Date),
    case io_lib:fread("~4d-~2d-~2d", Date) of
        {ok, [Year, Month, Day]} ->
            {ok, {Year, Month, Day}};
        _ ->
            stop
    end.

CurrentDate = 
MinAge = 18,
%% bad age checker
CheckAge = fun(Date) ->
  Days = calendar:date_to_gregorian(Date),
  {Today, _} = calendar:local_time(),
  TodayDays = calendar:date_to_gregorian(Today),
  Age = (TodayDays - Days)/365.0,
  case Age > MinAge ->
      true -> ok
      false -> error
  end,

Rules = [{<<"birthday">>,
  [{{func, DateConvert}, <<"Must be a valid datetime string.">>},
   {{func, CheckAge}, <<"Must be at least 18 years old.">>}]}].

deputy's People

Contributors

teburd avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.