+ All Categories
Home > Technology > Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Date post: 12-Feb-2017
Category:
Upload: metosin-oy
View: 908 times
Download: 0 times
Share this document with a friend
25
Clojure Finland 10.3.2016 Tommi Reiman / Metosin @ikitommi Schema-tools & tricks
Transcript
Page 1: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Clojure Finland 10.3.2016Tommi Reiman / Metosin

@ikitommi

Schema-tools & tricks

Page 2: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Schema-tools & tricks& quick clojure.spec intro

Clojure Finland 10.3.2016 22.6.2016Tommi Reiman / Metosin

@ikitommi

Page 3: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 4: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 5: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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 with Transit (allows only partial validation)– Resolve the expression problem (new wire-formats, new types)

Page 6: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Schema example (with kekkonen.io)

Page 7: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Schema-tools (https://github.com/metosin/schema-tools)

• Selectors, transformers, walkers• Handling of defaults• Coercion helpers

• DEMO: cut & paste models• DEMO: declarative transformations• DEMO: defaults• DEMO: coercion speed (case RegistryX)

Page 8: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Schema-viz (https://github.com/metosin/schema-viz)

Page 9: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 10: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

clojure.spec(a quick overview)

Page 11: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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.

Page 12: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

CODE

Page 13: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Function specs

https://clojure.org/guides/spec

+spec forhigher order functions+spec formacros+multispec formultimethods

Page 14: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Spec for runtime validation?

https://clojure.org/guides/spec

Page 15: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Coercion?

• Conformers live within the spec (vs external with Schema)

• How can I do custom conform based on external info (e.g. protocol used)

Page 16: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Generating data

https://clojure.org/guides/spec

Page 17: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Generative testing

https://clojure.org/guides/spec

Page 18: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 19: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Schema, Spec vs Both?

Page 20: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Performance2-6xfaster

Notoptimizedforperf,yet.http://muhuk.github.io/validation-benchmark/

Page 21: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 22: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 23: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

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

Page 24: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Thanks!

Page 25: Schema tools-and-trics-and-quick-intro-to-clojure-spec-22.6.2016

Links

• Schema https://github.com/plumatic/schema– Schema-tools https://github.com/metosin/schema-tools– Schema-viz https://github.com/metosin/schema-viz– Ring-Swagger https://github.com/metosin/ring-swagger

• Spec http://clojure.org/about/spec– Regular Expressions:

https://en.wikipedia.org/wiki/Regular_expression


Recommended