metosin / spec-tools

Clojure(Script) tools for clojure.spec

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

s/pos? & s/neg? produce invalid schema

hipitihop opened this issue · comments

The following Spec

(s/def :my.namespace/sort (s/and number? pos?))

produces:

"sort": {
    "allOf": [
                  {
                    "type": "number",
                    "format": "double"
                  },
                  {
                    "minimum": 0,
                    "exclusiveMinimum": true
                  }
                ]
           }

Suspect:

(defmethod accept-spec 'clojure.core/pos? [_ _ _ _] {:minimum 0 :exclusiveMinimum true})
(defmethod accept-spec 'clojure.core/neg? [_ _ _ _] {:maximum 0 :exclusiveMaximum true})

This causes following errror in python/jsonschema validation on exclusiveMinimum

jsonschema.exceptions.SchemaError: True is not of type 'number'

Note that docs also suggest that both should not be specified:
https://json-schema.org/understanding-json-schema/reference/numeric.html#range

Also note if the Spec is changed to:

(s/def :my.namespace/sort (s/and number? #(> % 0)))

The resulting schema works but omits the extra test by the anon fn. But understandably anon fn can't be represented. Perhaps this should cause a console warning so it doesn't go undetected.

Hi, the generated output is valid for JSON Schema Draft 04.

@miikka Hi, when I specify :json-schema/$schema "http://json-schema.org/draft-04/schema#" it fails with the following but it wasn't immediately obvious which part of the schema it is complaining about.

Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['additionalProperties']:
    {'anyOf': [{'type': 'boolean'}, {'$ref': '#'}], 'default': {}}

As soon as I bump specify "...draft-05/schema#" or later, the validation in python works. Might be a bug in the python implementation. When I get a chance I will try and get a minimal example and report back.