forked from typestack/class-validator
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow validate plain object (typestack#1378)
- Loading branch information
Christian Forgács
committed
Apr 27, 2023
1 parent
6b326d5
commit f1bd4a9
Showing
7 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Contains, IsInt, Length, IsEmail, IsFQDN, IsDate, ValidateNested } from '../../src/decorator/decorators'; | ||
import { Tag } from './Tag'; | ||
|
||
export class Post { | ||
@Length(10, 20, { | ||
message: 'Incorrect length!', | ||
}) | ||
title: string; | ||
|
||
@ValidateNested({ | ||
plain: true, | ||
}) | ||
tags: Record<string, Tag>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Contains, IsInt, Length, IsEmail, IsFQDN, IsDate } from '../../src/decorator/decorators'; | ||
|
||
export class Tag { | ||
@Length(10, 20, { | ||
message: 'Tag value is too short or long', | ||
}) | ||
value: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Validator } from '../../src/validation/Validator'; | ||
import { Post } from './Post'; | ||
import { Tag } from './Tag'; | ||
|
||
let validator = new Validator(); | ||
|
||
let tag1 = new Tag(); | ||
tag1.value = 'ja'; | ||
|
||
let tag2 = new Tag(); | ||
tag2.value = 'node.js'; | ||
|
||
let post1 = new Post(); | ||
post1.title = 'Hello world'; | ||
post1.tags = { | ||
tag1, | ||
tag2, | ||
}; | ||
|
||
validator.validate(post1).then(result => { | ||
console.log('1. should not pass: ', result); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters