Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] oneOf and empty-string #585

Closed
leberknecht opened this issue Jul 17, 2019 · 2 comments
Closed

[Bug] oneOf and empty-string #585

leberknecht opened this issue Jul 17, 2019 · 2 comments

Comments

@leberknecht
Copy link

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

@Julian
Copy link
Member

Julian commented Jul 17, 2019 via email

@leberknecht
Copy link
Author

Indeed, changing it to

from jsonschema import FormatChecker
[...]
validate(data, schema, format_checker=FormatChecker())

solved it :) Thank you!

Julian added a commit that referenced this issue Aug 17, 2022
14d05dcea Merge pull request #585 from json-schema-org/absolute-uri
b643e11fa Tweak the remotes retrieval README language a bit more.
0c90a5f98 Explicitly mention the base URI for those not using jsonschema_remotes.
79daf05b6 Emit fully absolute URIs from `jsonschema_suite remotes`.

git-subtree-dir: json
git-subtree-split: 14d05dcea95edd138fca251b2a9cf99bd32426bf
Julian added a commit that referenced this issue Aug 17, 2022
4cfead7ba Fix #585 on Windows.

git-subtree-dir: json
git-subtree-split: 4cfead7ba7b1af840eaf95f19a088c89ed67b5a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants