forked from Cutlery/immich
refactor(server)!: structured validation error responses (#28204)
* refactor(server)!: structured validation error responses * refactor(server): clarify comment on removing duplicate HTTP response fields * enhance validation error tests * make path and message required * fmt * fix e2e test * fmt * feat: enhance error handling in getServerErrorMessage function
This commit is contained in:
@@ -27,31 +27,41 @@ describe(SearchController.name, () => {
|
||||
it('should reject page as a string', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/metadata').send({ page: 'abc' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[page] Invalid input: expected number, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['page'], message: 'Invalid input: expected number, received string' }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject page as a negative number', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/metadata').send({ page: -10 });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[page] Too small: expected number to be >=1']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['page'], message: 'Too small: expected number to be >=1' }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject page as 0', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/metadata').send({ page: 0 });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[page] Too small: expected number to be >=1']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['page'], message: 'Too small: expected number to be >=1' }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject size as a string', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/metadata').send({ size: 'abc' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[size] Invalid input: expected number, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['size'], message: 'Invalid input: expected number, received string' }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject an invalid size', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/metadata').send({ size: -1 });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[size] Too small: expected number to be >=1']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['size'], message: 'Too small: expected number to be >=1' }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject an visibility as not an enum', async () => {
|
||||
@@ -60,7 +70,9 @@ describe(SearchController.name, () => {
|
||||
.send({ visibility: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(
|
||||
errorDto.badRequest([expect.stringContaining('[visibility] Invalid option: expected one of')]),
|
||||
errorDto.validationError([
|
||||
{ path: ['visibility'], message: expect.stringContaining('Invalid option: expected one of') },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -69,7 +81,11 @@ describe(SearchController.name, () => {
|
||||
.post('/search/metadata')
|
||||
.send({ isFavorite: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[isFavorite] Invalid input: expected boolean, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([
|
||||
{ path: ['isFavorite'], message: 'Invalid input: expected boolean, received string' },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject an isEncoded as not a boolean', async () => {
|
||||
@@ -77,7 +93,11 @@ describe(SearchController.name, () => {
|
||||
.post('/search/metadata')
|
||||
.send({ isEncoded: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[isEncoded] Invalid input: expected boolean, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([
|
||||
{ path: ['isEncoded'], message: 'Invalid input: expected boolean, received string' },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject an isOffline as not a boolean', async () => {
|
||||
@@ -85,13 +105,19 @@ describe(SearchController.name, () => {
|
||||
.post('/search/metadata')
|
||||
.send({ isOffline: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[isOffline] Invalid input: expected boolean, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([
|
||||
{ path: ['isOffline'], message: 'Invalid input: expected boolean, received string' },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject an isMotion as not a boolean', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/metadata').send({ isMotion: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[isMotion] Invalid input: expected boolean, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['isMotion'], message: 'Invalid input: expected boolean, received string' }]),
|
||||
);
|
||||
});
|
||||
|
||||
describe('POST /search/random', () => {
|
||||
@@ -105,7 +131,11 @@ describe(SearchController.name, () => {
|
||||
.post('/search/random')
|
||||
.send({ withStacked: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[withStacked] Invalid input: expected boolean, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([
|
||||
{ path: ['withStacked'], message: 'Invalid input: expected boolean, received string' },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject if withPeople is not a boolean', async () => {
|
||||
@@ -113,7 +143,11 @@ describe(SearchController.name, () => {
|
||||
.post('/search/random')
|
||||
.send({ withPeople: 'immich' });
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[withPeople] Invalid input: expected boolean, received string']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([
|
||||
{ path: ['withPeople'], message: 'Invalid input: expected boolean, received string' },
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -140,7 +174,9 @@ describe(SearchController.name, () => {
|
||||
it('should require a name', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).get('/search/person').send({});
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[name] Invalid input: expected string, received undefined']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['name'], message: 'Invalid input: expected string, received undefined' }]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -153,7 +189,9 @@ describe(SearchController.name, () => {
|
||||
it('should require a name', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).get('/search/places').send({});
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest(['[name] Invalid input: expected string, received undefined']));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([{ path: ['name'], message: 'Invalid input: expected string, received undefined' }]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -173,7 +211,11 @@ describe(SearchController.name, () => {
|
||||
it('should require a type', async () => {
|
||||
const { status, body } = await request(ctx.getHttpServer()).get('/search/suggestions').send({});
|
||||
expect(status).toBe(400);
|
||||
expect(body).toEqual(errorDto.badRequest([expect.stringContaining('[type] Invalid option: expected one of')]));
|
||||
expect(body).toEqual(
|
||||
errorDto.validationError([
|
||||
{ path: ['type'], message: expect.stringContaining('Invalid option: expected one of') },
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user