{"id":22641059,"url":"https://github.com/alexanderkiel/spec-coerce","last_synced_at":"2025-06-27T07:35:20.850Z","repository":{"id":141114707,"uuid":"151450756","full_name":"alexanderkiel/spec-coerce","owner":"alexanderkiel","description":"Coercion for Clojure Spec","archived":false,"fork":false,"pushed_at":"2019-10-15T08:22:37.000Z","size":33,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T05:27:43.894Z","etag":null,"topics":["clojure","clojure-spec","coercion"],"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/alexanderkiel.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-03T17:14:03.000Z","updated_at":"2020-06-20T18:55:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"44880c78-44ed-4d2f-9ec4-d1ba4882c7fb","html_url":"https://github.com/alexanderkiel/spec-coerce","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/alexanderkiel/spec-coerce","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexanderkiel%2Fspec-coerce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexanderkiel%2Fspec-coerce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexanderkiel%2Fspec-coerce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexanderkiel%2Fspec-coerce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexanderkiel","download_url":"https://codeload.github.com/alexanderkiel/spec-coerce/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexanderkiel%2Fspec-coerce/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262213146,"owners_count":23276055,"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","clojure-spec","coercion"],"created_at":"2024-12-09T04:17:07.561Z","updated_at":"2025-06-27T07:35:20.836Z","avatar_url":"https://github.com/alexanderkiel.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/alexanderkiel/spec-coerce.svg?branch=master)](https://travis-ci.org/alexanderkiel/spec-coerce)\n\n# Spec Coerce\n\nCoercion for Clojure Spec\n\n## Rationale\n\nIn computing [coercion][1] stands for the conversation of values into a different data type. It can be done implicit or explicit. In Clojure implicit conversations occurs at arithmetic expressions. For example if you add a double to an integer, the integer will be converted into a double before calculating the sum. The result will be a double.\n\n```clojure\n(+ 1 1.0)\n;;=\u003e 2.0\n```\n\nOne the other hand, explicit conversations are done by casting. The next example shows\n\n```clojure\n(double 3)\n;;=\u003e 3.0\n```\n\n\n\nIn Clojure we can use specs to describe constraints a value must conform to. For example we can write a spec which constraints values of the map key `::x` to conform to a double.\n\n```clojure\n(s/def ::x double?)\n``` \n\nNow we can build a mechanism which coerces maps with integer values to doubles.\n\n```clojure\n(coerce (s/keys :req [::x]) {::x 1})\n;;=\u003e {::x 1.0}\n```\n\nThat coercion is still implicit because it only uses the spec of the target and the type of the source to decide which conversation to undertake. Such implicit conversations have to be documented very clearly and this library will do that.\n\nIn most programming languages, an explicit conversation would be a cast. Spec coerce will also support casts by a function, one can supply.\n\n## Install\n\n```clojure\n[org.clojars.akiel/spec-coerce \"0.4.0\"]\n```\n\n## Usage\n\n```clojure\n(require '[spec-coerce.alpha :refer [coerce]])\n\n(coerce int? \"1\")\n;;=\u003e 1\n```\n\n## Implicit Conversations\n\n| Spec       | Source Type | Conversation\n|------------|-------------|-------------\n| `int?`     | string      | `Long/parseLong` / `js/parseInt`\n| `nat-int?` | string      | same as `int?`\n| `pos-int?` | string      | same as `int?`\n\n## Unsupported Spec Forms\n\n### cat\n\nThe `cat` form is unsupported because it's most useful in parsing sequential syntaxes. However such sequences are uncommon in data structures which are used for communication or storage. Spec Coerce concentrates on coercing maps or collections of values following a common spec. Such collections can be spec'ed with the `every` or `coll-of` form. Please file an issue if you have a use case for `cat`.\n\n[1]: \u003chttps://en.wikipedia.org/wiki/Type_conversion\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexanderkiel%2Fspec-coerce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexanderkiel%2Fspec-coerce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexanderkiel%2Fspec-coerce/lists"}