How to describe validators for two possible types: array or string?
See original GitHub issueHow to describe validators for two possible types: array or string?
Two decorators seems not working.
@IsBoolean()
@IsString()
private readonly foo: boolean | string;
Issue Analytics
- State:
- Created 6 years ago
- Reactions:14
- Comments:17 (2 by maintainers)
Top Results From Across the Web
Multiple Types - JSON Schema
The basic declaration of multiple types is through the "type" keyword, where we can now have an array containing different types. For example,...
Read more >validation value having two possible types - Stack Overflow
public function boot() { Validator::extend('string_or_array', ... by checking type of the variable in order to add the appropriate rule as:
Read more >typestack/class-validator - Gitter
How to describe validators for two possible types : array or string? Two decorators seems not working. @IsBoolean() @IsString() private readonly foo: boolean ......
Read more >Dynamic type validation in TypeScript - LogRocket Blog
In the two other methods, the validator and the type can be seen as different entities: the validator will take the incoming object...
Read more >Types - Superstruct
You can also define your own custom validation functions using the struct ... [1, 2, 3] ... array structs accept a list of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

How about a signature like:
@NoNameProvided Personally i’m against complex logic in decorator definitions. Imagine having repeat those
val => typeof val == 'string'in every property you want to validate. It quickly gets messy. Especially when you will need to change the logic of validating - then you will need to look for all references and change them one by one.I suggest you to implement similar solution to the one i posted here. Or if it’s common to validate multi-typed properties in your project, you can use custom decorator factory to specify which type you want to validate.
And then usage is very simple and clean: