Open laruellef opened 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:
SchemaError
instance with the data structure being validated, and a tuple of keys specifying where the schema is not satisfied.formatter
function parameter to the schema with a specified API for formatting error messages.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!
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("'")
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
This looks trivial with the example above, however with a large and heavily nested object, not so!