keleshev / schema

Schema validation just got Pythonic
MIT License
2.88k stars 215 forks source link

More user friendly error messages #147

Open laruellef opened 6 years ago

laruellef commented 6 years ago

Great package! :-) Errors are a bit hard to decipher, esp. when dealing with large json objects... It would be great if the error could pretty print the object and highlight the line where the problem is... eg instead of having "'{'prop': 1} should be instance of 'list'" have

{
   'key': {
       'prop': 1
   }   << should be instance of 'list'
}

This looks trivial with the example above, however with a large and heavily nested object, not so!

JakobGM commented 6 years ago

I just wanted to chime in and say that this is something I too am really interested in!

It would be a cool way to use schema for user configuration validation, that way you could present the schema validation error directly to the user. I have exactly this use case on https://github.com/JakobGM/astrality.

Here are some ideas, with various degrees of feasibility:

That way you could present arbitrary formatting to end users. In my case, I would like to format the dictionary as YAML before showing the error to the user. Populating the SchemaError with aditional metadata is probably the easiest way to go here, that way it is up to the user to catch exceptions and handle them properly. But it would require additional parameters to be passed around in the stack, I think.

Just some ideas, Thanks!

MrBlaise commented 6 years ago

In my use case I would just love to get the key which failed. I am using this now:

def extract_key_from_schema_error(e: SchemaError) -> str:
    return str(e).split(" ")[1].strip("'")