Skip to content

[Bug] oneOf and empty-string  #585

Closed
@leberknecht

Description

@leberknecht

Hi, im struggling with a field that should either have a date or an empty string:

from jsonschema import validate

schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "array",
    "items": {
        "$ref": "#/definitions/MyElement"
    },
    "definitions": {
        "MyElement": {
            "type": "object",
            "properties": {
                "my_date": { "$ref": "#/definitions/MaybeDate" },
            }
        },
        "MaybeDate": {
            "title": "MaybeDate",
            "oneOf": [{
                "type": "string",
                "format": "date"
            }, {
                "const": ""
            }]
        }
    }
}

data = [{
    "my_date": "2018-05-05",
}]

validate(data, schema)

data = [{
    "my_date": "",
}]

validate(data, schema)

Both should be valid, but the empty-string causes

Traceback (most recent call last):
  File "config/scratches/scratch_25.py", line 38, in <module>
    validate(data, schema)
  File "/venv/lib/python3.6/site-packages/jsonschema/validators.py", line 899, in validate
    raise error
jsonschema.exceptions.ValidationError: '' is valid under each of {'const': ''}, {'type': 'string', 'format': 'date'}

Failed validating 'oneOf' in schema['items']['properties']['my_date']:
    {'oneOf': [{'format': 'date', 'type': 'string'}, {'const': ''}],
     'title': 'MaybeDate'}

On instance[0]['my_date']:

This does not happen on https://www.jsonschemavalidator.net/ nor using the nodejs module https://www.npmjs.com/package/jsonschema
Am i missing something?
Using v3.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions