2p4b

2p4b

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

First 2 of 2 Posts Switch mode

2p4b

2p4b OP

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.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New

We're in Beta

About us Mission Statement