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
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 2 of 2 Posts
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
idanduser_idinto a base schema, then extending it across your app.Base schema
Extending for different forms
Both
UserandAgentnow share all base fields while defining their own.Required fields propagate
Multi-level inheritance
Multiple inheritance
Overwriting inherited fields
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
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.