-
Notifications
You must be signed in to change notification settings - Fork 32
/
classTransformer.test.ts
97 lines (87 loc) · 2.35 KB
/
classTransformer.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// tslint:disable:no-submodule-imports
import { Type } from 'class-transformer'
import {
ArrayMinSize,
getFromContainer,
IsOptional,
IsString,
MetadataStorage,
ValidateNested,
} from 'class-validator'
import { validationMetadatasToSchemas } from '../src'
const { defaultMetadataStorage } = require('class-transformer/cjs/storage')
class ValidationError {
@IsString({ each: true })
path: string[]
@IsString({ each: true })
constraints: string[]
}
export class ValidationErrorModel {
@IsString() name: 'ValidationError'
@Type(() => ValidationError)
@ValidateNested({ each: true })
errorList: ValidationError[]
@ArrayMinSize(1)
@IsOptional()
@ValidateNested({ each: true })
@Type(() => ValidationError)
anotherErrorList: ValidationError[]
}
describe('class-transformer compatibility', () => {
it('ignores @Type decorator when classTransformerMetadataStorage option is not defined', () => {
// @ts-ignore
const metadata = getFromContainer(MetadataStorage).validationMetadatas
const schemas = validationMetadatasToSchemas()
expect(schemas.ValidationErrorModel).toEqual({
properties: {
anotherErrorList: {
items: {
$ref: '#/definitions/Array',
},
minItems: 1,
type: 'array',
},
errorList: {
items: {
$ref: '#/definitions/Array',
},
type: 'array',
},
name: {
type: 'string',
},
},
required: ['name', 'errorList'],
type: 'object',
})
})
it('applies @Type decorator when classTransformerMetadataStorage option is defined', () => {
// @ts-ignore
const metadata = getFromContainer(MetadataStorage).validationMetadatas
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
})
expect(schemas.ValidationErrorModel).toEqual({
properties: {
anotherErrorList: {
items: {
$ref: '#/definitions/ValidationError',
},
minItems: 1,
type: 'array',
},
errorList: {
items: {
$ref: '#/definitions/ValidationError',
},
type: 'array',
},
name: {
type: 'string',
},
},
required: ['name', 'errorList'],
type: 'object',
})
})
})