sift

Package Version Hex Docs

Schema validation for Gleam — constraints, error accumulation, and field paths.

All errors are collected in a single pass using the use pattern. No short-circuiting, no missing fields.

gleam add sift@1

Quick example

import sift
import sift/string as s
import sift/int as i

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

pub type User {
  User(name: String, email: String, age: Int)
}

pub fn validate(input: UserInput) -> Result(User, List(sift.FieldError)) {
  use name <- sift.check("name", input.name, s.min_length(1, "required"))
  use email <- sift.check("email", input.email, s.email("invalid email"))
  use age <- sift.check("age", input.age, i.between(0, 150, "out of range"))
  sift.ok(User(name:, email:, age:))
  |> sift.validate
}

Invalid input returns all errors at once:

validate(UserInput(name: "", email: "nope", age: -1))
// -> Error([
//   FieldError(path: ["name"], message: "required"),
//   FieldError(path: ["email"], message: "invalid email"),
//   FieldError(path: ["age"], message: "out of range"),
// ])

Features

Modules

ModuleValidators
siftcheck, nested, each, ok, validate, and, or, not, equals, custom
sift/stringmin_length, max_length, length, non_empty, matches, one_of, starts_with, ends_with, contains, email, url, uuid
sift/intmin, max, between, positive, non_negative, one_of
sift/floatmin, max, between, positive
sift/listmin_length, max_length, non_empty
sift/optionrequired, optional

Further documentation can be found at https://hexdocs.pm/sift.

Search Document