0

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.

erdos
  • 3,135
  • 2
  • 16
  • 27
  • I remembered an answer I wrote to a similar question here https://stackoverflow.com/q/45578607 – glts Oct 06 '17 at 16:00
  • yes, that question is a good answer to this one and covered both plumatic/schema and spec, so I'll make this question redirect to it. ir @erdos doesn't find this useful I'm happy to un-dupe it. – Arthur Ulfeldt Oct 06 '17 at 19:13

0 Answers0