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

YYYY-M-D is incorrectly allowed as a format for dates #685

Closed
DJTommek opened this issue May 6, 2020 · 8 comments · Fixed by #722
Closed

YYYY-M-D is incorrectly allowed as a format for dates #685

DJTommek opened this issue May 6, 2020 · 8 comments · Fixed by #722
Labels
Bug Something doesn't work the way it should.

Comments

@DJTommek
Copy link

DJTommek commented May 6, 2020

Hi,
I'm struggling with validating "date" type. According to specifications it should be in format YYYY-MM-DD (4 digits - 2 digits - 2 digits) but this jsonschema validator is ok with YYYY-M-D.

Example schema snippet:

"d": {
    "type": "string",
    "format": "date"
}

If I test it in https://www.jsonschemavalidator.net/ it properly throws error: String '2000-1-01' does not validate against format 'date'.

I don't think that is expected behaviour.

See live demo here: https://repl.it/@TomasPalider/QueasyDrabServerapplication

I came up with workaround by combination date and regex but I don't like it since schema validation should be strict.

"d": {
    "type": "string",
    "format": "date",
    "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
}
@Julian
Copy link
Member

Julian commented May 7, 2020

Sounds like a bug if so. An upstream test case would be appreciated in the upstream test suite (and of course a PR even more appreciated).

@Julian Julian added Bug Something doesn't work the way it should. Needs Test Upstream Issues that need to have a test added to https://github.com/json-schema-org/JSON-Schema-Test-Suite labels May 7, 2020
@Hubro

This comment has been minimized.

@Julian

This comment has been minimized.

@Hubro

This comment has been minimized.

@DJTommek

This comment has been minimized.

@Hubro

This comment has been minimized.

@Julian Julian changed the title Validator is not strict on date type YYYY-M-D is incorrectly allowed as a format for dates Jul 1, 2020
@helrond
Copy link
Contributor

helrond commented Aug 13, 2020

I am experiencing this issue too, which I believe is occurring because strptime will parse dates which do not have zero-padded months or days.

I've added a PR to the upstream validation library.

I'd also be happy to submit a PR here. As I see it there are two options for replacing the current strptime implementation:

  • Use the built-in fromisoformat() method on datetime.date. The drawback for this is that i t only works on Python 3.7+.
  • Use a regex check (as OP did)

Any preference?

@Julian
Copy link
Member

Julian commented Aug 13, 2020

Awesome! Thank you very much for filing the upstream PR (I saw you already got a review there but yeah you're on the right track).

On implementation... I suppose fromisoformat is the better option, and that on 3.6 we fall back to the current (buggy) behavior.

@Julian Julian removed the Needs Test Upstream Issues that need to have a test added to https://github.com/json-schema-org/JSON-Schema-Test-Suite label Aug 19, 2020
Julian added a commit that referenced this issue Sep 20, 2023
4ecd01f30 Merge pull request #687 from swaeberle/check-single-label-idn-hostnames
732e7275a test single label IDN hostnames
ab3924a66 Merge pull request #685 from swaeberle/check-single-label-hostnames
9265a4fa9 do not test hostname with leading digit for older drafts
261b52db1 do not allow starting digits in hostnames for older drafts
9fc231ea4 test digits in hostnames
e9b20158e test plain single label hostnames
c8b57093d test valid single label hostnames
299aa7fe5 Merge pull request #682 from json-schema-org/useless-branch
fbb3cac60 Bump the sanity check to use a released version of jsonschema

git-subtree-dir: json
git-subtree-split: 4ecd01f30bce36a61224fa0f46c2c3f0cf7481dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something doesn't work the way it should.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants