jvilk / MakeTypes

Make TypeScript types and proxy objects from example JSON objects. Can use proxy objects to dynamically type check JSON at runtime.
455 stars 45 forks source link

Better error messages when runtime type checking fails #1

Open jvilk opened 7 years ago

jvilk commented 7 years ago

What's a good way to have decent error messages without adding too much overhead / context tracking embedded in the JSON objects?

plievone commented 7 years ago

If you would emit json-schemas, then one could use ajv for runtime error reporting. I like it because then I can use the same schema to (A) validate strictly (throw errors etc, like MakeTypes does), or (B) ensure valid type by assigning defaults to required fields and using type coercion. Usually one needs to filter additional properties with removeAdditional, but sometimes APIs use objects as hashes where index types are needed. In that case it is good to filter out Object.prototype properties at least so that the resulting objects are well-behaving in runtime. So there could be all kinds of intricacies to get json-schemas work, but it might be worth it.