From 935f8b6dafc5503435413340728a47981f070f1b Mon Sep 17 00:00:00 2001 From: Aleksi Pekkala Date: Thu, 31 Dec 2020 13:46:25 +0900 Subject: [PATCH] Format __tests__ folder --- __tests__/additionalProperties.ts | 66 +++++++++++------------- __tests__/classTransformer.test.ts | 32 ++++++------ __tests__/customValidation.test.ts | 24 ++++----- __tests__/decorators.test.ts | 22 ++++---- __tests__/defaultConverters.test.ts | 72 +++++++++++++-------------- __tests__/index.test.ts | 44 ++++++++-------- __tests__/inheritedProperties.test.ts | 40 +++++++-------- __tests__/options.test.ts | 28 +++++------ package.json | 4 +- 9 files changed, 163 insertions(+), 169 deletions(-) diff --git a/__tests__/additionalProperties.ts b/__tests__/additionalProperties.ts index 29ccf97..4faf68f 100644 --- a/__tests__/additionalProperties.ts +++ b/__tests__/additionalProperties.ts @@ -6,7 +6,7 @@ import { defaultMetadataStorage } from 'class-transformer/storage' class User { @IsString() - name: string; + name: string } // @ts-ignore: not referenced @@ -14,13 +14,13 @@ class Post { @Type(() => { return String }) - @MinLength(2, {each: true}) + @MinLength(2, { each: true }) userStatus: Map } // @ts-ignore: not referenced class PostWidthUsers { - @ValidateNested({each: true}) + @ValidateNested({ each: true }) @Type(() => User) users: Map } @@ -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'], + }, }) }) }) diff --git a/__tests__/classTransformer.test.ts b/__tests__/classTransformer.test.ts index afba4ce..c11320d 100644 --- a/__tests__/classTransformer.test.ts +++ b/__tests__/classTransformer.test.ts @@ -7,7 +7,7 @@ import { IsOptional, IsString, MetadataStorage, - ValidateNested + ValidateNested, } from 'class-validator' import { validationMetadatasToSchemas } from '../src' @@ -44,23 +44,23 @@ 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', }) }) @@ -68,30 +68,30 @@ describe('class-transformer compatibility', () => { // @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', }) }) }) diff --git a/__tests__/customValidation.test.ts b/__tests__/customValidation.test.ts index 3ed2a7d..7940db4 100644 --- a/__tests__/customValidation.test.ts +++ b/__tests__/customValidation.test.ts @@ -3,7 +3,7 @@ import { Validate, ValidationArguments, ValidatorConstraint, - ValidatorConstraintInterface + ValidatorConstraintInterface, } from 'class-validator' import * as _ from 'lodash' @@ -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', }) }) }) diff --git a/__tests__/decorators.test.ts b/__tests__/decorators.test.ts index cb92828..183e526 100644 --- a/__tests__/decorators.test.ts +++ b/__tests__/decorators.test.ts @@ -6,7 +6,7 @@ import { IsEmpty, IsMongoId, MaxLength, - MetadataStorage + MetadataStorage, } from 'class-validator' import * as _ from 'lodash' @@ -15,14 +15,14 @@ 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 @@ -30,13 +30,13 @@ class User { @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 @@ -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', + }, }) }) }) diff --git a/__tests__/defaultConverters.test.ts b/__tests__/defaultConverters.test.ts index bf9c4a8..1ebc33f 100644 --- a/__tests__/defaultConverters.test.ts +++ b/__tests__/defaultConverters.test.ts @@ -9,11 +9,11 @@ class Comment {} enum PostType { Public, - Private + Private, } enum Role { Anonymous = 'anonymous', - User = 'user' + User = 'user', } // @ts-ignore: not referenced @@ -169,11 +169,11 @@ describe('defaultConverters', () => { { type: 'boolean' }, { type: 'integer' }, { type: 'array' }, - { type: 'object' } - ] - } - } - ] + { type: 'object' }, + ], + }, + }, + ], }, isNotEmpty: { minLength: 1, type: 'string' }, isInEmpty: {}, @@ -186,8 +186,8 @@ describe('defaultConverters', () => { isDate: { oneOf: [ { format: 'date', type: 'string' }, - { format: 'date-time', type: 'string' } - ] + { format: 'date-time', type: 'string' }, + ], }, isString: { type: 'string' }, isNumber: { type: 'number' }, @@ -207,21 +207,21 @@ describe('defaultConverters', () => { description: `After ${new Date('2017').toJSON()}`, oneOf: [ { format: 'date', type: 'string' }, - { format: 'date-time', type: 'string' } - ] + { format: 'date-time', type: 'string' }, + ], }, maxDate: { description: `Before ${new Date('2017').toJSON()}`, oneOf: [ { format: 'date', type: 'string' }, - { format: 'date-time', type: 'string' } - ] + { format: 'date-time', type: 'string' }, + ], }, isBooleanString: { type: 'string', enum: ['true', 'false'] }, isDateString: { pattern: 'd{4}-[01]d-[0-3]dT[0-2]d:[0-5]d:[0-5]d.d+Z?', - type: 'string' + type: 'string', }, isNumberString: { pattern: '^[-+]?[0-9]+$', type: 'string' }, @@ -239,17 +239,17 @@ describe('defaultConverters', () => { isFullWidth: { pattern: '[^\\u0020-\\u007E\\uFF61-\\uFF9F\\uFFA0-\\uFFDC\\uFFE8-\\uFFEE0-9a-zA-Z]', - type: 'string' + type: 'string', }, isHalfWidth: { pattern: '[\\u0020-\\u007E\\uFF61-\\uFF9F\\uFFA0-\\uFFDC\\uFFE8-\\uFFEE0-9a-zA-Z]', - type: 'string' + type: 'string', }, isVariableWidth: { type: 'string' }, isHexColor: { pattern: '^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$', - type: 'string' + type: 'string', }, isHexadecimal: { pattern: '^[0-9a-fA-F]+$', type: 'string' }, isIPv4: { format: 'ipv4', type: 'string' }, @@ -259,23 +259,23 @@ describe('defaultConverters', () => { isISO8601: { oneOf: [ { format: 'date', type: 'string' }, - { format: 'date-time', type: 'string' } - ] + { format: 'date-time', type: 'string' }, + ], }, isJSON: { format: 'json', type: 'string' }, isLowerCase: { type: 'string' }, isMobilePhone: { format: 'mobile-phone', type: 'string' }, isMongoId: { pattern: '^[0-9a-fA-F]{24}$', - type: 'string' + type: 'string', }, isMultibyte: { pattern: '[^\\x00-\\x7F]', - type: 'string' + type: 'string', }, isSurrogatePair: { pattern: '[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]', - type: 'string' + type: 'string', }, isUrl: { format: 'url', type: 'string' }, isUUID: { format: 'uuid', type: 'string' }, @@ -287,7 +287,7 @@ describe('defaultConverters', () => { matches: { pattern: '\\d[a-zA-Z]+', type: 'string' }, isMilitaryTime: { pattern: '^([01]\\d|2[0-3]):?([0-5]\\d)$', - type: 'string' + type: 'string', }, arrayContainsString: { @@ -295,18 +295,18 @@ describe('defaultConverters', () => { not: { anyOf: [ { items: { not: { type: 'string', enum: ['x'] } } }, - { items: { not: { type: 'string', enum: ['y'] } } } - ] - } + { items: { not: { type: 'string', enum: ['y'] } } }, + ], + }, }, arrayContainsVarious: { type: 'array', not: { anyOf: [ { items: { not: { type: 'boolean', enum: [true] } } }, - { items: { not: { type: 'number', enum: [1] } } } - ] - } + { items: { not: { type: 'number', enum: [1] } } }, + ], + }, }, arrayContainsComplex: { type: 'array', items: {} }, arrayNotContains: { @@ -315,20 +315,20 @@ describe('defaultConverters', () => { not: { anyOf: [ { enum: ['x'], type: 'string' }, - { enum: ['y'], type: 'string' } - ] - } - } + { enum: ['y'], type: 'string' }, + ], + }, + }, }, arrayNotContainsComplex: { type: 'array', items: {} }, arrayNotEmpty: { type: 'array', items: {}, minItems: 1 }, arrayMinSize: { type: 'array', items: {}, minItems: 1 }, arrayMaxSize: { type: 'array', items: {}, maxItems: 10 }, - arrayUnique: { type: 'array', items: {}, uniqueItems: true } + arrayUnique: { type: 'array', items: {}, uniqueItems: true }, }, required: expect.any(Array), - type: 'object' - } + type: 'object', + }, }) }) }) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 8985ce8..3b1a85c 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -9,7 +9,7 @@ import { Length, MaxLength, MinLength, - ValidateNested + ValidateNested, } from 'class-validator' import { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata' import * as _ from 'lodash' @@ -50,12 +50,12 @@ describe('classValidatorConverter', () => { it('handles empty metadata', () => { const emptyStorage: any = { constraintMetadatas: [], - validationMetadatas: [] + validationMetadatas: [], } expect( validationMetadatasToSchemas({ - classValidatorMetadataStorage: emptyStorage + classValidatorMetadataStorage: emptyStorage, }) ).toEqual({}) }) @@ -71,15 +71,15 @@ describe('classValidatorConverter', () => { propertyName: 'id', target: User, type: 'NON_EXISTENT_METADATA_TYPE', - validationTypeOptions: {} + validationTypeOptions: {}, } const storage: any = { constraintMetadatas: [], - validationMetadatas: [customMetadata] + validationMetadatas: [customMetadata], } const schemas = validationMetadatasToSchemas({ - classValidatorMetadataStorage: storage + classValidatorMetadataStorage: storage, }) expect(schemas.User.properties!.id).toEqual({ type: 'string' }) }) @@ -91,18 +91,18 @@ describe('classValidatorConverter', () => { Post: { properties: { published: { - type: 'boolean' + type: 'boolean', }, title: { maxLength: 100, minLength: 2, - type: 'string' + type: 'string', }, user: { - $ref: '#/definitions/User' - } + $ref: '#/definitions/User', + }, }, - type: 'object' + type: 'object', }, User: { properties: { @@ -117,12 +117,12 @@ describe('classValidatorConverter', () => { { type: 'boolean' }, { type: 'integer' }, { type: 'array' }, - { type: 'object' } - ] + { type: 'object' }, + ], }, - nullable: true - } - ] + nullable: true, + }, + ], }, firstName: { minLength: 5, type: 'string' }, id: { type: 'string' }, @@ -130,17 +130,17 @@ describe('classValidatorConverter', () => { items: { maxLength: 20, not: { - anyOf: [{ enum: ['admin'], type: 'string' }] + anyOf: [{ enum: ['admin'], type: 'string' }], }, - type: 'string' + type: 'string', }, maxItems: 5, - type: 'array' - } + type: 'array', + }, }, required: ['id', 'firstName'], - type: 'object' - } + type: 'object', + }, }) }) }) diff --git a/__tests__/inheritedProperties.test.ts b/__tests__/inheritedProperties.test.ts index 0f42d1f..ba59a76 100644 --- a/__tests__/inheritedProperties.test.ts +++ b/__tests__/inheritedProperties.test.ts @@ -9,7 +9,7 @@ import { IsString, MaxLength, MetadataStorage, - MinLength + MinLength, } from 'class-validator' import * as _ from 'lodash' @@ -17,11 +17,11 @@ import { JSONSchema, validationMetadatasToSchemas } from '../src' @JSONSchema({ description: 'Contains email, password and phone', - summary: 'Base object' + summary: 'Base object', }) class BaseContent { @JSONSchema({ - default: 'some@email.com' + default: 'some@email.com', }) @IsDefined() @IsEmail() @@ -29,7 +29,7 @@ class BaseContent { @JSONSchema({ description: 'Password field', - summary: 'Password' + summary: 'Password', }) @IsString() @IsOptional() @@ -41,7 +41,7 @@ class BaseContent { } @JSONSchema({ - summary: 'User object' + summary: 'User object', }) // @ts-ignore: not referenced class User extends BaseContent { @@ -50,14 +50,14 @@ class User extends BaseContent { name: string @JSONSchema({ - description: 'Password field - required!' + description: 'Password field - required!', }) @MinLength(20) @IsDefined() password: string @JSONSchema({ - summary: 'Mobile phone number' + summary: 'Mobile phone number', }) @IsOptional() phone: string @@ -80,21 +80,21 @@ describe('Inheriting validation decorators', () => { email: { default: 'some@email.com', format: 'email', - type: 'string' + type: 'string', }, password: { description: 'Password field', summary: 'Password', - type: 'string' + type: 'string', }, phone: { format: 'mobile-phone', - type: 'string' - } + type: 'string', + }, }, required: ['email', 'phone'], summary: 'Base object', - type: 'object' + type: 'object', }) expect(schemas.User).toEqual({ @@ -102,37 +102,37 @@ describe('Inheriting validation decorators', () => { email: { default: 'some@email.com', format: 'email', - type: 'string' + type: 'string', }, name: { maxLength: 20, minLength: 10, - type: 'string' + type: 'string', }, password: { description: 'Password field - required!', minLength: 20, - type: 'string' + type: 'string', }, phone: { format: 'mobile-phone', summary: 'Mobile phone number', - type: 'string' + type: 'string', }, welcome: { pattern: 'hello', - type: 'string' - } + type: 'string', + }, }, required: ['name', 'welcome', 'email'], summary: 'User object', - type: 'object' + type: 'object', }) }) it('handles inherited IsDefined decorators when skipMissingProperties is enabled', () => { const schemas = validationMetadatasToSchemas({ - skipMissingProperties: true + skipMissingProperties: true, }) expect(schemas.BaseContent.required).toEqual(['email', 'phone']) diff --git a/__tests__/options.test.ts b/__tests__/options.test.ts index 1ead9fa..990bda0 100644 --- a/__tests__/options.test.ts +++ b/__tests__/options.test.ts @@ -8,7 +8,7 @@ import { MAX_LENGTH, MaxLength, MetadataStorage, - ValidateNested + ValidateNested, } from 'class-validator' import * as _ from 'lodash' @@ -39,17 +39,17 @@ const defaultSchemas = validationMetadatasToSchemas(metadata) describe('options', () => { it('sets default refPointerPrefix', () => { expect(defaultSchemas.Post.properties!.user).toEqual({ - $ref: '#/definitions/User' + $ref: '#/definitions/User', }) }) it('handles refPointerPrefix option', () => { const schemas = validationMetadatasToSchemas({ - refPointerPrefix: '#/components/schema/' + refPointerPrefix: '#/components/schema/', }) expect(schemas.Post.properties!.user).toEqual({ - $ref: '#/components/schema/User' + $ref: '#/components/schema/User', }) }) @@ -59,22 +59,22 @@ describe('options', () => { id: { type: 'string' }, tags: { items: { type: 'string', maxLength: 20 }, - type: 'array' - } + type: 'array', + }, }) const schemas = validationMetadatasToSchemas({ additionalConverters: { [IS_STRING]: { description: 'A string value', - type: 'string' + type: 'string', }, - [MAX_LENGTH]: meta => ({ + [MAX_LENGTH]: (meta) => ({ exclusiveMaximum: true, maxLength: meta.constraints[0] + 1, - type: 'string' - }) - } + type: 'string', + }), + }, }) expect(schemas.User.properties).toEqual({ @@ -82,8 +82,8 @@ describe('options', () => { id: { description: 'A string value', type: 'string' }, tags: { items: { exclusiveMaximum: true, type: 'string', maxLength: 21 }, - type: 'array' - } + type: 'array', + }, }) }) @@ -92,7 +92,7 @@ describe('options', () => { expect(defaultSchemas.Post).not.toHaveProperty('required') const schemas = validationMetadatasToSchemas({ - skipMissingProperties: true + skipMissingProperties: true, }) expect(schemas.User.required).toEqual(['id']) expect(defaultSchemas.Post).not.toHaveProperty('required') diff --git a/package.json b/package.json index ae9d649..f046a6d 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ "scripts": { "clean": "rimraf coverage build", "build": "npm run clean && tsc -p tsconfig.release.json", - "format": "prettier --write {src,test}/**/*.ts", - "test:format": "prettier --check {src,test}/**/*.ts", + "format": "prettier --write {src,__tests__}/**/*.ts", + "test:format": "prettier --check {src,__tests__}/**/*.ts", "test:lint": "tslint --project . src/**/*.ts", "prepare": "install-self-peers --npm -- --no-save --ignore-scripts && npm run build", "send-coverage": "codecov -f coverage/*.json",