Skip to content

Commit

Permalink
Format __tests__ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
epiphone committed Dec 31, 2020
1 parent a7182eb commit 935f8b6
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 169 deletions.
66 changes: 30 additions & 36 deletions __tests__/additionalProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { defaultMetadataStorage } from 'class-transformer/storage'

class User {
@IsString()
name: string;
name: string
}

// @ts-ignore: not referenced
class Post {
@Type(() => {
return String
})
@MinLength(2, {each: true})
@MinLength(2, { each: true })
userStatus: Map<string, string>
}

// @ts-ignore: not referenced
class PostWidthUsers {
@ValidateNested({each: true})
@ValidateNested({ each: true })
@Type(() => User)
users: Map<string, User>
}
Expand All @@ -31,46 +31,40 @@ describe('classValidatorConverter', () => {
classTransformerMetadataStorage: defaultMetadataStorage,
})
expect(schemas).toEqual({
"User": {
"properties": {
"name": {
"type": "string"
}
User: {
properties: {
name: {
type: 'string',
},
},
"type": "object",
"required": [
"name"
]
type: 'object',
required: ['name'],
},
"Post": {
"properties": {
"userStatus": {
"additionalProperties": {
"minLength": 2,
"type": "string"
Post: {
properties: {
userStatus: {
additionalProperties: {
minLength: 2,
type: 'string',
},
"type": "object"
}
type: 'object',
},
},
"type": "object",
"required": [
"userStatus"
]
type: 'object',
required: ['userStatus'],
},
"PostWidthUsers": {
"properties": {
"users": {
"additionalProperties": {
"$ref": "#/definitions/User"
PostWidthUsers: {
properties: {
users: {
additionalProperties: {
$ref: '#/definitions/User',
},
"type": "object"
}
type: 'object',
},
},
"type": "object",
"required": [
"users"
]
}
type: 'object',
required: ['users'],
},
})
})
})
32 changes: 16 additions & 16 deletions __tests__/classTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IsOptional,
IsString,
MetadataStorage,
ValidateNested
ValidateNested,
} from 'class-validator'

import { validationMetadatasToSchemas } from '../src'
Expand Down Expand Up @@ -44,54 +44,54 @@ describe('class-transformer compatibility', () => {
properties: {
anotherErrorList: {
items: {
$ref: '#/definitions/Array'
$ref: '#/definitions/Array',
},
minItems: 1,
type: 'array'
type: 'array',
},
errorList: {
items: {
$ref: '#/definitions/Array'
$ref: '#/definitions/Array',
},
type: 'array'
type: 'array',
},
name: {
type: 'string'
}
type: 'string',
},
},
required: ['name', 'errorList'],
type: 'object'
type: 'object',
})
})

it('applies @Type decorator when classTransformerMetadataStorage option is defined', () => {
// @ts-ignore
const metadata = getFromContainer(MetadataStorage).validationMetadatas
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage
classTransformerMetadataStorage: defaultMetadataStorage,
})

expect(schemas.ValidationErrorModel).toEqual({
properties: {
anotherErrorList: {
items: {
$ref: '#/definitions/ValidationError'
$ref: '#/definitions/ValidationError',
},
minItems: 1,
type: 'array'
type: 'array',
},
errorList: {
items: {
$ref: '#/definitions/ValidationError'
$ref: '#/definitions/ValidationError',
},
type: 'array'
type: 'array',
},
name: {
type: 'string'
}
type: 'string',
},
},
required: ['name', 'errorList'],
type: 'object'
type: 'object',
})
})
})
24 changes: 12 additions & 12 deletions __tests__/customValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Validate,
ValidationArguments,
ValidatorConstraint,
ValidatorConstraintInterface
ValidatorConstraintInterface,
} from 'class-validator'
import * as _ from 'lodash'

Expand Down Expand Up @@ -35,43 +35,43 @@ class InvalidPost {
describe('custom validation classes', () => {
it('uses property type if no additional converter is supplied', () => {
const schemas = validationMetadatasToSchemas({
classValidatorMetadataStorage: getMetadataStorage()
classValidatorMetadataStorage: getMetadataStorage(),
})
expect(schemas.Post).toEqual({
properties: {
title: { type: 'string' }
title: { type: 'string' },
},
required: ['title'],
type: 'object'
type: 'object',
})

expect(schemas.InvalidPost).toEqual({
properties: {
titleBoolean: { type: 'boolean' },
titleNumber: { type: 'number' }
titleNumber: { type: 'number' },
},
required: ['titleNumber', 'titleBoolean'],
type: 'object'
type: 'object',
})
})

it('uses additionalConverter to generate schema when supplied', () => {
const schemas = validationMetadatasToSchemas({
additionalConverters: {
CustomTextLength: meta => ({
CustomTextLength: (meta) => ({
maxLength: meta.constraints[1] - 1,
minLength: meta.constraints[0] + 1,
type: 'string'
})
}
type: 'string',
}),
},
})

expect(schemas.Post).toEqual({
properties: {
title: { maxLength: 10, minLength: 1, type: 'string' }
title: { maxLength: 10, minLength: 1, type: 'string' },
},
required: ['title'],
type: 'object'
type: 'object',
})
})
})
22 changes: 11 additions & 11 deletions __tests__/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
IsEmpty,
IsMongoId,
MaxLength,
MetadataStorage
MetadataStorage,
} from 'class-validator'
import * as _ from 'lodash'

Expand All @@ -15,28 +15,28 @@ import { JSONSchema, validationMetadatasToSchemas } from '../src'
@JSONSchema({
deprecated: true,
description: 'A User object',
example: { id: '123' }
example: { id: '123' },
})
// @ts-ignore: not referenced
class User {
@JSONSchema({
default: '1',
description: 'User ID',
pattern: '.*'
pattern: '.*',
})
@IsMongoId()
id: string

@MaxLength(20, { each: true })
@ArrayMaxSize(5)
@JSONSchema({
items: { description: 'Tag string' }
items: { description: 'Tag string' },
})
@ArrayNotContains(['admin'])
tags?: string[]

@JSONSchema(() => ({
anyOf: [{ type: 'null' }, { type: 'string', const: '' }]
anyOf: [{ type: 'null' }, { type: 'string', const: '' }],
}))
@IsEmpty()
empty?: string
Expand All @@ -57,26 +57,26 @@ describe('decorators', () => {
it('merges property-level schema keywords from decorator value', () => {
expect(schemas.User.properties).toEqual({
empty: {
anyOf: [{ type: 'null' }, { type: 'string', const: '' }]
anyOf: [{ type: 'null' }, { type: 'string', const: '' }],
},
id: {
default: '1',
description: 'User ID',
pattern: '.*',
type: 'string'
type: 'string',
},
tags: {
items: {
description: 'Tag string',
maxLength: 20,
not: {
anyOf: [{ enum: ['admin'], type: 'string' }]
anyOf: [{ enum: ['admin'], type: 'string' }],
},
type: 'string'
type: 'string',
},
maxItems: 5,
type: 'array'
}
type: 'array',
},
})
})
})
Loading

0 comments on commit 935f8b6

Please sign in to comment.