Prismatic Adds Data Type Coercion to Schema 0.2
Prismatic have added data coercion in the 0.2 release of their Clojure data description library, Schema. The addition of coercion means that the library doesn’t just reject data that has the wrong types, but it can be configured to modify instances to fit the schema.
In Clojure it is idiomatic to use keywords for Map keys, meaning there is often some boilerplate code to perform the conversion when receiving JSON objects. Previously this conversion would have needed to be performed before attempting to validate the request. Now, if your schema defines the keys as keywords, Schema will handle that for you. Of course, you can write your own coercers for your own specific needs. Along with this added capability, Prismatic claims the changes have reduced the time required to validate data by five times.
Schema’s goal, when first released in September, was to “get many of the benefits of type systems in Clojure with less hassle”. When released it seemed it could compete with core.typed, a Clojure library also bringing types to the language. This view was refuted by core.typed’s author Ambrose Bonnaire-Sergeant at the time, arguing that they will in fact complement one another, a view he repeated during a later InfoQ interview on core.typed.
InfoQ: When Schema was originally released, it was suggested a combination of core.typed and Schema could be very powerful. Has your thinking progressed on this idea since then?
I've been excited about gradual types in Clojure since I saw my first Qi program a few years ago, and I think Ambrose is doing a great job of making that a reality. There are a few ways we've talked about that Schema could play well with core.typed, the most interesting of which is probably using Schema as a bridge between core.type-checked code and unchecked code.That said, I'm sad to say that we haven't had the time to explore core.typed in depth yet, so I don't have much more to say on this just yet.
We're still exploring the options. I've read a lot of really great things about simple-check, and I think this should be doable, but we're still trying to understand its implementation and figure out how to incorporate additional constraints into the generation process. There will also probably be a simple generator for pseudo-randomly fleshing out a partial datum, which is something we end up using often in tests.
InfoQ: What further ideas do you have on extracting value from Schema definitions?
Coercions and transformation are extremely powerful and I think we're still figuring out all the applications they will enable. My colleague Dave Golland will be speaking at Clojure West on a new library “fnhouse” that ties together graph and schema to make building web APIs easy. Following this release will be “coax”, which automatically generates Objective C and ClojureScript model classes and client API libraries from fnhouse APIs.
After that, we have lots of crazy ideas, but nothing we're ready to talk about yet.
How Can We Use Our Creative Power and Technological Opportunity to Address the Challenges of the 21st Century?
Gyorgyi Galik Feb 26, 2015