question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validating plain objects using a class with decorators?

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Yaojiancommented, Jun 21, 2018

How about write a Custom Validator, such as ValidateNestedAs(Outer) ?

for example

class Outer {
    @ValidateNestedAs(Inner)
    inner: Inner;
}
0reactions
MichalLytekcommented, Jun 21, 2018

Of course, you need instances for each class or array. That’s why I recommend using class-transformer that has covered all edge cases.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found