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

Print out key giving ValidationError #28

Closed
GraemeWatt opened this issue Jun 15, 2021 · 3 comments · Fixed by #31
Closed

Print out key giving ValidationError #28

GraemeWatt opened this issue Jun 15, 2021 · 3 comments · Fixed by #31
Labels

Comments

@GraemeWatt
Copy link
Member

The jsonschema package only prints out the value giving a ValidationError, not the associated key. This is often not informative, for example, if the value is None:

error - None is not of type 'string' in None

arising from a submission.yaml file containing:

data_license: {description: null, name: null, url: null}

This line was originally written by default when converting from the oldhepdata format (see HEPData/hepdata-converter#32), therefore many old submissions are now invalidated. The key giving rise to a ValidationError should be printed in addition to the invalid value. See Handling Validation Errors in the jsonschema docs.

@GraemeWatt GraemeWatt added the bug label Jun 15, 2021
@alisonrclarke
Copy link
Contributor

Pasting the above into one of the test files and stepping through, it looks like the path of the ValidationError gives us the details we need:

['data_license', 'name']

So perhaps we could just add the contents of the path, separated by colons or slashes, to the 'in' part of the message, e.g.

error - None is not of type 'string' in data_license:name:None

For an invalid keyword this could look something like:

'energies' is not one of ['cmenergies', 'observables', 'reactions', 'phrases'] in keywords:2:name:energies

(2 is the index of the item in the list - would we want to modify indices to make them easier to read?)

@GraemeWatt
Copy link
Member Author

Yes, that looks good. A simpler approach might be just to use the string representation (source code) of the ValidationError exception, although it's a bit verbose. I tried replacing message=ve.message + ' in ' + str(ve.instance) with message=str(ve) in this line. The error message for null values in the data_license is:

error - None is not of type 'string'

Failed validating 'type' in schema['properties']['data_license']['properties']['name']:
    {'maxLength': 256, 'type': 'string'}

On instance['data_license']['name']:
    None

For an invalid energies keyword the corresponding message is:

error - 'energies' is not one of ['cmenergies', 'observables', 'reactions', 'phrases']

Failed validating 'enum' in schema['properties']['keywords']['items']['properties']['name']:
    {'enum': ['cmenergies', 'observables', 'reactions', 'phrases'],
     'maxLength': 128,
     'type': 'string'}

On instance['keywords'][3]['name']:
    'energies'

@alisonrclarke
Copy link
Contributor

Draft PR in #31. Sample error messages:

Using a number instead of a string for data_file:

error - 12321 is not of type 'string' in 'data_file' (expected: {'type': 'string'})

Missing license info:

error - None is not of type 'string' in 'data_license.name' (expected: {'type': 'string', 'maxLength': 256})

Invalid cmenergies value (this had to be fudged slightly as it doesn't come directly from the schema):

error - Invalid value (in GeV) for cmenergies: '7000 GeV' in 'keywords[2].name.cmenergies' (expected: {'type': 'number or hyphen-separated range of numbers e.g. 1.7-4.7'})

Do these look OK?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants