typestack / class-validator

Decorator-based property validation for classes.
MIT License
11.02k stars 800 forks source link

question: Class Validator throws ',' expected when I run "tsc" #881

Closed SkullCutter02 closed 3 years ago

SkullCutter02 commented 3 years ago

Description

I'm using TypeORM and Class Validator together. I need to deploy my app to heroku now, so I created a script called "npm build", which runs the command "tsc". However, it shows these errors which made my app unable to deploy. Below are the errors:

Minimal code-snippet showcasing the problem

node_modules/class-validator/types/decorator/common/IsIn.d.ts:6:71 - error TS1005: ',' expected.

6 export declare function isIn(value: unknown, possibleValues: readonly unknown[]): boolean;
                                                                        ~~~~~~~

node_modules/class-validator/types/decorator/common/IsIn.d.ts:6:78 - error TS1005: ',' expected.

6 export declare function isIn(value: unknown, possibleValues: readonly unknown[]): boolean;
                                                                               ~

node_modules/class-validator/types/decorator/common/IsIn.d.ts:10:47 - error TS1005: ',' expected.

10 export declare function IsIn(values: readonly any[], validationOptions?: ValidationOptions): PropertyDecorator;
                                                 ~~~

node_modules/class-validator/types/decorator/common/IsIn.d.ts:10:50 - error TS1005: ',' expected.

10 export declare function IsIn(values: readonly any[], validationOptions?: ValidationOptions): PropertyDecorator;
                                                    ~

node_modules/class-validator/types/decorator/common/IsNotIn.d.ts:6:74 - error TS1005: ',' expected.

6 export declare function isNotIn(value: unknown, possibleValues: readonly unknown[]): boolean;
                                                                           ~~~~~~~

node_modules/class-validator/types/decorator/common/IsNotIn.d.ts:6:81 - error TS1005: ',' expected.

6 export declare function isNotIn(value: unknown, possibleValues: readonly unknown[]): boolean;
                                                                                  ~

node_modules/class-validator/types/decorator/common/IsNotIn.d.ts:10:50 - error TS1005: ',' expected.

10 export declare function IsNotIn(values: readonly any[], validationOptions?: ValidationOptions): PropertyDecorator;
                                                    ~~~

node_modules/class-validator/types/decorator/common/IsNotIn.d.ts:10:53 - error TS1005: ',' expected.

10 export declare function IsNotIn(values: readonly any[], validationOptions?: ValidationOptions): PropertyDecorator;
                                                       ~

Found 8 errors.

Expected behavior

I've tried uninstalling and reinstalling class validator, but in vain. I expect it to have zero errors and my app successfully deployed to heroku.

Actual behavior

What happens is that the errors above are thrown, making me unable to deploy my application.

NoNameProvided commented 3 years ago

You should not compile your node_modules. Add it to the exclude list in your tsconfig.json.

SkullCutter02 commented 3 years ago

You should not compile your node_modules. Add it to the exclude list in your tsconfig.json.

Just added "exclude": ["node_modules"] to tsconfig.json, still same problem.

SkullCutter02 commented 3 years ago

I'd also like to add that if I just run tsc in my terminal, these errors will not log. However, if I did npm run build which executes the command tsc, then it will throw these errors. Very weird

NoNameProvided commented 3 years ago

I am almost 100% sure this is some problem with your build setup. Please provide a minimal, reproducible example otherwise I can guess only.

SkullCutter02 commented 3 years ago

K sorry for the late reply. I got a reproducible example that can be set up easily.

First do npx typeorm init to initialise the project. Update the dependency version with npm install, then install class-validator by doing: npm i class-validator. When the project is initialised, it should have created an entity called User. Add a @MinLength decorator to one of the columns in the user entity.

Afterwards, in package.json, create a script called build: "build": "tsc". Now run npm run build, and the errors will be produced.

vlapo commented 3 years ago

What typescript version do you use? I think you need version ^3.1.3. Maybe try tsc -v command.

SkullCutter02 commented 3 years ago

What typescript version do you use? I think you need version ^3.1.3. Maybe try tsc -v command.

It's version 4.1.3

NoNameProvided commented 3 years ago

I cannot reproduce. After following the provided steps the starter successfully builds for me. It's some problem with your setup.

SuperITMan commented 3 years ago

Dear @NoNameProvided I'm currently working on a framework based on Angular 7 with TypeScript 3.2.4 and I encountered the same issue šŸ˜•

To understand what's is going on, I created the following project https://github.com/SuperITMan/class-validator-ts-issue with a basic example in ng7, ng8 and ng9. ng7 and ng8 do not compile by running npm run tsc, you can find all the information in the README file šŸ˜Š

Could you reopen this issue and investigate it ? I have the feeling it might be related to TypeScript version...

Thanks for your great library! Regards, Alexis

mdolr commented 3 years ago

Hello @SkullCutter02 @SuperITMan, I had the same issue, fixed it by using Typescript v4.0.5 and adding --skipLibCheck or setting skipLibCheck to true in tsconfig.json

Hope this can help you

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.