class-validator in Nodejs Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of class-validator in Nodejs without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to use class-validator package in my node project and somehow it is not being recognized. It throws an error

 @MinLength(10, {
    ^

SyntaxError: Invalid or unexpected token

Here is what I’m doing:

    const classValidator = require('class-validator');
    
    module.exports = class Entity{
        @MinLength(10, {
            message: 'Title is too short',
          })
          @MaxLength(50, {
            message: 'Title is too long',
          })
}

Answer

“Decorators are not part of the language yet” as mentioned by @Abrar Hossain in the comments suffices your answer. In JavaScript they’re currently a stage 2 proposal, meaning they should be part of a future update to the language

But reading your code implies that you’re using Decorators for schema validation purposes. You can use other libraries such as yup and joi which are as powerful as class-validator package.

We are here to answer your question about class-validator in Nodejs - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji