{"id":16003024,"url":"https://github.com/luposlip/json-schema","last_synced_at":"2025-04-12T11:49:06.302Z","repository":{"id":40371111,"uuid":"164465851","full_name":"luposlip/json-schema","owner":"luposlip","description":"Clojure library JSON Schema validation and generation - Draft-07 compatible","archived":false,"fork":false,"pushed_at":"2024-10-08T15:42:40.000Z","size":93,"stargazers_count":73,"open_issues_count":5,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-06T17:11:37.016Z","etag":null,"topics":["clojure-library","data-validator","edn","json-schema","json-validation","validation"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luposlip.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}},"created_at":"2019-01-07T17:16:54.000Z","updated_at":"2025-02-12T06:21:45.000Z","dependencies_parsed_at":"2024-10-25T17:06:50.563Z","dependency_job_id":"6319e9a8-b510-45d7-927a-03a0543f61f7","html_url":"https://github.com/luposlip/json-schema","commit_stats":{"total_commits":80,"total_committers":5,"mean_commits":16.0,"dds":"0.050000000000000044","last_synced_commit":"3e35e80ef75fc0fd6f6cdd816e45a1e19b10eb4e"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luposlip%2Fjson-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luposlip%2Fjson-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luposlip%2Fjson-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luposlip%2Fjson-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luposlip","download_url":"https://codeload.github.com/luposlip/json-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248564798,"owners_count":21125412,"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-library","data-validator","edn","json-schema","json-validation","validation"],"created_at":"2024-10-08T10:05:48.030Z","updated_at":"2025-04-12T11:49:06.271Z","avatar_url":"https://github.com/luposlip.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Clojure CI](https://github.com/luposlip/json-schema/workflows/Clojure%20CI/badge.svg?branch=main) [![Clojars Project](https://img.shields.io/clojars/v/luposlip/json-schema.svg)](https://clojars.org/luposlip/json-schema) [![Dependencies Status](https://versions.deps.co/luposlip/json-schema/status.svg)](https://versions.deps.co/luposlip/json-schema) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Downloads](https://versions.deps.co/luposlip/json-schema/downloads.svg)](https://versions.deps.co/luposlip/json-schema)\n\n# Clojure JSON Schema Validator \u0026 Generator\n\n```clojure\n[luposlip/json-schema \"0.4.6\"]\n```\n\nA Clojure library for:\n- validation of EDN or JSON data according to JSON Schema https://json-schema.org\n- generation of JSON Schemas based on EDN data\n\nSupports up until JSON Schema Draft-07, for validation. Generates Draft-07 Schemas.\n\n## Usage: Validation\n\nThis library can be used to validate data (EDN or JSON strings) based on a JSON Schema.\n\nIt has a single public function, `validate` that return the validated data when no errors are found. This makes it very easy to incorporate validation in a pipeline (see pseudo example below).\n\nAll found errors will cause the function to throw an instance of `clojure.lang.ExceptionInfo`, which can be inspected with the help of `ex-data` and the likes.\n\nIf you're using Clojure 1.7 or newer, you can convert any Throwable to a map via `Throwable-\u003emap`.\n\nTo switch draft versions, simply use the according version notation in the `$schema` uri (in the root of the JSON document):\n\n```\n{\"$schema\": \"http://json-schema.org/draft-04/schema\", ...}\n\nor:\n{\"$schema\": \"http://json-schema.org/draft-06/schema\", ...}\n\nor:\n{\"$schema\": \"http://json-schema.org/draft-07/schema\", ...}\n```\n\nJSON and JSON Schema params has to be input as either a JSON encoded string or EDN (map for both or vector for JSON).\n\nExample usage:\n\n```clojure\n(let [schema {:$schema \"http://json-schema.org/draft-07/schema#\"\n              :id \"https://luposlip.com/some-schema.json\"\n              :type \"object\"\n              :properties {:id {:type \"number\"\n                                :exclusiveMinimum 0}}\n              :required [:id]}\n      json \"{\\\"id\\\": 0.001}\"] ;; get from url, or anywhere\n  (validate schema json)\n  (comment do whatever you only wanna do when JSON is valid)\n  :success)\n```\n\nWith version 0.3.x you can reuse schemas via the [`$ref` attribute](https://json-schema.org/understanding-json-schema/structuring.html#reuse). See `/test/json-schema/core_test.clj#classpath-ref-resolution` for an example.\n\nPseudo-example for pipelining (note the reuse of the prepared schema):\n\n```clojure\n(let [schema (json-schema/prepare-schema\n                (-\u003e \"resources/json-schema.json\"\n                    slurp\n                    (cheshire.core/parse-string true)))]\n  (-\u003e\u003e huge-seq-of-edn-or-jsonstrings\n       (map do-stuff-to-each-doc)\n       (map do-even-more-to-each)\n       (map (partial json-schema/validate schema))\n     (lazily-save-docs-to-disk \"/path/to/output-filename.ndjson\")\n     dorun))\n```\n\nMore usage examples can be seen in the tests.\n\n### Custom Format Validators\n\nCheck the unit test for examples on how to do this.\n\n## Usage: Generation\n\nThe generation of JSON Schemas is pretty naive, but should work in many scenarios. The generated JSON Schemas doesn't use definitions, pointers or references, but is instead nested.\n\nSchema generation is strict possible. This means that all found keys are set as required, and no other keys are allowed. But it's possible to add a `:optional` set of keys, which will be treated as non-required.\n\nPlease note that the explicit optionality will be schema wide, so keys used in different places in the structure having the same name will be treated the same! This might (probably) change in future releases, so you can supply a set keys for global optionality, or path vectors for specificity. A combination with wildcard paths could also be possible in a future release.\n\nTo generate a schema:\n\n```clojure\n(json-schema.infer/infer-strict\n    {:title \"ent-1\"})\n    {:things [{:quantity 1}]})\n```\n\nThis will generate the following schema:\n\n```clojure\n{:$schema \"http://json-schema.org/draft-07/schema#\"\n          :title \"ent-1\"\n          :type :object\n          :additionalProperties false\n          :properties {\"things\" {:type :array\n                                 :items {:type :object\n                                         :additionalProperties false\n                                         :properties {\"quantity\" {:type :integer}}\n                                         :required [\"quantity\"]}}}\n          :required [\"things\"]}\n```\n\n**Implicit optionality** is supported by passing in multiple documents to `infer`:\n\n```clojure\n(json-schema.infer/infer\n    {:title \"ent-1\"}\n    {:things [{:quantity 1}]}\n    {:things [{:quantity 1 :limit 2}]})\n```\n\nor if you have multiple documents pre-ordered in a sequence:\n\n```clojure\n(apply\n    (partial json-schema.infer/infer {:title \"ent-1\"})\n    [{:things [{:quantity 1}]}\n     {:things [{:quantity 1 :limit 2}]}])\n```\n\nIf you want optionality by allowing null values, this is the way:\n\n```clojure\n(json-schema.infer/infer\n    {:title \"ent-1\"\n     :nullable true}\n    {:things [{:quantity 1}]\n     :meta {:foo \"bar}}\n    {:things [{:quantity 1\n               :limit 2}]\n     :meta nil})\n```\n\n**Explicit optionality** is set like this:\n\n```clojure\n(json-schema.infer/infer-strict\n    {:title \"ent-1\"\n     :optional #{:meta}}\n    {:things [{:quantity 1}]\n     :meta 123})\n```\n\nIf you want to allow **additional properties**, set `additional-props` to true:\n\n```clojure\n(json-schema.infer/infer-strict\n    {:title \"ent-1\"\n     :additional-props true}\n    {:things [{:quantity 1}]\n     :meta 123})\n```\n\nThere's a helper function generating the Schema directly to a JSON string:\n\n```clojure\n(json-schema.infer/infer-\u003ejson\n    {:title \"ent-1\"}\n    {:thing {:quantities [1.3 2.2 3.1]}})\n```\n\nMore usage examples can be found in the tests.\n\nNB: In Clojure any data is allowed as key in a map. This is not the case in JSON, where keys can only be string. Because of that all non-string keys are converted to strings in the generated JSON schema. Clojure data with other types of keys will still validate based on the generated schema.\n\n\n### Future\n\nA future release might allow for a configuration map, setting property attributes such as minimum and maximum values for numbers, length constraints for strings etc.\n\n## deps.edn\n\nIf you use `tools.deps` (as opposed to Leiningen), you'll have to copy all dependencies from the Java library manually into `deps.edn`. This is due to [a bug in `tools.deps`](https://dev.clojure.org/jira/browse/TDEPS-46). Refer to [issue #1](https://github.com/luposlip/json-schema/issues/1) for more information.\n\n## Thanks\n\nTo the maintainers of: https://github.com/everit-org/json-schema, on which _validation_ in this Clojure Library is based.\n\nTo the contributors!\n\n## Copyright \u0026 License\n\nCopyright (C) 2020-2024 Henrik Mohr\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n            http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluposlip%2Fjson-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluposlip%2Fjson-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluposlip%2Fjson-schema/lists"}