Explanations from schema/check

25 views
Skip to first unread message

Rovanion Luckey

unread,
Apr 28, 2017, 9:48:28 AM4/28/17
to Plumbing and Graph: the Clojure utility belt
I should perhaps begin by saying that I'm a complete newcomer to
Schema and that the issue may simply be that I'm using it wrong.

But it seems to me like `check` could do a better work when it comes
 to explaining cond-pre-schemas:


```clojure
(def int-or-str
  (schema/cond-pre schema/Int schema/Str))
(schema/check int-or-str :a)
; => (not (matches-some-precondition? :a))
```

Instead of simply saying "it matches nothing", perhaps the error
message could say "it doesn't match either X, Y or Z" with X, Y and Z
being the individual preconditions.

Is there some way to get a better error message out of Schema or
can this be considered a feature request?

Jason Wolfe

unread,
Apr 28, 2017, 12:05:17 PM4/28/17
to Rovanion Luckey, Plumbing and Graph: the Clojure utility belt
Hi!  I think we played with doing something like that but we couldn't figure out a good general solution that seemed always better, so we prefer just providing an explicit hint in this case.  

One easy workaround is to use `s/named` to provide a semantic name.  This can also be accomplished with defschema. 

(def int-or-str (s/named (schema/cond-pre schema/Int schema/Str) 'int-or-string?))

or 

(s/defschema int-or-str (schema/cond-pre schema/Int schema/Str))

The other way is to use `s/conditional`.  You have to provide the conditions but this way you can provide the replacement for `matches-some-precondition?` directly (rather than wrapping the above error with a name).

(def int-or-str (s/conditional int? s/Int string? s/Str 'int-or-string?))

Do either of those work for you?

Thanks, 
Jason



 

--
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.

Jason Wolfe

unread,
Apr 28, 2017, 12:07:19 PM4/28/17
to Rovanion Luckey, Plumbing and Graph: the Clojure utility belt
On Fri, Apr 28, 2017 at 9:04 AM, Jason Wolfe <ja...@w01fe.com> wrote:
Hi!  I think we played with doing something like that but we couldn't figure out a good general solution that seemed always better, so we prefer just providing an explicit hint in this case.  

One easy workaround is to use `s/named` to provide a semantic name.  This can also be accomplished with defschema. 

(def int-or-str (s/named (schema/cond-pre schema/Int schema/Str) 'int-or-string?))

or 

(s/defschema int-or-str (schema/cond-pre schema/Int schema/Str))

never mind, I think defschema doesn't do this (it's been awhile) but the other two methods should work. 


The other way is to use `s/conditional`.  You have to provide the conditions but this way you can provide the replacement for `matches-some-precondition?` directly (rather than wrapping the above error with a name).

(def int-or-str (s/conditional int? s/Int string? s/Str 'int-or-string?))

Do either of those work for you?

Thanks, 
Jason



 
On Fri, Apr 28, 2017 at 6:48 AM, Rovanion Luckey <rovanio...@gmail.com> wrote:
I should perhaps begin by saying that I'm a complete newcomer to
Schema and that the issue may simply be that I'm using it wrong.

But it seems to me like `check` could do a better work when it comes
 to explaining cond-pre-schemas:


```clojure
(def int-or-str
  (schema/cond-pre schema/Int schema/Str))
(schema/check int-or-str :a)
; => (not (matches-some-precondition? :a))
```

Instead of simply saying "it matches nothing", perhaps the error
message could say "it doesn't match either X, Y or Z" with X, Y and Z
being the individual preconditions.

Is there some way to get a better error message out of Schema or
can this be considered a feature request?

--
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+unsubscribe@googlegroups.com.

Rovanion Luckey

unread,
May 2, 2017, 8:00:30 AM5/2/17
to Plumbing and Graph: the Clojure utility belt
Do either of those work for you?

Yes, having a name to the error is much better than getting an anonymous "something went wrong somewhere".

 
Reply all
Reply to author
Forward
0 new messages