Apollo

Package Version Hex Docs

A validation library named after the God of truth and prophecy.

An Basic Example

import gleam/io
import apollo/t
import apollo/filter as f

pub type Post {
  Post(id: String, timestamp: Int, title: String, content: String)
}

pub fn main() {
  let post_schema: t.Schema(Post) =
    apollo.typed(
      Post,
      [
        #(
          "id",
          t.string()
          |> f.min(4)
          |> f.max(10),
        ),
        #(
          "timestamp",
          t.int()
          |> f.length(10),
        ),
        #(
          "title",
          t.string()
          |> f.min(3)
          |> f.max(40),
        ),
        #("content", t.string()),
      ],
    )

  let post =
    apollo.validate(
      post_schema,
      #(
        #("id", "549"),
        // id should be at least 4 characters
        #("timestamp", 1_691_611_376),
        #("title", "Apollo, the God of Truth and Prophecy"),
        #(
          "content",
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
        ),
      ),
    )
  io.debug(post) // Error("Length of \"549\" is less than than 4")
}

Installation

This package can be added to your Gleam project:

gleam add apollo

and its documentation can be found at https://hexdocs.pm/apollo.

Search Document