Creating record constructors that check for schema in Clojure

39 views Asked by At

Hello fellow Clojurians,

it is known that Clojure's defrecords do not respect type hints and any type of data can be put in record fields. I am looking for a way to restrict the fields of records in Clojure.

I started experimenting with the defrecord call of Prismatic Schema.

(require '[schema.core :as sc])
(sc/defrecord Item [size :- Long, colour  :- String])

Now calling (->Item 1 2) just returns a new Item instance despite the second argument is a number instead of a string. I want the constructor call to throw some exception.

I have tried the followings to turn on validation on record instantiation without success.

  • adding ^:always-validate metadata to the defrecord
  • calling (sc/set-fn-validation! true)
  • calling (sc/with-fn-validation (->Item 1 1))

Is it possible to turn on schema validation on constructor calls without writing custom constructors?

Thank you for your help.

0

There are 0 answers