Validating plain objects using a class with decorators?
See original GitHub issueThe document says plain objects can be validated using a ValidationSchema, so I think it is possible to enhance the validate to accept a class with decorators to valid plain objects?
For example:
export interface RegisterUserArgs {
username: string;
}
export class RegisterUserArgsSchema implements RegisterUserArgs {
@MinLength(6)
username: string;
}
const args = { username: 'Whoami' }
validate(RegisterUserArgsSchema, args).then(/*...*/);
Comparing to ValidationSchema, define a schema class avoids typos.
I know there is a class-transform-validator package, but it actually validates a new transformed object and thus may leads to additional complexity and performance issues.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
How to validate plain object with class-validator?
How do I validate someObject? Its value is a plain JS object, just some arbitrary JSON data, so it's NOT another DTO. It...
Read more >Next Level Your Typescript Runtime Type Validation Using ...
This article aims to demonstrate a clean and easy way to validate data using Typescript class and class-validator decorators.
Read more >data-transfer-object
Create your own classes by importing and extending DataTransferObject with your own properties, and using the appropriate validator decorators.
Read more >typestack/class-validator - Gitter
When i use await validate(object, { whitelist: true }); ... I was going to create a PR to allow using custom validators to...
Read more >Advanced TypeScript With Code Challenges: Property ...
Decorators provide a way to add annotations for class declarations and members.
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 write a Custom Validator, such as
ValidateNestedAs(Outer)?for example
Of course, you need instances for each class or array. That’s why I recommend using
class-transformerthat has covered all edge cases.