2p4b

2p4b

Draft - enforce typed structs and validation rules with draft schema

Define typed structs with built-in validation. Draft allows developers to describe the structure of data and enforce constraints when constructing structs, helping ensure that invalid data never enters your domain.

Instead of passing loosely shaped maps through your application, Draft lets you define clear schemas for your data while keeping things lightweight and independent from persistence layers.

Example

defmodule Book do
  use Draft.Schema

  schema required: true do
    field :id, :string
    field :title, :string, min: 1, max: 32
    field :author_id, :string
    field :isbn, :integer
  end
end

Creating a struct:

book = Book.new(%{id: "1", title: "Draft", author_id: "a1", isbn: 1234})
{:ok, book} = Book.cast(%{id: "1", title: "Draft", author_id: "a1", isbn: 1234})
{:error, errors} = Book.cast(%{id: "1", title: "", author_id: "a1"})

Draft validates the data against the schema and returns a properly constructed struct when the input is valid.

When the data is valid:

{:ok, book} = Draft.validate(%Book{id: "1", title: "Draft", author_id: "a1", isbn: 1234})

Why Draft?

Elixir makes it easy to work with maps and structs, but validation and shape guarantees are often left to ad-hoc code or external systems. Draft provides a simple way to:

  • Define data schemas

  • Enforce types and constraints

  • Safely construct validated structs

  • Keep domain models independent of databases or frameworks

  • Cast and validate structured outputs from LLMs before using them in your system

When working with LLMs, responses are often returned as JSON or maps. Draft can be used as a guard layer to ensure the generated data matches the expected structure and constraints before it enters your application logic.

Use cases

Draft is useful in situations where you want validated data structures without introducing database dependencies, such as:

  • Domain models

  • API request/response validation

  • Configuration structures

  • Internal application data

  • Casting and validating LLM outputs (for example, ensuring generated JSON matches the expected schema before it is used)

Status

Draft is currently in active development and feedback from the community is welcome.

Links

Hex: https://hex.pm/packages/draft
Documentation: https://hexdocs.pm/draft

https://github.com/2p4b/blueprint

Most Liked

2p4b

2p4b

Inheritance in Draft (Docs Update)

Draft supports schema inheritance, making it easier to reuse common fields across forms and domain models.A typical pattern is extracting shared fields like id and user_id into a base schema, then extending it across your app.

Base schema

defmodule Entity do
  use Draft.Schema

  schema do
    field :id,         :uuid
    field :user_id,    :uuid
    field :name,       :string
    field :role,       :string
  end
end

Extending for different forms

defmodule User do
  use Draft.Schema

  schema extends: Entity do
    field :email, :string
  end
end

defmodule Agent do
  use Draft.Schema

  schema extends: Entity do    
    field :active, :boolean
  end
end

Both User and Agent now share all base fields while defining their own.


Required fields propagate

defmodule Entity do
  use Draft.Schema

  schema required: true do
    field :id,      :uuid
    field :user_id, :uuid
  end
end

defmodule User do
  use Draft.Schema

  schema extends: Entity do
    field :email, :string, required: true
  end
end

User.new(email: "test@example.com")
# raises — :id and :user_id are required


Multi-level inheritance

defmodule Timestamps do
  use Draft.Schema

  schema do
    field :created_at, :datetime
    field :updated_at, :datetime
  end
end

defmodule Entity do
  use Draft.Schema

  schema extends: Timestamps do
    field :id, :uuid
    field :user_id, :uuid
  end
end

defmodule Agent do
  use Draft.Schema

  schema extends: Entity do
    field :role, :string
  end
end


Multiple inheritance

defmodule SoftDelete do
  use Draft.Schema

  schema do
    field :deleted_at, :datetime
  end
end

defmodule Agent do
  use Draft.Schema

  schema extends: [Entity, SoftDelete] do
    field :role, :string
  end
end


Overwriting inherited fields

defmodule User do
  use Draft.Schema

  schema extends: Entity do
    field :email, :string
  end
end

defmodule Agent do
  use Draft.Schema

  schema extends: User do
    field :email, :string, overwrite: true, format: :email
  end
end


Inheritance lets you define common form fields once and reuse them everywhere. While still allowing specialization, stricter validation, or composition when needed. This keeps your schemas consistent and eliminates duplication across forms, APIs, and internal models.

mudasobwa

mudasobwa

Creator of Cure

You might want to take a look at estructura, e. g. for the inspiration. It supports deeply nested structs, custom types with coercion and validation, andalso data generation for property-based testing.

Where Next?

Popular in Announcing Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
bradley
I’ve been working with Claude Code extensively and absolutely love it. However, I’ve come across the challenge of managing configuration ...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. The distributed characteristics of Elixir and the low memory footprint...
New
RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 12673 134
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamPipe ...
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
zachdaniel
Ash Framework What is Ash? Ash Framework is a declarative, resource-oriented application development framework for Elixir. A resource can...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement