-
Notifications
You must be signed in to change notification settings - Fork 786
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 objects (#1378)
- Loading branch information
Christian Forgács
committed
Apr 28, 2023
1 parent
279600d
commit 3975774
Showing
8 changed files
with
214 additions
and
7 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
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,20 @@ | ||
import { IsString, Length, ValidateNested } from '../../src/decorator/decorators'; | ||
import { Tag } from './Tag'; | ||
|
||
export class Post { | ||
@Length(10, 20, { | ||
message: 'Incorrect length!', | ||
}) | ||
title: string; | ||
|
||
@IsString({ | ||
each: true, | ||
plain: true, | ||
}) | ||
categories: Record<string, unknown>; | ||
|
||
@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 { Length } 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,26 @@ | ||
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.categories = { | ||
foo: 'bar', | ||
bar: 123, | ||
}; | ||
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