{"id":405,"date":"2014-01-27T11:34:57","date_gmt":"2014-01-27T08:34:57","guid":{"rendered":"http:\/\/www.suuronen.eu\/blog\/?p=405"},"modified":"2014-01-27T11:45:10","modified_gmt":"2014-01-27T08:45:10","slug":"clojure-schema-validation","status":"publish","type":"post","link":"https:\/\/suuronen.eu\/blog\/clojure-schema-validation\/","title":{"rendered":"Clojure Schema validation"},"content":{"rendered":"<p><a title=\"Schema\" href=\"https:\/\/github.com\/prismatic\/schema\" target=\"_blank\">Schema<\/a> is a Clojure library for declarative data description and validation. Basically it will throw exception when data is not what it is supposed to be.<\/p>\n<p>Couldn&#8217;t find by googling a way to enable date to be either existing or not known (null). So when I figured it out, I wanted to share solution with everybody. The example requires that the person has a name (String), but birthdate (Date) is not required.<\/p>\n<pre lang=\"clojure\" line=\"1\">\r\n(:require [schema.core :as s])\r\n\r\n(defn person\r\n  [name born]\r\n  (s\/validate s\/Str name)\r\n  (s\/validate (s\/either java.util.Date (s\/pred nil?)) born)\r\n  {:name name\r\n   :born born\r\n  })\r\n<\/pre>\n<p>Oh, and it is also possible to combine different types like Integers and Strings for enum validation:<\/p>\n<pre lang=\"clojure\" line=\"1\">\r\n(:require [schema.core :as s])\r\n\r\n(defn person\r\n  [name hands]\r\n  (s\/validate s\/Str name)\r\n  (s\/validate (s\/enum 1 2 \"none\") hands)\r\n  {:name name\r\n   :hands hands\r\n  })\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Schema is a Clojure library for declarative data description and validation. Basically it will throw exception when data is not what it is supposed to be. Couldn&#8217;t find by googling a way to enable date to be either existing or &hellip; <a href=\"https:\/\/suuronen.eu\/blog\/clojure-schema-validation\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[26],"class_list":["post-405","post","type-post","status-publish","format-standard","hentry","category-coding","tag-clojure"],"_links":{"self":[{"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/posts\/405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/comments?post=405"}],"version-history":[{"count":8,"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/posts\/405\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suuronen.eu\/blog\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}