Class Validator Rule

tslint rule for class-validator
Alternatives To Class Validator Rule
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Typescript Standard3016334 years ago44March 07, 2018otherTypeScript
Zero-configuration TypeScript 2 & 3 Standard Validation
Tslint Angular Security18
5 years ago5November 30, 2018TypeScript
TSLint rules for Angular
Class Validator Rule9
3 years agoTypeScript
tslint rule for class-validator
Tslint.tmbundle2
6 years agobsd-3-clauseTypeScript
Integrates the TSLint TypeScript validator with TextMate
Tslint Import Rules2
4 years agomitTypeScript
Set of TSLint rules that help validate proper imports.
Alternatives To Class Validator Rule
Select To Compare


Alternative Project Comparisons
Readme

class-validator-rule

Strongly typed validation

It is possible to do strongly typed validation with class-validator.

Example

This class

enum Color { Red, Green, Blue }
class SomeObject {
    string?: string;
}
class Model {
    color: Color;
    string: string;
    number: number;
    boolean: boolean;
    date: Date;

    stringArray: string[];
    numberArray: number[];
    booleanArray: boolean[];
    dateArray: Date[];
    enumArray: Color[];

    object: SomeObject;
    objectArray: SomeObject[];
}

Should decorate like:

enum Color { Red, Green, Blue }
class SomeObject {
    @IsOptional()
    @IsString()
    string?: string;
}
class Model {
    @IsEnum(Color)
    color: Color;
    @IsString()
    string: string;
    @IsNumber()
    number: number;
    @IsBoolean()
    boolean: boolean;
    @IsDate()
    date: Date;

    @IsString({ each: true })
    stringArray: string[];
    @IsNumber({},{ each: true })
    numberArray: number[];
    @IsBoolean({ each: true })
    booleanArray: boolean[];
    @IsDate({ each: true })
    dateArray: Date[];
    @IsEnum(Color,{ each: true })
    enumArray: Color[];

    @ValidateNested()
    object: SomeObject;
    @ValidateNested({ each: true })
    objectArray: SomeObject[];
}

This rule reminds you to add the correct decorator and can fix your models.

Get started

Install rule with dev flag:

npm i tslint-class-validator-rule -D

tslint configuration

Add tslint configoration to root of your models folder

{
    "rulesDirectory": ["tslint-class-validator-rule"],
    "rules": {
        "no-property-without-decorator":true
    }
}

Fix

tslint -p . --fix 

Add middleware (nestjs)

 app.useGlobalPipes(new ValidationPipe({ forbidUnknownValues: true }));

TODO

- Add imports
- file/folder name option, to support '**/*model.ts'
Popular Tslint Projects
Popular Validation Projects
Popular Code Quality Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Typescript
Validator
Validation
Nestjs
Tslint