Code Monkey home page Code Monkey logo

spec-coerce's Introduction

Build Status

Spec Coerce

Coercion for Clojure Spec

Rationale

In computing coercion 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.

(+ 1 1.0)
;;=> 2.0

One the other hand, explicit conversations are done by casting. The next example shows

(double 3)
;;=> 3.0

In 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.

(s/def ::x double?)

Now we can build a mechanism which coerces maps with integer values to doubles.

(coerce (s/keys :req [::x]) {::x 1})
;;=> {::x 1.0}

That 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.

In most programming languages, an explicit conversation would be a cast. Spec coerce will also support casts by a function, one can supply.

Install

[org.clojars.akiel/spec-coerce "0.4.0"]

Usage

(require '[spec-coerce.alpha :refer [coerce]])

(coerce int? "1")
;;=> 1

Implicit Conversations

Spec Source Type Conversation
int? string Long/parseLong / js/parseInt
nat-int? string same as int?
pos-int? string same as int?

Unsupported Spec Forms

cat

The 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.

spec-coerce's People

Contributors

alexanderkiel avatar dicanio avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

dicanio

spec-coerce's Issues

Don't Coerce and Conform

Currently we do two things in this lib, we coerce values and we conform them. I decided to go this route because I was implementing something similar to s/conformer. But it would be better to only coerce in this lib.

Double? -> int? example in readme doesn't work

org.clojars.akiel/spec-coerce {:mvn/version "0.4.0"}
org.clojure/spec.alpha {:mvn/version "0.2.176"}

Clojure 1.10.0 ; JVM
(require '[spec-coerce.alpha :refer [coerce]])
(require '[clojure.spec.alpha :as s])

(s/def ::x double?)

(spec-coerce.alpha/coerce (s/keys :req [::x]) {::x 1})
=> :clojure.spec.alpha/invalid

(spec-coerce.alpha/coerce (s/keys :req [::x]) {::x "1"})
=> #:user{:x 1.0}

Support map specs using 'and' or 'or'

What happened:
Calling the coerce function with a map validation spec containing and or or leads to an exception.

Exception:

Syntax error (IllegalArgumentException) compiling at (core.clj:10:1).
No matching clause: :or

Version used:
v0.3

How to reproduce it:

(ns spec-coerce-minimal-example.core
  (:require
    [clojure.spec.alpha :as s]
    [spec-coerce.alpha :refer [coerce]]))

(s/def ::firstname string?)
(s/def ::lastname string?)
(s/def :config/person (s/keys :req-un [(or ::firstname ::lastname)]))

(coerce :config/person {:firstname "Bob"})

Additional information:
The error seems to be within the keys-coercer function in the spec-coerce.alpha namespace starting at line 197. The reducer used by this function only supports a single kind which is :key and lacks support for :or and :and.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.