plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.41k stars 257 forks source link

Mutually recursive schemas and StackOverflowError #347

Closed jpmonettas closed 8 years ago

jpmonettas commented 8 years ago

I'm trying to define a couple of mutually recursive schemas and getting a StackOverflowError when validating them. Maybe it's I'm not using it correctly.

This is a minimal example that doesn't work for me :

(declare Sch2)

(def Sch1
  {:v s/Int
   (s/optional-key :childs) [(s/either (s/recursive #'Sch1)
                                       (s/recursive #'Sch2))]})

(def Sch2
  {:v s/Str
   (s/optional-key :childs) [(s/either (s/recursive #'Sch1)
                                       (s/recursive #'Sch2))]})

(s/check Sch1 {:v 10
                  :childs [{:v "bar"
                            :childs [{:v 5} {:v "foo"}]}]})

Working with schema 1.0.5 and clojure 1.8

w01fe commented 8 years ago

Thanks for the report. either is deprecated in Schema 1.x and does not work with recursive, try if or conditional instead, e.g.

(declare Sch1 Sch2)

(def Child 
  (s/if #(integer? (:v %)) (s/recursive #'Sch1) (s/recursive #'Sch2)))

(def Sch1
  {:v s/Int
   (s/optional-key :childs) [Child]})

(def Sch2
  {:v s/Str
   (s/optional-key :childs) [Child]})

(s/check Sch1 {:v 10
                  :childs [{:v "bar"
                            :childs [{:v 5} {:v "foo"}]}]})
jpmonettas commented 8 years ago

Like a charm! Thanks a lot, and great library by the way!

w01fe commented 8 years ago

No problem, thanks!

On Fri, Mar 11, 2016 at 2:10 AM, Juan Monetta notifications@github.com wrote:

Closed #347 https://github.com/plumatic/schema/issues/347.

— Reply to this email directly or view it on GitHub https://github.com/plumatic/schema/issues/347#event-585472217.