-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from exceptionless/main
Fixed Class Validator using development debug messages
- Loading branch information
Showing
2 changed files
with
75 additions
and
9 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 |
---|---|---|
|
@@ -6,15 +6,24 @@ import { | |
IsEmail, | ||
IsInt, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsString, | ||
IsUUID, | ||
Min, | ||
ValidateNested, | ||
} from 'class-validator'; | ||
import {expectTypeOf} from 'expect-type'; | ||
import {describe, expect, test} from 'vitest'; | ||
|
||
import {assert, validate, wrap} from '..'; | ||
|
||
describe('class-validator', () => { | ||
class NestedSchema { | ||
@IsString() | ||
@IsNotEmpty() | ||
value!: string; | ||
} | ||
|
||
class Schema { | ||
@IsInt() | ||
@Min(0) | ||
|
@@ -34,6 +43,12 @@ describe('class-validator', () => { | |
|
||
@IsDateString() | ||
updatedAt!: string; | ||
|
||
@ValidateNested() | ||
nested!: NestedSchema; | ||
@IsOptional() | ||
@ValidateNested() | ||
nestedArray?: NestedSchema[]; | ||
} | ||
const schema = Schema; | ||
|
||
|
@@ -44,14 +59,26 @@ describe('class-validator', () => { | |
id: 'c4a760a8-dbcf-4e14-9f39-645a8e933d74', | ||
name: 'John Doe', | ||
updatedAt: '2021-01-01T00:00:00.000Z', | ||
}; | ||
} as Schema; | ||
|
||
const badData = { | ||
age: '123', | ||
createdAt: '2021-01-01T00:00:00.000Z', | ||
email: '[email protected]', | ||
id: 'c4a760a8-dbcf-4e14-9f39-645a8e933d74', | ||
name: 'John Doe', | ||
updatedAt: '2021-01-01T00:00:00.000Z' | ||
}; | ||
|
||
const badNestedData = { | ||
age: 123, | ||
createdAt: '2021-01-01T00:00:00.000Z', | ||
email: '[email protected]', | ||
id: 'c4a760a8-dbcf-4e14-9f39-645a8e933d74', | ||
name: 'John Doe', | ||
updatedAt: '2021-01-01T00:00:00.000Z', | ||
nested: new NestedSchema(), | ||
nestedArray: [new NestedSchema()], | ||
}; | ||
|
||
test('infer', () => { | ||
|
@@ -67,11 +94,35 @@ describe('class-validator', () => { | |
expect(await validate(schema, badData)).toStrictEqual({ | ||
issues: [ | ||
{ | ||
message: `An instance of Schema has failed the validation: | ||
- property age has failed the following constraints: min, isInt | ||
`, | ||
message: 'age must not be less than 0', | ||
path: ['age'], | ||
}, | ||
{ | ||
message: 'age must be an integer number', | ||
path: ['age'], | ||
} | ||
], | ||
success: false, | ||
}); | ||
|
||
expect(await validate(schema, badNestedData)).toStrictEqual({ | ||
issues: [ | ||
{ | ||
message: 'value should not be empty', | ||
path: ['nested.value'], | ||
}, | ||
{ | ||
message: 'value must be a string', | ||
path: ['nested.value'], | ||
}, | ||
{ | ||
message: 'value should not be empty', | ||
path: ['nestedArray[0].value'], | ||
}, | ||
{ | ||
message: 'value must be a string', | ||
path: ['nestedArray[0].value'], | ||
} | ||
], | ||
success: false, | ||
}); | ||
|
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