Low-level Specs for web APIs/form fields that coerce string->type and also generate strings.
deps.edn
:
clj -Sdeps '{:deps {worldsingles/web-specs {:mvn/version "0.1.1"}}}'
Leiningen / Boot Dependency:
[worldsingles/web-specs "0.1.1"]
All the specs defined here accept strings that coerce to a given type, as well as values of that type, and will generate strings that can be coerced to that type.
:ws.web.spec/boolean
-- a spec for Boolean
values:ws.web.spec/long
-- a spec for long
values:ws.web.spec/opt-long
-- a spec for "optional" long
values (nil
and ""
are accepted, nil
will be generated -- but not ""
):ws.web.spec/pos-int
-- a spec for positive integer values (based on pos-int?
):ws.web.spec/age
-- a spec for positive integer values in the range 18..120:ws.web.spec/double
-- a spec for double
values:ws.web.spec/opt-double
-- a spec for "optional" double
values (nil
and ""
are accepted, nil
will be generated -- but not ""
):ws.web.spec/date
-- a spec for date values:ws.web.spec/opt-date
-- a spec for "optional" date values (nil
and ""
are accepted, nil
will be generated -- but not ""
):ws.web.spec/date-time
-- a spec for date time values:ws.web.spec/opt-date-time
-- a spec for "optional" date time values (nil
and ""
are accepted, nil
will be generated -- but not ""
)For dates, the following formats are accepted:
yyyy/M/d
M/d/yyyy
yyyy-M-d
M-d-yyyy
M/d/yy
-- short years are considered 20xxM-d-yy
-- short years are considered 20xxEEE MMM dd HH:mm:ss zzz yyyy
-- e.g., "Fri Sep 20 13:02:00 PDT 2019"
For date times, the format corresponds to ISO 8601: yyyy-MM-ddTHH:mm:ss.SSSZ
-- e.g., "2020-04-05T22:28:12.000Z"
All generated dates are strings of the form MM/dd/yyyy
.
These specs are all built on two macros that wrap low-level coercions and predicates to produce string->type coercing specs:
param-spec
, opt-param-spec
These accept a "coercion function", an optional "stringify" function, and a spec:
:clojure.spec.alpha/invalid
str
In addition, the following "coercions" are defined that accept strings or a given type, and produce :clojure.spec.alpha/invalid
on bad input:
->boolean
, ->long
, ->double
, and ->date
For ->long
, ->double
, and ->date
there are utility coerce->...
functions that accept the given type or a string that they will attempt to coerce to the given type, throwing exceptions on bad input. These are used to build the ->...
coercions but are left public in case they are useful in other contexts.
Finally, there is a convenience function that accepts a comma-separated list of numbers and will coerce that to a vector of long values, or produce :clojure.spec.alpha/invalid
on bad input:
split->longs
0.1.1 -- Jun 09, 2020 -- adds date-time
spec and support functions (PR #2 @juan-ignacio-848)
0.1.0 -- Sep 20, 2019 -- first public release.
Copyright © 2017-2019 World Singles Networks llc.
Distributed under the Eclipse Public License version 1.0.
Can you improve this documentation? These fine people already did:
Sean Corfield & nmkipEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close