{"id":20856426,"url":"https://github.com/lob/litmus","last_synced_at":"2025-05-12T07:31:59.675Z","repository":{"id":32805657,"uuid":"141321808","full_name":"lob/litmus","owner":"lob","description":"Data schema validation in Elixir","archived":false,"fork":false,"pushed_at":"2022-03-18T23:40:01.000Z","size":1660,"stargazers_count":34,"open_issues_count":2,"forks_count":4,"subscribers_count":59,"default_branch":"master","last_synced_at":"2025-04-17T13:07:32.012Z","etag":null,"topics":["elixir","elixir-lang","parameters","plug","schema","validation","validator"],"latest_commit_sha":null,"homepage":null,"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/lob.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}},"created_at":"2018-07-17T17:25:23.000Z","updated_at":"2024-03-11T04:33:25.000Z","dependencies_parsed_at":"2022-08-07T18:01:52.806Z","dependency_job_id":null,"html_url":"https://github.com/lob/litmus","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lob%2Flitmus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lob%2Flitmus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lob%2Flitmus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lob%2Flitmus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lob","download_url":"https://codeload.github.com/lob/litmus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253695236,"owners_count":21948845,"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":["elixir","elixir-lang","parameters","plug","schema","validation","validator"],"created_at":"2024-11-18T04:31:31.999Z","updated_at":"2025-05-12T07:31:57.349Z","avatar_url":"https://github.com/lob.png","language":"Elixir","readme":"# Litmus\n\n[![Hex.pm](https://img.shields.io/hexpm/v/litmus.svg)](https://hex.pm/packages/litmus)\n[![Build Docs](https://img.shields.io/badge/hexdocs-release-blue.svg)](https://hexdocs.pm/litmus/Litmus.html)\n[![Build Status](https://travis-ci.org/lob/litmus.svg?branch=master)](https://travis-ci.org/lob/litmus)\n\nData validation in Elixir\n\n## Installation\n\nThe package can be installed by adding `litmus` to your list of dependencies in\n`mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:litmus, \"~\u003e 1.0.2\"}\n  ]\nend\n```\n\n## Usage\n\nLitmus validates data against a predefined schema with the `Litmus.validate/2`\nfunction.\n\nIf the data is valid, the function returns `{:ok, data}`. The data returned\nwill be coerced according to the provided schema.\n\nIf the data passed does not follow the rules defined in the schema, the\nfunction returns `{:error, error_message}`. It will also return an error when\nreceiving a field that has not been specified in the provided schema.\n\n```elixir\nschema = %{\n  \"id\" =\u003e %Litmus.Type.Any{\n    required: true\n  },\n  \"username\" =\u003e %Litmus.Type.String{\n    min_length: 6,\n    required: true\n  },\n  \"pin\" =\u003e %Litmus.Type.Number{\n    min: 1000,\n    max: 9999,\n    required: true\n  },\n  \"new_user\" =\u003e %Litmus.Type.Boolean{\n    truthy: [\"1\"],\n    falsy: [\"0\"]\n   },\n  \"account_ids\" =\u003e %Litmus.Type.List{\n    max_length: 3,\n    type: :number\n  },\n  \"remember_me\" =\u003e %Litmus.Type.Boolean{\n    default: false\n  }\n}\n\nparams = %{\n  \"id\" =\u003e 1,\n  \"username\" =\u003e \"user@123\",\n  \"pin\" =\u003e 1234,\n  \"new_user\" =\u003e \"1\",\n  \"account_ids\" =\u003e [1, 3, 9]\n}\n\nLitmus.validate(params, schema)\n# =\u003e {:ok,\n#      %{\n#        \"id\" =\u003e 1,\n#        \"new_user\" =\u003e true,\n#        \"pin\" =\u003e 1234,\n#        \"username\" =\u003e \"user@123\",\n#        \"account_ids\" =\u003e [1, 3, 9],\n#        \"remember_me\" =\u003e false\n#      }\n#    }\n\nLitmus.validate(%{}, schema)\n# =\u003e {:error, \"id is required\"}\n```\n\n## Supported Types\n\nLitmus currently supports the following types.\n\n* `Litmus.Type.Any`\n* `Litmus.Type.Boolean`\n* `Litmus.Type.DateTime`\n* `Litmus.Type.List`\n* `Litmus.Type.Number`\n* `Litmus.Type.String`\n\n## Plug Integration\n\nLitmus comes with a Plug for easy integration with Plug's built-in router. You can automatically validate query\nparameters and body parameters by passing the `litmus_query` and `litmus_body` private options to each route. When\ndeclaring the plug you must include a `on_error/2` function to be called when validation fails. It is recommended that\nyou initialize this Plug between the `:match` and `:dispatch` plugs. If you want processing to stop on a validation\nerror, be sure to halt the request with `Plug.Conn.halt/1`.\n\n#### Example\n\n```elixir\ndefmodule MyRouter do\n  use Plug.Router\n\n  plug(Plug.Parsers, parsers: [:urlencoded, :multipart])\n\n  plug(:match)\n\n  plug(Litmus.Plug, on_error: \u0026__MODULE__.on_error/2)\n\n  plug(:dispatch)\n\n  @schema %{\n    \"id\" =\u003e %Litmus.Type.Number{\n      required: true\n    }\n  }\n\n  get \"/test\", private: %{litmus_query: @schema} do\n    Plug.Conn.send_resp(conn, 200, \"items\")\n  end\n\n  post \"/test\", private: %{litmus_body: @schema} do\n    Plug.Conn.send_resp(conn, 200, \"items\")\n  end\n\n  def on_error(conn, error_message) do\n    conn\n    |\u003e Plug.Conn.send_resp(400, error_message)\n    |\u003e Plug.Conn.halt()\n  end\nend\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flob%2Flitmus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flob%2Flitmus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flob%2Flitmus/lists"}