-
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 object literal (#1378)
- Loading branch information
Christian Forgács
committed
Apr 28, 2023
1 parent
e2a6f5d
commit 568f41e
Showing
8 changed files
with
230 additions
and
8 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, | ||
objectLiteral: true, | ||
}) | ||
categories: Record<string, unknown>; | ||
|
||
@ValidateNested({ | ||
objectLiteral: 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