-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathclassValidatorToJsonSchema.test.ts
More file actions
53 lines (44 loc) · 1.53 KB
/
classValidatorToJsonSchema.test.ts
File metadata and controls
53 lines (44 loc) · 1.53 KB
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
import { IsString, MinLength } from 'class-validator'
import { classValidatorToJsonSchema, validationMetadatasToSchemas } from '../src'
export class User {
@MinLength(5)
@IsString()
name: string;
}
describe('classValidatorToJsonSchema', () => {
it('handles default object', async () => {
const schema = classValidatorToJsonSchema(User)
expect(schema).toStrictEqual({
properties: {
name: { minLength: 5, type: 'string' }
},
required: ['name'],
type: 'object'
})
// Import another User class.
const alternativeUserImport = await import('./classes/User')
/**
* By importing another User class with the same name JSON schemas get merged.
* User JSON schema now contains properties from the classes/User.ts class as
* well (firstName)
*/
const schemas = validationMetadatasToSchemas()
expect(Boolean(schemas.User!.properties!.name)).toBeTruthy()
expect(Boolean(schemas.User!.properties!.firstName)).toBeTruthy()
/**
* When we get JSON schema for a specific class,
* it returns properties specific for that class (without merging)
*/
const schema2 = classValidatorToJsonSchema(User)
// Schema stays the same
expect(schema).toStrictEqual(schema2)
const alternativeUserSchema = classValidatorToJsonSchema(alternativeUserImport.User)
expect(alternativeUserSchema).toStrictEqual({
properties: {
firstName: { minLength: 5, type: 'string' }
},
required: ['firstName'],
type: 'object'
})
})
})