{"id":16656501,"url":"https://github.com/mhanberg/schematic","last_synced_at":"2025-04-12T21:31:15.649Z","repository":{"id":142816439,"uuid":"612475395","full_name":"mhanberg/schematic","owner":"mhanberg","description":"📐 schematic","archived":false,"fork":false,"pushed_at":"2025-03-29T14:52:27.000Z","size":114,"stargazers_count":91,"open_issues_count":2,"forks_count":5,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-04T01:09:26.187Z","etag":null,"topics":["data","elixir","specification","validation"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mhanberg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"mhanberg","custom":["https://champions-of-hope.funraise.org/fundraiser/mitchell-hanberg"]}},"created_at":"2023-03-11T03:45:47.000Z","updated_at":"2025-03-30T14:39:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"acc1d253-8a84-418c-a87a-7e9c197e0c5e","html_url":"https://github.com/mhanberg/schematic","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhanberg%2Fschematic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhanberg%2Fschematic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhanberg%2Fschematic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhanberg%2Fschematic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mhanberg","download_url":"https://codeload.github.com/mhanberg/schematic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248634967,"owners_count":21137151,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data","elixir","specification","validation"],"created_at":"2024-10-12T09:57:31.835Z","updated_at":"2025-04-12T21:31:15.617Z","avatar_url":"https://github.com/mhanberg.png","language":"Elixir","funding_links":["https://github.com/sponsors/mhanberg","https://champions-of-hope.funraise.org/fundraiser/mitchell-hanberg"],"categories":[],"sub_categories":[],"readme":"# schematic\n\n[![Hex.pm](https://img.shields.io/hexpm/v/schematic)](https://hex.pm/packages/schematic)\n[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/schematic/)\n\n\u003cimg width=\"300px\" src=\"https://user-images.githubusercontent.com/5523984/229656560-e1e96c2c-b51f-481a-b8e3-00127432b20e.png\" alt=\"schematic logo\"\u003e\n\n\u003c!-- MDOC !--\u003e\n\nschematic is a library for data specification, validation, and transformation.\n\nschematic 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.\n\nThere are 12 builtin schematics that you can use to build new schematics that fit your own domain model.\n\n- `bool/0`\n- `str/0`\n- `atom/0`\n- `int/0`\n- `float/0`\n- `list/1`\n- `tuple/1`\n- `map/1`\n- `schema/2`\n- `keyword/1`\n- `raw/2`\n- `any/0`\n- `all/1`\n- `oneof/1`\n\nLiterals can be used as schematics and are unified with `==` semantics.\n\nStruct literals can be used as schematics, and the input is unified by seeing if it is an instance of the given struct.\n\n## Example\n\nLet's take a look at an example schematic for a JSON-RPC request for a bookstore API.\n\n```elixir\ndefmodule Bookstore do\n  defmodule Datetime do\n    import Schematic\n\n    def schematic() do\n      raw(\n        fn\n          i, :to -\u003e is_binary(i) and match?({:ok, _, _}, DateTime.from_iso8601(i))\n          i, :from -\u003e match?(%DateTime{}, i)\n        end,\n        transform: fn\n          i, :to -\u003e\n            {:ok, dt, _} = DateTime.from_iso8601(i)\n            dt\n\n          i, :from -\u003e\n            DateTime.to_iso8601(i)\n        end\n      )\n    end\n  end\n\n  defmodule Author do\n    import Schematic\n\n    defstruct [:name]\n\n    def schematic() do\n      schema(__MODULE__, %{\n        name: str()\n      })\n    end\n  end\n\n  defmodule Book do\n    import Schematic\n\n    defstruct [:title, :authors, :publication_date]\n\n    def schematic() do\n      schema(__MODULE__, %{\n        {\"publicationDate\", :publication_date} =\u003e Bookstore.Datetime.schematic(),\n        title: str(),\n        authors: list(Bookstore.Author.schematic())\n      })\n    end\n  end\n\n  defmodule BooksListResult do\n    import Schematic\n\n    defstruct [:books]\n\n    def schematic() do\n      schema(__MODULE__, %{\n        books: list(Bookstore.Book.schematic())\n      })\n    end\n  end\n\n  defmodule BooksListParams do\n    import Schematic\n\n    defstruct [:query, :order]\n\n    def schematic() do\n      schema(__MODULE__, %{\n        query:\n          nullable(\n            map(%{\n              {\"field\", :field} =\u003e\n                oneof([\"title\", \"authors\", \"publication_date\"]),\n              {\"value\", :value} =\u003e str()\n            })\n          ),\n        order: nullable(oneof([\"asc\", \"desc\"]))\n      })\n    end\n  end\n\n  defmodule BooksList do\n    import Schematic\n\n    defstruct [:id, :method, :params]\n\n    def schematic() do\n      schema(__MODULE__, %{\n        id: int(),\n        method: \"books/list\",\n        params: Bookstore.BooksListParams.schematic()\n      })\n    end\n  end\nend\n```\n\nReading external data into your data model.\n\n```elixir\niex\u003e alias SchematicTest.Bookstore\niex\u003e import Schematic\niex\u003e unify(Bookstore.BooksList.schematic(), %{\n...\u003e   \"id\" =\u003e 99,\n...\u003e   \"method\" =\u003e \"books/list\",\n...\u003e   \"params\" =\u003e %{\n...\u003e     \"query\" =\u003e %{\n...\u003e       \"field\" =\u003e \"authors\",\n...\u003e       \"value\" =\u003e \"Michael Crichton\"\n...\u003e     },\n...\u003e     \"order\" =\u003e \"desc\"\n...\u003e   }\n...\u003e })\n{:ok,\n %Bookstore.BooksList{\n   id: 99,\n   method: \"books/list\",\n   params: %Bookstore.BooksListParams{\n     query: %{field: \"authors\", value: \"Michael Crichton\"},\n     order: \"desc\"\n   }\n }}\n```\n\nDumping your internal data model.\n\n```elixir\niex\u003e alias SchematicTest.Bookstore\niex\u003e import Schematic\niex\u003e dump(Bookstore.BooksListResult.schematic(), %Bookstore.BooksListResult{\n...\u003e   books: [\n...\u003e     %Bookstore.Book{\n...\u003e       title: \"Jurassic Park\",\n...\u003e       authors: [%Bookstore.Author{name: \"Michael Crichton\"}],\n...\u003e       publication_date: ~U[1990-11-20 00:00:00.000000Z]\n...\u003e     },\n...\u003e     %Bookstore.Book{\n...\u003e       title: \"The Lost World\",\n...\u003e       authors: [%Bookstore.Author{name: \"Michael Crichton\"}],\n...\u003e       publication_date: ~U[1995-09-08 00:00:00.000000Z]\n...\u003e     }\n...\u003e   ]\n...\u003e })\n{:ok,\n%{\n  \"books\" =\u003e [\n    %{\n      \"authors\" =\u003e [%{\"name\" =\u003e \"Michael Crichton\"}],\n      \"publicationDate\" =\u003e \"1990-11-20T00:00:00.000000Z\",\n      \"title\" =\u003e \"Jurassic Park\"\n    },\n    %{\n      \"authors\" =\u003e [%{\"name\" =\u003e \"Michael Crichton\"}],\n      \"publicationDate\" =\u003e \"1995-09-08T00:00:00.000000Z\",\n      \"title\" =\u003e \"The Lost World\"\n    }\n  ]\n}}\n```\n\n## Telemetry\n\nschematic fires the following events:\n\n- `[:schematic, :unify, :start]` - Fired when unification starts.\n- `[:schematic, :unify, :stop]` - Fired when unification stops.\n- `[:schematic, :unify, :exception]` - Fired when unification raises an exception.\n\n\u003c!-- MDOC !--\u003e\n\n## Installation\n\n\u003c!-- x-release-please-start-version --\u003e\n```elixir\ndef deps do\n  [\n    {:schematic, \"~\u003e 0.1\"}\n  ]\nend\n```\n\u003c!-- x-release-please-end --\u003e\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at \u003chttps://hexdocs.pm/schematic\u003e.\n\n## License\n\nThe MIT License (MIT)\n\nCopyright © 2023 Mitchell A. Hanberg\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhanberg%2Fschematic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmhanberg%2Fschematic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhanberg%2Fschematic/lists"}