plumatic / schema

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

`optional` doesn't work at the end of a sequence #289

Closed mbossenbroek closed 8 years ago

mbossenbroek commented 9 years ago

This works:

=> (schema/validate
     [(schema/optional [schema/Keyword] "Optional")
      (schema/either schema/Keyword schema/Symbol)]
     [[:baz] :foo 'bar])
[[:baz] :foo bar]

But this doesn't:

=> (schema/validate
     [(schema/either schema/Keyword schema/Symbol)
      (schema/optional [schema/Keyword] "Optional")]
     [:foo 'bar [:baz]])
RuntimeException Sequence schema [(either Keyword Symbol) #schema.core.One{:schema [Keyword], :optional? true, :name "Optional"}] does not match [one* optional* rest-schema?]  schema.core/parse-sequence-schema (core.clj:761)
w01fe commented 9 years ago

Sequence schemas must follow the syntax [one* optional* rest-schema?]. This is to allow greedy matching without backtracking. Otherwise, e.g., with the schema you provide, it's ambiguous whether 'bar should be validated against the first schema or the second schema.

mbossenbroek commented 9 years ago

Could you update the docstring for the relevant operators (one & optional) to reflect that limitation? It's also very unclear from the error message that it was an issue with the schema construction itself and not what it was validating.

w01fe commented 9 years ago

Sure, when we can. PR also very welcome. Thanks.

On Wed, Oct 14, 2015, 12:52 PM Matt Bossenbroek notifications@github.com wrote:

Could you update the docstring for the relevant operators (one & optional) to reflect that limitation? It's also very unclear from the error message that it was an issue with the schema construction itself and not what it was validating.

— Reply to this email directly or view it on GitHub https://github.com/Prismatic/schema/issues/289#issuecomment-148134802.

mbossenbroek commented 8 years ago

Thanks! Appreciate it!