Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: api validators (boolean and date) #7709

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/src/commands/upload.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class Asset {
assetData: new File([await fs.openAsBlob(this.path)], basename(this.path)),
deviceAssetId: this.deviceAssetId,
deviceId: 'CLI',
fileCreatedAt: this.fileCreatedAt,
fileModifiedAt: this.fileModifiedAt,
fileCreatedAt: this.fileCreatedAt.toISOString(),
fileModifiedAt: this.fileModifiedAt.toISOString(),
isFavorite: String(false),
};
const formData = new FormData();
Expand Down
23 changes: 11 additions & 12 deletions e2e/src/api/specs/person.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import request from 'supertest';
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';

const invalidBirthday = [
// TODO: enable after replacing `@Type(() => Date)`
// { birthDate: 'false', response: 'Invalid date' },
// { birthDate: '123567', response: 'Invalid date },
// { birthDate: 123_567, response: ['Birth date cannot be in the future'] },
{ birthDate: 'false', response: 'birthDate must be a date string' },
{ birthDate: '123567', response: 'birthDate must be a date string' },
{ birthDate: 123_567, response: 'birthDate must be a date string' },
{ birthDate: new Date(9999, 0, 0).toISOString(), response: ['Birth date cannot be in the future'] },
];

Expand Down Expand Up @@ -152,16 +151,16 @@ describe('/person', () => {
expect(body).toEqual(errorDto.unauthorized);
});

it('should not accept invalid birth dates', async () => {
for (const { birthDate, response } of invalidBirthday) {
for (const { birthDate, response } of invalidBirthday) {
it(`should not accept an invalid birth date [${birthDate}]`, async () => {
const { status, body } = await request(app)
.post(`/person`)
.set('Authorization', `Bearer ${admin.accessToken}`)
.send({ birthDate });
expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest(response));
}
});
});
}

it('should create a person', async () => {
const { status, body } = await request(app)
Expand Down Expand Up @@ -202,16 +201,16 @@ describe('/person', () => {
});
}

it('should not accept invalid birth dates', async () => {
for (const { birthDate, response } of invalidBirthday) {
for (const { birthDate, response } of invalidBirthday) {
it(`should not accept an invalid birth date [${birthDate}]`, async () => {
const { status, body } = await request(app)
.put(`/person/${visiblePerson.id}`)
.set('Authorization', `Bearer ${admin.accessToken}`)
.send({ birthDate });
expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest(response));
}
});
});
}

it('should update a date of birth', async () => {
const { status, body } = await request(app)
Expand Down
2 changes: 1 addition & 1 deletion mobile/openapi/doc/PersonApi.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mobile/openapi/doc/ScanLibraryDto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mobile/openapi/doc/SharedLinkCreateDto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions mobile/openapi/lib/model/scan_library_dto.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions mobile/openapi/lib/model/shared_link_create_dto.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mobile/openapi/test/scan_library_dto_test.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mobile/openapi/test/shared_link_create_dto_test.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions open-api/immich-openapi-specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4012,7 +4012,6 @@
"required": false,
"in": "query",
"schema": {
"default": false,
"type": "boolean"
}
}
Expand Down Expand Up @@ -8937,7 +8936,6 @@
"ScanLibraryDto": {
"properties": {
"refreshAllFiles": {
"default": false,
"type": "boolean"
},
"refreshModifiedFiles": {
Expand Down Expand Up @@ -9346,7 +9344,6 @@
"type": "boolean"
},
"allowUpload": {
"default": false,
"type": "boolean"
},
"assetIds": {
Expand Down
7 changes: 3 additions & 4 deletions server/src/domain/album/dto/album-update.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsBoolean, IsString } from 'class-validator';
import { Optional, ValidateUUID } from '../../domain.util';
import { IsString } from 'class-validator';
import { Optional, ValidateBoolean, ValidateUUID } from '../../domain.util';

export class UpdateAlbumDto {
@Optional()
Expand All @@ -13,7 +13,6 @@ export class UpdateAlbumDto {
@ValidateUUID({ optional: true })
albumThumbnailAssetId?: string;

@Optional()
@IsBoolean()
@ValidateBoolean({ optional: true })
isActivityEnabled?: boolean;
}
8 changes: 2 additions & 6 deletions server/src/domain/album/dto/album.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Transform } from 'class-transformer';
import { IsBoolean } from 'class-validator';
import { Optional, toBoolean } from '../../domain.util';
import { ValidateBoolean } from '../../domain.util';

export class AlbumInfoDto {
@Optional()
@IsBoolean()
@Transform(toBoolean)
@ValidateBoolean({ optional: true })
withoutAssets?: boolean;
}
10 changes: 2 additions & 8 deletions server/src/domain/album/dto/get-albums.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean } from 'class-validator';
import { Optional, toBoolean, ValidateUUID } from '../../domain.util';
import { ValidateBoolean, ValidateUUID } from '../../domain.util';

export class GetAlbumsDto {
@Optional()
@IsBoolean()
@Transform(toBoolean)
@ApiProperty()
@ValidateBoolean({ optional: true })
/**
* true: only shared albums
* false: only non-shared own albums
Expand Down
16 changes: 4 additions & 12 deletions server/src/domain/asset/dto/asset-statistics.dto.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { AssetType } from '@app/infra/entities';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean } from 'class-validator';
import { Optional, toBoolean } from '../../domain.util';
import { ValidateBoolean } from '../../domain.util';
import { AssetStats } from '../../repositories';

export class AssetStatsDto {
@IsBoolean()
@Transform(toBoolean)
@Optional()
@ValidateBoolean({ optional: true })
isArchived?: boolean;

@IsBoolean()
@Transform(toBoolean)
@Optional()
@ValidateBoolean({ optional: true })
isFavorite?: boolean;

@IsBoolean()
@Transform(toBoolean)
@Optional()
@ValidateBoolean({ optional: true })
isTrashed?: boolean;
}

Expand Down
Loading
Loading