Stacked schema/named + schema/constrained schemas vs validation errors

32 views
Skip to first unread message

László Török

unread,
Sep 27, 2016, 10:26:56 AM9/27/16
to prismatic-plumbing
Hi,

I working with some legacy code and trying to eliminate schema/both type schemas and move towards "composable" schema/contrained based schemas.
Most of the schemas are decorated with s/named to provide friendly error messages for distinct error cases.

Consider the following toy example:

(defn positive [x] (s/named (s/constrained x pos?) "The amount should be positive"))

(defn less-than [x m] (s/named (s/constrained x #(> m %)) (str "The amount should be less than " m)))

(def some-param-schema (-> s/Int (positive) (less-than 10)))

Now the following call:

(schema/check some-param-schema -1)

returns:

(named (named (not (pos? -1)) "The amount should be positive") "The amount should be less than 10")

This is somewhat surprising, as I would expect

(named (not (pos? -1)) "The amount should be positive")

Now, I can certainly "unwrap" the result and propagate the correct error message to the client.

My question is, wether this is part of the public API contract and whether we can safely rely on this behaviour.

Thanks,

Laszlo

Jason Wolfe

unread,
Sep 27, 2016, 2:13:24 PM9/27/16
to László Török, prismatic-plumbing
names are intended to describe the data that matches the schema, to e.g. help see which element in a positional sequence is having trouble.  Thus, all names are expected to appear in the printed error messages.

I wouldn't recommend relying on the precise format of the returned errors. 

You could get nicer (but still schema-style) errors by just dropping the `named`, or providing a third argument to `constrained` that names the predicate in a more descriptive way.  

If you want to try to produce errors for non-programmers, I would probably recommend walking the return value and extracting the/all ValidationError s


and then see which schema failed and use that to generate a message.  

--
You received this message because you are subscribed to the Google Groups "Plumbing and Graph: the Clojure utility belt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prismatic-plumbing+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages