Skip to content

eraviart/Biryani.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Biryani

A data conversion and validation toolbox, written in Julia.

Build Status Coverage Status Biryani

Quickstart

julia> Pkg.add("Biryani")

julia> using Biryani

Usage Examples

Example 1: Email validator

Before starting a conversion or validation, the value to convert must be wrapped in a Convertible object. A Convertible contains 3 fields:

  • The value
  • A conversion context
  • An error
julia> convertible = Convertible("[email protected]")
Convertible("[email protected]",EmptyContext(),nothing)

Every converter accepts a Convertible as argument and returns another Convertible:

julia> input_to_email(convertible)
Convertible("[email protected]",EmptyContext(),nothing)

Operations can be chained using the |> operator:

julia> Convertible("[email protected]") |> input_to_email
Convertible("[email protected]",EmptyContext(),nothing)
julia> Convertible("   \n  ") |> input_to_email
Convertible(nothing,EmptyContext(),nothing)
julia> Convertible("John.DOE.name") |> input_to_email
Convertible("john.doe.name",EmptyContext(),"An email must contain exactly one \"@\".")

The value of a convertible can be extracted using the to_value function:

julia> Convertible("[email protected]") |> input_to_email |> to_value
"[email protected]"

When conversion has failed (the convertible has an error field), to_value throws an exception instead of returning the value:

julia> Convertible("John.DOE.name") |> input_to_email |> to_value
ERROR: An email must contain exactly one "@".
Value: john.doe.name
 in to_value at .../Biryani.jl:571
 in |> at ./operators.jl:178

When you don't want an exception to be thrown, use the to_value_error function instead of to_value. It returns a couple (value, error):

julia> value, error = Convertible("[email protected]") |> input_to_email |> to_value_error
("[email protected]",nothing)

julia> value, error = Convertible("John.DOE.name") |> input_to_email |> to_value_error
("john.doe.name","An email must contain exactly one \"@\".")

Example 2: Required email validator

Converters can be combined together to form more complex converters:

julia> Convertible("[email protected]") |> input_to_email |> require |> to_value_error
("[email protected]",nothing)

julia> Convertible("   \n  ") |> input_to_email |> require |> to_value_error
(nothing,"Missing value.")

julia> Convertible("John.DOE.name") |> input_to_email |> require |> to_value_error
("john.doe.name","An email must contain exactly one \"@\".")

The pipe converter can also be used to chain conversions:

julia> Convertible("[email protected]") |> pipe(input_to_email, require) |> to_value_error
("[email protected]",nothing)

You can easily create new converters by combining existing ones:

julia> input_to_required_email = pipe(input_to_email, require)
(anonymous function)

julia> Convertible("[email protected]") |> input_to_required_email |> to_value_error
("[email protected]",nothing)

julia> Convertible("John.DOE.name") |> input_to_required_email |> to_value_error
("john.doe.name","An email must contain exactly one \"@\".")

Example 3: Web form validator

A sample validator for a web form containing the following fields:

  • Username
  • Password (twice)
  • Email
julia> validate_form = struct(
         [
           "username" => pipe(strip, require),
           "password" => pipe(
             test(passwords -> length(passwords) == 2 && passwords[1] == passwords[2], error = "Password mismatch."),
             call(passwords -> passwords[1]),
           ),
           "email" => input_to_email,
         ],
       )

julia> input_data = [
         "username" => "   John Doe\n  ",
         "password" => ["secret", "secret"],
         "email" => "[email protected]",
       ]

julia> result, errors = Convertible(input_data) |> validate_form |> to_value_error
(["password"=>"secret","username"=>"John Doe","email"=>"[email protected]"],nothing)

Note: The same validation using the classic composition of functions instead of |>:

julia> result, errors = to_value_error(validate_form(Convertible(input_data)))
(["password"=>"secret","username"=>"John Doe","email"=>"[email protected]"],nothing)

julia> to_value_error(validate_form(Convertible(
         [
           "password" => ["secret", "other secret"],
           "email" => "   [email protected]\n  ",
         ],
       )))
(["password"=>ASCIIString["secret","other secret"],"username"=>nothing,"email"=>"[email protected]"],["password"=>"Password mismatch.","username"=>"Missing value."])

About

A conversion and validation toolbox

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages