quicktype
quicktype copied to clipboard
Improve Typescript validation error messages
NOTE: This is a placeholder issue for an upcoming PR where I will try to implement the suggested improvements.
As a user of the generated typescript type validation code, I would like improved error messages. Specifically:
-
Property keys are printed in some cases, but left out in others. I would like keys to be part of the error message whenever possible.
-
When a value does not have a type matching the requirements, the built-in 'typ' is serialized. For instance an optional string is represented as the enumeration of
undefinedand"". It would be an improvement to have the types in an improved human readable format - like 'Expected the type to be one of undefined, string' - perhaps even with a special case for optional values like 'Expected the type to be an optional string' -
When a property is part of an object and the object type is known, I would like the object type to be part of the error message.
Example: Consider the following data to be parsed as an object type named 'Customer' and the 'name' property to be an optional string:
{
"name": 1234
}
Current error message is:
Invalid value 1234 for type [null,""]
Suggestion for improved error message:
Invalid value 1234 for key 'name' on 'Customer'. Expected an optional string.
+1, hoping this will get merged soon. I'll also add one more request: it would be invaluable if this error message also referenced the faulty line of code (or any other kind stack trace). The existing design of parse errors is severely flawed, so we must always use external tools to debug any root cause. There's very little added value in displaying Invalid value for key "name". Expected type "" but got undefined if we use a more complex JSON schema.