Skip to content

Validation and sanitization library of Elixir for nested objects and arrays

License

Notifications You must be signed in to change notification settings

keshihoriuchi/paraiso

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Paraiso

Validation and sanitization library of Elixir for nested objects and arrays

Install

def deps do
  [
    {:paraiso, "~> 0.0.1"}
  ]
end

Usage Example

import Paraiso

email_regex =
  ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

props = [
  prop(:user_id, :required, {:string, {:regex, ~r/^[a-zA-Z0-9]{1,255}$/}}),
  prop(:name, {:optional, ""}, {:string, {:range, 0, 255}}),
  prop(
    :emails,
    {:optional, []},
    {:array,
     {:object,
      [
        prop(:email_address, :required, {:string, {:regex, email_regex}}),
        prop(:is_primary, :required, :boolean),
        prop(:notification, {:optional, false}, :boolean)
      ]}}
  )
]

## Success case
sample = %{
  "user_id" => "keshihoriuchi",
  "name" => "Takeshi Horiuchi",
  "emails" => [
    %{
      "email_address" => "[email protected]",
      "is_primary" => true,
      "notification" => true
    },
    %{
      "email_address" => "[email protected]",
      "is_primary" => false
    }
  ]
}

{:ok, result} = Paraiso.process(sample, props)

expect = %{
  user_id: "keshihoriuchi",
  name: "Takeshi Horiuchi",
  emails: [
    %{
      email_address: "[email protected]",
      is_primary: true,
      notification: true
    },
    %{
      email_address: "[email protected]",
      is_primary: false,
      notification: false
    }
  ]
}

assert(result === expect)

## Failure case
sample = %{
  "user_id" => "keshihoriuchi",
  "name" => "Takeshi Horiuchi",
  "emails" => [
    %{
      "email_address" => "invalid string",
      "is_primary" => true,
      "notification" => true
    },
    %{
      "email_address" => "[email protected]",
      "is_primary" => false
    }
  ]
}

assert({:error, [:emails, 0, :email_address], :invalid} === Paraiso.process(sample, props))

API (Japanese Language)

https://hexdocs.pm/paraiso/Paraiso.html

About

Validation and sanitization library of Elixir for nested objects and arrays

Topics

Resources

License

Stars

Watchers

Forks

Languages