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

Add datetime to formats of JSONSchema #192

Closed
vipulgupta2048 opened this issue Jul 23, 2019 · 3 comments

Comments

@vipulgupta2048
Copy link
Contributor

commented Jul 23, 2019

Filename - tests/test_validators_jsonschema.py
Line Number - 505
Command - pytest tests/test_validators_jsonschema.py
Testcase in question

      DataTest(
            name="datetime. invalid",
            data={
                "datetimes": [
                    "2015-05-13 13:35:15.718978",
                    "2015-05-13 13:35:15",
                    "13-05-2013",
                ]
            },
            valid=False,
            expected_errors={
                "datetimes.0": [messages.INVALID_DATETIME],
                "datetimes.1": [messages.INVALID_DATETIME],
                "datetimes.2": [messages.INVALID_DATETIME],
            },

Error with this test,

self = <tests.test_validators_jsonschema.Format testMethod=test_datetime_invalid>

    def _function(self):
        validator = JSONSchemaValidator(data_test.schema or self.schema)
>       assert validator.validate(data_test.data) == (
            data_test.valid,
            data_test.expected_errors,
        )
E       AssertionError: assert (True, {}) == (False, {'datetimes.0': ['Invalid datetime']})
E         At index 0 diff: True != False
E         Use -v to get the full diff

test_validators_jsonschema.py:17: AssertionError

Problem - Datetime validation not being done properly due to lack of formats.

Explanation
Now, there is an issue for it in StackOverflow, hence investigating further. I have confirmed that strict-rfc3339==0.7 is in fact being installed with Spidermon.

On further digging, I checked that the validate method does have a format_checker in place. Referring to this line in validator.py of JSONSchema
validator = validator_cls(schema=self._schema, format_checker=format_checker)

Checking the formats.py, finds us with no draft checks for Time, DateTime.

Conclusion - Adding them, since without them DateTime validation with Spidermon JsonSchema won't work.

@vipulgupta2048 vipulgupta2048 changed the title Proposal to modify testcase of datetime from JSONSchema Tests Add datetime to Format for JSONSchema Jul 23, 2019

@vipulgupta2048 vipulgupta2048 changed the title Add datetime to Format for JSONSchema Add datetime to formats of JSONSchema Jul 23, 2019

@vipulgupta2048 vipulgupta2048 changed the title Add datetime to formats of JSONSchema [Bug] Add datetime to formats of JSONSchema Jul 23, 2019

@vipulgupta2048

This comment has been minimized.

Copy link
Contributor Author

commented Jul 23, 2019

I recommend taking a look here for the same, https://github.com/Julian/jsonschema/blob/e4fa34f6517895a81e5ba7e648dc0796f25f9b21/jsonschema/_format.py#L288

Also, I am not sure how other formats work without their format_checker in place. Maybe their test cases haven't been written or they just somehow work.

@rennerocha

This comment has been minimized.

Copy link
Collaborator

commented Jul 31, 2019

@vipulgupta2048 I couldn't get this test failing in a fresh install of spidermon. It is working correctly. These tests weren't updated for a long time and we didn't get errors, so maybe there is something different in your code that is making this error happen.

We also have some (custom format checkers)[https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/validation/jsonschema/formats.py] that extends the existing ones from jsonschema. That is the reason that the other formats are working correctly.

@rennerocha rennerocha changed the title [Bug] Add datetime to formats of JSONSchema Add datetime to formats of JSONSchema Jul 31, 2019

@vipulgupta2048

This comment has been minimized.

Copy link
Contributor Author

commented Jul 31, 2019

I apologize for the false alarm on this one, on a fresh install even I don't see this error popping up. I must have missed something here. Will be more considerate before posting issues next time onwards. Closing!

Regarding custom format checkers, I did check them. I assumed the absence of a DateTime format checker might be the reason for this problem, as also mentioned in the StackOverflow answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.