Re: Prismatic Schema: Defining a schema for key-value pairs

2016-01-16 Thread JvJ
That is exactly what I am looking for!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Prismatic Schema: Defining a schema for key-value pairs

2016-01-16 Thread Jason Wolfe
Schemas are "path-independent", so you have to do this at the level of the 
map.  The easiest way would be to use s/constrained with a predicate, e.g.,

user> (def my-map (s/constrained {Class Object} (fn matching-kvs? [m] 
(every? #(isa? (type (val %)) (key %)) m
#'user/my-map
user> (s/validate my-map {Number 1 String "asdf"})
{java.lang.Number 1, java.lang.String "asdf"}
user> (s/validate my-map {Number 1 "String" "asdf"})
ExceptionInfo Value does not match schema: {(not (instance? java.lang.Class 
"String")) invalid-key}  schema.core/validator/fn--612 (core.clj:151)
user> (s/validate my-map {Number "1" String "asdf"})
ExceptionInfo Value does not match schema: (not (user/matching-kvs?--25671 
a-clojure.lang.PersistentArrayMap))  schema.core/validator/fn--612 
(core.clj:151)

Does that do what you want? 


On Friday, January 15, 2016 at 7:04:08 PM UTC, JvJ wrote:
>
>
> I've recently started using prismatic schema, and I'm attempting to define 
> a schema for a map data structure.
>
> I understand that it is possible to use a schema to define the types of 
> both keys and values in the map, like so:
>
> (def my-map {key-schema val-schema})
>
>
> However, I would like to use the schema to enforce certain relationships 
> between the keys and values.  In particular, this one:
>
> (fn [[k v]]
>(isa? (type v) k))
>
>
> Is it possible to define an associative map schema that applies a 
> predicate to key-value pairs rather than just keys and values separately?
>
> Thanks
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Prismatic Schema: Defining a schema for key-value pairs

2016-01-16 Thread JvJ
Do you know if there's a way to identify the particular key that failed in 
the error message?

On Saturday, 16 January 2016 02:01:49 UTC-8, JvJ wrote:
>
> That is exactly what I am looking for!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Prismatic Schema: Defining a schema for key-value pairs

2016-01-16 Thread Jason Wolfe
Currently, you would have to define your own schema type to control the 
error messages: 

https://github.com/plumatic/schema/wiki/Defining-New-Schema-Types-1.0

If you want to submit an issue, we can look into ways to make this simpler.

Thanks!

On Saturday, January 16, 2016 at 11:52:20 PM UTC, JvJ wrote:
>
> Do you know if there's a way to identify the particular key that failed in 
> the error message?
>
> On Saturday, 16 January 2016 02:01:49 UTC-8, JvJ wrote:
>>
>> That is exactly what I am looking for!
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Prismatic Schema: Defining a schema for key-value pairs

2016-01-15 Thread JvJ

I've recently started using prismatic schema, and I'm attempting to define 
a schema for a map data structure.

I understand that it is possible to use a schema to define the types of 
both keys and values in the map, like so:

(def my-map {key-schema val-schema})


However, I would like to use the schema to enforce certain relationships 
between the keys and values.  In particular, this one:

(fn [[k v]]
   (isa? (type v) k))


Is it possible to define an associative map schema that applies a predicate 
to key-value pairs rather than just keys and values separately?

Thanks

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.