Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhanberg/schematic
π schematic
https://github.com/mhanberg/schematic
data elixir specification validation
Last synced: 4 days ago
JSON representation
π schematic
- Host: GitHub
- URL: https://github.com/mhanberg/schematic
- Owner: mhanberg
- License: mit
- Created: 2023-03-11T03:45:47.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-11T02:35:15.000Z (3 months ago)
- Last Synced: 2024-12-14T09:06:07.094Z (11 days ago)
- Topics: data, elixir, specification, validation
- Language: Elixir
- Homepage:
- Size: 99.6 KB
- Stars: 83
- Watchers: 4
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# schematic
[![Hex.pm](https://img.shields.io/hexpm/v/schematic)](https://hex.pm/packages/schematic)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/schematic/)schematic is a library for data specification, validation, and transformation.
schematic works by constructing **schematics** that specify your data and can then **unify** to them from external data and **dump** your internal data back to the external data.
There are 12 builtin schematics that you can use to build new schematics that fit your own domain model.
- `bool/0`
- `str/0`
- `atom/0`
- `int/0`
- `float/0`
- `list/1`
- `tuple/1`
- `map/1`
- `schema/2`
- `raw/2`
- `any/0`
- `all/1`
- `oneof/1`Literals can be used as schematics and are unified with `==` semantics.
Struct literals can be used as schematics, and the input is unified by seeing if it is an instance of the given struct.
## Example
Let's take a look at an example schematic for a JSON-RPC request for a bookstore API.
```elixir
defmodule Bookstore do
defmodule Datetime do
import Schematicdef schematic() do
raw(
fn
i, :to -> is_binary(i) and match?({:ok, _, _}, DateTime.from_iso8601(i))
i, :from -> match?(%DateTime{}, i)
end,
transform: fn
i, :to ->
{:ok, dt, _} = DateTime.from_iso8601(i)
dti, :from ->
DateTime.to_iso8601(i)
end
)
end
enddefmodule Author do
import Schematicdefstruct [:name]
def schematic() do
schema(__MODULE__, %{
name: str()
})
end
enddefmodule Book do
import Schematicdefstruct [:title, :authors, :publication_date]
def schematic() do
schema(__MODULE__, %{
{"publicationDate", :publication_date} => Bookstore.Datetime.schematic(),
title: str(),
authors: list(Bookstore.Author.schematic())
})
end
enddefmodule BooksListResult do
import Schematicdefstruct [:books]
def schematic() do
schema(__MODULE__, %{
books: list(Bookstore.Book.schematic())
})
end
enddefmodule BooksListParams do
import Schematicdefstruct [:query, :order]
def schematic() do
schema(__MODULE__, %{
query:
nullable(
map(%{
{"field", :field} =>
oneof(["title", "authors", "publication_date"]),
{"value", :value} => str()
})
),
order: nullable(oneof(["asc", "desc"]))
})
end
enddefmodule BooksList do
import Schematicdefstruct [:id, :method, :params]
def schematic() do
schema(__MODULE__, %{
id: int(),
method: "books/list",
params: Bookstore.BooksListParams.schematic()
})
end
end
end
```Reading external data into your data model.
```elixir
iex> alias SchematicTest.Bookstore
iex> import Schematic
iex> unify(Bookstore.BooksList.schematic(), %{
...> "id" => 99,
...> "method" => "books/list",
...> "params" => %{
...> "query" => %{
...> "field" => "authors",
...> "value" => "Michael Crichton"
...> },
...> "order" => "desc"
...> }
...> })
{:ok,
%Bookstore.BooksList{
id: 99,
method: "books/list",
params: %Bookstore.BooksListParams{
query: %{field: "authors", value: "Michael Crichton"},
order: "desc"
}
}}
```Dumping your internal data model.
```elixir
iex> alias SchematicTest.Bookstore
iex> import Schematic
iex> dump(Bookstore.BooksListResult.schematic(), %Bookstore.BooksListResult{
...> books: [
...> %Bookstore.Book{
...> title: "Jurassic Park",
...> authors: [%Bookstore.Author{name: "Michael Crichton"}],
...> publication_date: ~U[1990-11-20 00:00:00.000000Z]
...> },
...> %Bookstore.Book{
...> title: "The Lost World",
...> authors: [%Bookstore.Author{name: "Michael Crichton"}],
...> publication_date: ~U[1995-09-08 00:00:00.000000Z]
...> }
...> ]
...> })
{:ok,
%{
"books" => [
%{
"authors" => [%{"name" => "Michael Crichton"}],
"publicationDate" => "1990-11-20T00:00:00.000000Z",
"title" => "Jurassic Park"
},
%{
"authors" => [%{"name" => "Michael Crichton"}],
"publicationDate" => "1995-09-08T00:00:00.000000Z",
"title" => "The Lost World"
}
]
}}
```## Telemetry
schematic fires the following events:
- `[:schematic, :unify, :start]` - Fired when unification starts.
- `[:schematic, :unify, :stop]` - Fired when unification stops.
- `[:schematic, :unify, :exception]` - Fired when unification raises an exception.## Installation
```elixir
def deps do
[
{:schematic, "~> 0.1"}
]
end
```Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at .## License
The MIT License (MIT)
Copyright Β© 2023 Mitchell A. Hanberg
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the βSoftwareβ), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.