rhys-vdw / ts-auto-guard

Generate type guard functions from TypeScript interfaces
MIT License
505 stars 54 forks source link

isFoo Functions should return an error message about which fields are missing or of wrong type #185

Open juliandm opened 2 years ago

juliandm commented 2 years ago

currently the generated functions just return a Boolean value. However in cases where ts-auto-guard is used for user feedback, a simple yes or no is not sufficient. A path to the faulty attributes would be better

{
 "isWellDefined": true
}

success response

{
 "isWellDefined": false,
 "faultyPaths": [".first.second.third", ".second.foo.bar"]
}

faulty example, could be even more specific about, which type was expected etc..

juliandm commented 2 years ago

Although i like the concept of ts-auto-guard i solved it now by combining two other more standardised open source projects:

  1. generate a json schema of your ts types with https://github.com/vega/ts-json-schema-generator
  2. put this schema into ajv schema validator https://github.com/ajv-validator/ajv

works beautifully and also template literals and other advanced features of ts are worked on.

rhys-vdw commented 2 years ago

@juliandm you can use debug mode to information about the first field that failed, however it is not intended for production.

This library is not a validation library, it generates type guards, which conventionally do not give more information other than is or is not a type.

That said when I first started working on this I did intend to create an option to generate validators, which is why it needs type-guard in the comment annotation (so that I could later add type-validator etc.) But if you're looking for runtime type validator you're better off using a library that explicitly has that as one of its goals.

I'll leave this issue open for now in case there's any more discussion.