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

schemas: format keyword does nothing #1363

Closed
jacquerie opened this issue Jul 21, 2016 · 8 comments
Closed

schemas: format keyword does nothing #1363

jacquerie opened this issue Jul 21, 2016 · 8 comments

Comments

@jacquerie
Copy link
Contributor

jacquerie commented Jul 21, 2016

Oh, this is going to be fun.

In [1]: from jsonschema import validate

In [2]: schema = {
   ...:     'format': 'uri',
   ...:     'type': 'string',
   ...: }

In [3]: validate('  ', schema)

In [4]: validate(42, schema)
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-6-1b316f1498c4> in <module>()
----> 1 validate(42, schema)

/Users/jacquerie/Code/jsonschema/jsonschema/validators.py in validate(instance, schema, cls, *args, **kwargs)
    476         cls = validator_for(schema)
    477     cls.check_schema(schema)
--> 478     cls(schema, *args, **kwargs).validate(instance)

/Users/jacquerie/Code/jsonschema/jsonschema/validators.py in validate(self, *args, **kwargs)
    121         def validate(self, *args, **kwargs):
    122             for error in self.iter_errors(*args, **kwargs):
--> 123                 raise error
    124
    125         def is_type(self, instance, type):

ValidationError: 42 is not of type 'string'

Failed validating 'type' in schema:
    {'format': 'uri', 'type': 'string'}

On instance:
    42

In fact this is documented by the implementation of JSONSchema we use: https://github.com/Julian/jsonschema/blob/37a7bb96cacb7db6e589bdb054e49e62c4367a7e/jsonschema/_format.py#L13-L15

One could define FormatCheckers that implement these format checks, but then our schemas are not portable, not even between Python and JavaScript. So we should replace all them with patterns, which do work in JSONSchema and are standard JavaScript regular expressions.

@kaplun
Copy link
Contributor

kaplun commented Jul 22, 2016

No format enforced means we can actually enforce all formats 💃 What if we actually do that? In this way we can e.g. enforce a date format to actually validate if the date is correct. Or an ISBN format that checks the checksum, same thing for ORCID. The number of these convention we need to implement for INSPIRE is not large, and would make validation great again.

For @harunurhan's record-editor we could then have the client code to provide a mapping between formats and validation functions...

@jacquerie
Copy link
Contributor Author

jacquerie commented Jul 22, 2016

I forgot to add that invenio-records does not currently support FormatCheckers being passed to validators: https://github.com/inveniosoftware/invenio-records/blob/aab3b024d0078ccd36334e1d2faae02d078c8882/invenio_records/ext.py#L49-L57.

@kaplun
Copy link
Contributor

kaplun commented Jul 22, 2016

We can propose an extension then. Would you mind opening an Issue in the repo? I can very see it can be implemented in a non disruptive way that could be easily get integrated.

@harunurhan
Copy link
Contributor

@kaplun

I think we should always have pattern where constraint can be described as regexp, because having format means that we should maintain the code that produces always same results in two places, in two different languages, so it is better to have as few as possible.

However, if it can not be described as regexp like isbn's checksum thingy then we should use format since we have no other option 😄

@jacquerie
Copy link
Contributor Author

Would you mind opening an Issue in the repo?

inveniosoftware/invenio-records#142

@jacquerie
Copy link
Contributor Author

Solution coming in inveniosoftware/invenio-records#144

@jacquerie
Copy link
Contributor Author

jacquerie commented Jul 27, 2016

inveniosoftware/invenio-records#144 has been merged, so now the ball is on us to write a FormatChecker for our use case (and maybe try to get a release for invenio-records).

@jacquerie jacquerie self-assigned this Aug 6, 2017
@jacquerie jacquerie removed their assignment Sep 29, 2017
@StellaCh StellaCh added this to the Review feature requests milestone Nov 21, 2017
@jacquerie
Copy link
Contributor Author

Formats are validated thanks to work from @michamos in inspire-schemas.

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

4 participants