GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - heywhy/elixir-request-validator: A blazing fast request validator for your phoenix app, which validates a request body before hitting the request handler in the controller.

BranchesTags
Open more actions menu

Latest commit

 

History

124 Commits
124 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
config
config
 
 
lib
lib
 
 
priv/gettext
priv/gettext
 
 
test
test
 
 
.credo.exs
.credo.exs
 
 
.formatter.exs
.formatter.exs
 
 
.gitignore
.gitignore
 
 
.tool-versions
.tool-versions
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
coveralls.json
coveralls.json
 
 
mix.exs
mix.exs
 
 
mix.lock
mix.lock
 
 

Repository files navigation

RequestValidator

A blazing fast request validator for your phoenix and bared plug app, which validates a request body before hitting the request handler in the controller.

  • Why RequestValidator
  • Installation
  • Usage
  • Custom Validation Rules

Why RequestValidator

It's common for a web service to validate incoming data. The most common layer where this is done is at the controller level and this sometimes leads to having a bloated controller. But with RequestValidator, you move the validation logic to a layer just before the controller and your controller is now free from doing validation.

Installation

The package can be installed by adding request_validator to your list of dependencies in mix.exs:

def deps do
  [
    {:request_validator, "~> 1.0.2"}
  ]
end

Usage

First of all, you need to define a validation schema to be used against the incoming data.

defmodule App.Requests.Registration do
  use Request.Validator

  import Request.Validator.Rules

  # Get the validation rules that apply to the incoming request.
  @impl Request.Validator
  def rules(_) do
    %{
      "email" => ~V[required|email],
      "first_name" => ~V[required|string],
      "last_name" => ~V[required|string],
      "password" => ~V[required|string|min:8|confirmed]
    }
  end

  # Determine if the user is authorized to make this request.
  @impl Request.Validator
  def authorize?(_), do: true
end

The above validation schema can now be used;

defmodule App.UserController do
  use AppWeb, :controller
  use Request.Validator.Plug

  @validate App.Requests.Registration
  def register(conn, params) do
    case App.UserService.create(params) do
      :ok ->
        conn
        |> put_status(201)
        |> json(%{message: "Account created successfully"})

      {:error, msg} ->
        conn
        |> put_status(500)
        |> json(%{message: msg})
    end
  end
end

As you can see in the controller, the register handler does not need to worry about validating the incoming request because RequestValidator will handle that automatically and send the right response if the request fails validation based on the given validation schema.

You can specify validation schema for each of the handlers in a controller:

defmodule App.UserController do
  use AppWeb, :controller
  use Request.Validator.Plug

  @validate App.Requests.Login
  def login(conn, params) do
    # ...
  end

  @validate App.Requests.Registration
  def register(conn, params) do
    # ...
  end
end

Full documentation can be found at https://hexdocs.pm/request_validator.

Custom Validation Rules

This library provides a variety of helpful rules, however, you might want to define some rules to house your validation logic. To achieve this, you need to create your own rules module;

defmodule App.Validation.Rules do
  import Request.Validator.Rules

  alias Request.Validator.Rules

  @spec uppercase() :: Rules.validator()
  def uppercase do
    fn attr, value ->
      message = gettext("The %{attribute} field must be uppercase.", attribute: attr)

      case String.upcase(value) == value do
        true -> :ok
        false -> {:error, message}
      end
    end
  end
end

To use, you simply import your custom rules and;

import Request.Validator.Rules
import App.Validation.Rules

# ...
  def rules(_) do
    %{
      "name" => ~V[required|string|uppercase],
      "age" => ~V[required|integer],
      "parent" => [required_if("age", 18, &Kernel.<=/2), upppercase()]
    }
  end
# ...

Available Rules

Find rules here.

Validating Nested Input

Validating nested map input shouldn't be a problem. For example the HTTP request contains address field which is a map with nested attributes (line1, line2...), you may validate it like so:

%{
  "address.line1" => ~V[required|string|max:100],
  "address.city" => ~V[required|string|max:50],
  "address.zip_code" => ~V[required|string|max:10],
  "address.country" => ~V[required|string|max:60],
  "address.line2" => ~V[string|max:100],
  "address.state" => ~V[string|max:50]
}

In the case where you want to validate elements nested in a list, you should use the * character;

# request
%{
  "likes" => ["epl", "laliga"],
  "documents" => [%{"type" => "selfie"}, %{"type" => "id", "issuing_country" => "US"}]
}

# rules
%{
  "likes" => ~V[required|list|min:1],
  "likes.*" => ~V[required|allowed:epl,laliga],
  "document" => ~V[required|list|min:2],
  "documents.*.type" => ~V[required|allowed:selfie,id]
}

TODOS

  • Include more validation rules
  • Norm validation support

License

RequestValidator is released under the MIT License - see the LICENSE file.

About

A blazing fast request validator for your phoenix app, which validates a request body before hitting the request handler in the controller.

Topics

ecto-supportelixirphoenixphoenix-elixirphoenix-frameworkrequestvalidator

Resources

Readme
MIT license
Activity

Stars

13 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.