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

sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19483 141
New
Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
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
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 13056 120
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
sbs
Only 650 LOC, wrote for fun :slight_smile: https://github.com/sunboshan/qrcode
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement