Code Monkey home page Code Monkey logo

gleam-validator's Introduction

Valid

CI

A validation library for Gleam.

API Docs: https://hexdocs.pm/valid.

This library follows the principle Parse don't validate.

Install

gleam add valid

Usage

You start with an input type and validate into an output type. These two types can be different. For example:

type UserInput { UserInput(name: Option(String), age: Int) }

type ValidUser { ValidUser(name: String, age: Int) }

Then you create a validator like:

import validator
import validator/common.{ValidatorResult}
import validator/int
import validator/option

fn user_validator(user: UserInput) -> ValidatorResult(ValidUser, String) {
  validator.build2(ValidUser)
  |> validator.validate(user.name, option.is_some("Please provide a name"))
  |> validator.validate(user.age, int.min(13, "Must be at least 13 years old"))
}

And run it:

case user_validator(input) {
  Ok(valid_user) -> ...
  Error(tuple(first_error, all_errors)) -> ...
}

Error type

Errors can be your own type e.g.

import validator
import validator/common.{ValidatorResult}
import validator/int
import validator/option

type Error {
  ErrorEmptyName,
  ErrorTooYoung,
}

fn user_validator(user: UserInput) -> ValidatorResult(ValidUser, String) {
  validator.build2(ValidUser)
  |> validator.validate(user.name, option.is_some(ErrorEmptyName))
  |> validator.validate(user.age, int.min(13, ErrorTooYoung))
}

ValidatorResult

ValidatorResult(valid, error) is an alias for Result(valid, tuple(error, List(error)))

The Ok branch has the valid output.

The Error branch has a tuple tuple(error, List(error)). The first value is the first error. The second value is a list with all errors (including the first).

Validators

See the API Docs for the list of included validators.

Custom property validator

A property validator has two components:

  • The error to return
  • A function that transforms the property if successful (fn(input) -> Option(output))

Example:

import gleam/option.{None, Option, Some}
import validator

fn bigger_than_10(num: Int) -> Option(num) {
  case num > 10 {
    True ->
      Some(num)
    False ->
      None
  }
}

let custom = validator.custom("Must be bigger than 10", bigger_than_10)

let validator = fn(form: FormInput) {
  validator.build1(ValidForm)
  |> validator.validate(form.quantity, custom)
}

Examples

See the tests for many examples

Test

rebar3 compile
rebar3 eunit

Installation

This package can be installed by adding gleam_validator to your rebar.config dependencies:

{deps, [
    gleam_validator
]}.

gleam-validator's People

Contributors

michallepicki avatar scotttrinh avatar sporto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.