2. Schema-tools & tricks
& quick clojure.spec intro
Clojure Finland 10.3.2016 22.6.2016
Tommi Reiman / Metosin
@ikitommi
3. Schema
âą https://github.com/plumatic/schema
âą Structural data validation library for Clojure(Script)
â Based on predicates, (almost) just data
âą Runtime type-checking
âą Documentation for data & functions
âą Recursive transformations with coercion and matchers
âą Macros for defining schematized functions
âą Data generation & completion
âą DEMO: basic syntax
4. Schema is real life projects (~all of ours)
âą (Optionally) annotate your domain functions and values
with Schema
â Schema def , defschema & defn
â Plumbing defnk , fnk & letk
âą Enable schema checking for tests (catching 90% of type
bugs)
âą Turn off optional validation in production
âą Force coercion when needed / at the system boundaries
âą Data coercion between contexts
5. Schemas in web development
âą Schema coercions when reading from external sources (JSON, XML,
db, âŠ)
âą Schema-based remote apis
â Ring-swagger (https://github.com/metosin/ring-swagger)
â Used by compojure-api,fnhouse-swagger, kekkonen, pedestal-api,yada
âą Schemas for the ClojureScript frontend
â Code sharing via cljc (common domain: both data & function to operate on âem)
â schema-tools for combining ui-views & server-views
âą End2end schemas for Clojure(Script)?
â Dynamic Schemas over the wire withTransit (allows only partial validation)
â Resolve the expression problem (new wire-formats, new types)
9. Schema takeaways
âą Schema is awesome for runtime structural validation â for both
system boundaries & tests
âą Noise-free syntax with schematized data & function definitions
âą Has been de facto in Clojure web (api) development
âą Mostly just data, enabling cool things like declarative
transformations
11. What is
âą a new Integrated system for specification and testing, shipped with Clojure 1.9
âą Automatic validation, error reporting, test-data & test generation
âą Specs can be written external to the actual functions & data (like core.typed)
âą Build from ground-up, recursive,regexps
âą (Mutable) Global spec registry
âą Form, Conform & Unform, multi-spec
âą Function specs on :args,:ret and :fn(!)
âą Opinions
â Maps should be open
â Namespaced keywords
â Verification via (generative) tests
âą Currently is not
â A runtime validation system
â In alpha6, the :ret and :fn was removed from standard instrumentation,hmph.
15. Coercion?
âą Conformers live
within the spec (vs
external with
Schema)
âą How can I do custom
conform based on
external info (e.g.
protocol used)
18. More info about spec
âą https://clojure.org/guides/spec
âą Slack #clojure-spec (both Rich & Alex seem to hang
out there)
âą Changes in the language
â Lotâs of new predicates in clojure.core
â Namespaced maps http://dev.clojure.org/jira/browse/CLJ-1910
â Destructuring http://dev.clojure.org/jira/browse/CLJ-1919
21. Differences
âą Spec has better support for language level
features like multi-methods and higher order
functions
âą Schema supports closed maps & coercion
âą Both support data generation & generative
tests
âą Schema is not found by Rich Hickey
22. Specs for the web, today?
âą Played with a idea to combine the two:
â Convert a pragmatic subset of spec predicates into Schema Predicates
âą integer? => s/Int
âą boolean? => s/Bool
âą (and integer? pos?) => (s/constrained s/Int pos?)
â Use Schema as before (web-libs, snappy coercion etc.)
Ăš https://github.com/metosin/spec-tools & integrate into current apis
23. My 2 cents
âą Schema is currently much better for runtime validation (at borders)
â Support (api-docs, api-libs), coercions, performance, low ceremony
âą Spec will become de facto way for specifying Clojure systems as it will be
part of the core
â Under active development, letâs enjoy the ride!
â Better runtime validation support via 3rd
party libs
âą Low ceremony function & data definitions via extra macros
âą Coercions(!)
â Tooling support (IDEs, api-libs)
âą At least for now, the can co-existâŠ
Ăš TODAY: Schema for the boundaries, more spec when things freeze down