class-validator
class-validator copied to clipboard
feature: treat some function as type assertions
Description
Sometimes I need to use functions directly instead of decorators, e.g. isUUID. Let's imagine the case in typescript where you validate big csv with streams row by row, e.g.
if (!isUUID(id) || seenIds.has(id as string)) {
throw new InvalidFileStructureException();
}
seenIds.add(id as string); // seenIds is of type Set<string>
It would be really convenient if we would be able to replace the line
seenIds.add(id as string);
with
seenIds.add(id)
because it is obvious that isUUID checks if something is a string of correct format.
Proposed solution
Treat some functions like mentioned isUUID as type assertions when it is obvious when the function assert some type, e.g. string