0

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',
          })
}
  • The error suggests that `@MinLength` is an unexpected token. That means Node.js can't understand the use of decorators. Decorators are not part of the language yet. You can read more about it here: https://stackoverflow.com/questions/48055771/how-do-i-use-and-apply-javascript-decorators – Abrar Hossain Feb 26 '21 at 04:23

2 Answers2

0

I think you should follow their documentation and import MinLength and MaxLength to avoid the Syntax error.

import { MinLength, MaxLength } from 'class-validator';
Rifat Bin Reza
  • 2,601
  • 2
  • 14
  • 29
0

"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.