quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Improve Typescript validation error messages

Open mortenbekditlevsen opened this issue 4 years ago • 1 comments

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:

  1. 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.

  2. 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 undefined and "". 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'

  3. 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.

mortenbekditlevsen avatar Aug 05 '21 07:08 mortenbekditlevsen

+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.

scscgit avatar Aug 01 '22 11:08 scscgit