{"id":21384788,"url":"https://github.com/daaku/schema","last_synced_at":"2025-03-16T11:43:00.832Z","repository":{"id":62432240,"uuid":"288171639","full_name":"daaku/schema","owner":"daaku","description":"Clojure schema validation and transformation, using simple functions.","archived":false,"fork":false,"pushed_at":"2021-10-28T13:00:39.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T23:41:20.064Z","etag":null,"topics":["clojure","clojurescript","schema","schema-validation","transformation"],"latest_commit_sha":null,"homepage":"https://cljdoc.org/d/daaku/schema","language":"Clojure","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/daaku.png","metadata":{"files":{"readme":"readme.md","changelog":null,"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":"2020-08-17T12:15:32.000Z","updated_at":"2021-10-28T13:00:42.000Z","dependencies_parsed_at":"2022-11-01T21:00:41.386Z","dependency_job_id":null,"html_url":"https://github.com/daaku/schema","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daaku%2Fschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daaku%2Fschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daaku%2Fschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daaku%2Fschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daaku","download_url":"https://codeload.github.com/daaku/schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243864626,"owners_count":20360355,"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":["clojure","clojurescript","schema","schema-validation","transformation"],"created_at":"2024-11-22T11:43:15.225Z","updated_at":"2025-03-16T11:43:00.797Z","avatar_url":"https://github.com/daaku.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"schema\n======\n\nSchema validation and transformation, using simple functions. This is a\nlibrary of boring functions that do mundane validation and\ntransformation. It isn't meant to be a fancy introspective schema\ndefinition language.\n\nThis library is compatible with [Clojure](https://clojure.org/) and\n[ClojureScript](https://clojurescript.org/).\n\n[![Documentation](https://cljdoc.org/badge/daaku/schema)](https://cljdoc.org/d/daaku/schema/CURRENT)\n[![Clojars](https://img.shields.io/clojars/v/daaku/schema.svg)](https://clojars.org/daaku/schema)\n[![Build](https://github.com/daaku/schema/workflows/build/badge.svg)](https://github.com/daaku/schema/actions?query=workflow%3Abuild)\n\nDocumentation: https://cljdoc.org/d/daaku/schema\n\n## Usage\n\nThe building block for `schema` are validator functions. These take a\nvalue, and return one of 3 things:\n\n1. `nil` to indicate the value should be dropped, and the rest of the\n   chain will not be run.\n1. An error using `(error ...)` which indicates an error, and the rest\n   of the chain will not be run.\n1. Any other value, which will be passed to the next validator in the\n   chain, if there is one.\n\nWith this in mind, a simple validator could be:\n\n```clojure\n(ns myapp\n  (:require [daaku.schema :as sc]))\n\n(defn ensure-int [v]\n  (if (int? v)\n    v\n    (sc/error \"not an int\")))\n```\n\nSimilarly, the pattern allows for transformation, for example:\n\n```clojure\n(defn toggle [v]\n  (if (= \"enable\" v)\n    true\n    false))\n```\n\nThe library provide a variety of predicates and transformers. They are\nall written as functions that return a validator, with the given options\n(if any).\n\n### Schema\n\nOf course, you usually want to work with `map`s. So here's how you would\ndo that:\n\n```clojure\n(def countries #{\"India\", \"Belgium\", \"USA\"})\n\n(def address-schema\n  (sc/schema {:street [(sc/required) (sc/string)]\n              :city [(sc/optional) (sc/string)]\n              :country [(sc/required) (sc/in countries)]}))\n\n(def person-schema\n  (sc/schema {:name [(sc/required) (sc/string)]\n              :age [(sc/required) (sc/to-int)]\n              :address [(sc/optional) address-schema]}))\n\n(def samples [{}\n              {:name \"Yoda\" :age \"842\"}\n              {:name \"Yoda\" :age \"old\"}\n              {:name \"Yoda\" :age 842 :address {}}])\n\n(for [input samples]\n  (let [validated (person-schema input)]\n    (if (sc/error? validated)\n      (println \"error: \\n\" (sc/error-value validated) \"\\n for: \\n\" input)\n      (println \"success: \\n\" validated))))\n```\n\n\n## TODO\n\n- [ ] transform: keyword\n- [ ] type: date\n- [ ] type: date time\n- [ ] type: uuid\n- [ ] transform: date\n- [ ] transform: date time\n- [ ] validate: either or key in map\n- [ ] validate: at least one of key in map\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaaku%2Fschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaaku%2Fschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaaku%2Fschema/lists"}