Coder Social home page Coder Social logo

talos's Introduction

Talos

hex.pm hex.pm hex.pm hex.pm github.com Elixir CI

Talos is simple parameters validation library

Documentation can be found at ExDoc

Why another one validation library?

I needed more checks than just whether the value belonged to a particular data type.

This library allows you to define your own checks and use typical checks with a simple setup.

Usage

  defmodule CheckUserJSON do
    import Talos

    @interests_type enum(members: ["sports", "games", "food"])
    @user map(fields: [
      field(key: "email", type: string(min_length: 5, max_length: 255, regexp: ~r/.*@.*/)),
      field(key: "age", type: integer(gteq: 18, allow_nil: true)),
      field(key: "interests", type: list(type: @interests_type), optional: true)
    ])

    def validate(%{} = map_data) do
      errors = Talos.errors(@user, map_data)

      case errors == %{} do
        true -> :ok
        false -> {:error, errors}
      end
    end
  end

Somewhere in UserController

  ...

  def new_user(conn, params)
    case CheckUserJSON.valid?(params) do
       :ok -> 
          result = MyApp.register_user!(params)
          render_json(%{"ok" => true)
       {:error, errors} -> 
          render_json_errors(errors)
    end
  end
  
  ...

Own Type definition

If you want define own Type, just create module with Talos.Types behavior

defmodule ZipCodeType do
  @behaviour Talos.Types
  defstruct [length: 6]

  def valid?(%__MODULE__{length: len}, value) do
    String.valid?(value) && String.match?(value, ~r/\d{len}/)
  end

  def errors(__MODULE__, value) do
    case valid?(__MODULE__,value) do
      true -> []
      false -> ["#{value} is not zipcode"]
    end
  end
end

# And use it

Talos.valid?(%ZipCodeType{}, "123456") # => true
Talos.valid?(%ZipCodeType{}, "1234") # => false
Talos.valid?(%ZipCodeType{}, 123456) # => false

Installation

def deps do
  [
    {:talos, "~> 1.7"}
  ]
end

Contribution

Feel free to make a pull request. All contributions are appreciated!

talos's People

Contributors

sofakingworld avatar natali-maximenko avatar xfynx avatar

Watchers

James Cloos 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.