Skip to content

Commit cbe297d

Browse files
committed
feat: update validation message with validation from strapi
1 parent 8945c4d commit cbe297d

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

packages/core/server/controllers/search.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import { Context } from 'koa';
22
import { UID } from '@strapi/strapi';
3+
import { errors } from '@strapi/utils';
34
import { isContentTypeEnabled } from '../util/enabledContentTypes';
4-
import { getMainField } from '../services/get-main-field';
5+
import { getPluginService } from '../util/getPluginService';
56

67
/**
78
* Search controller
89
*/
910
export default {
1011
search: async (ctx: Context & { params: { id: number } }) => {
1112
const { q } = ctx.query;
12-
const { id } = ctx.params;
13-
let results = [];
13+
const results = [];
1414

1515
const qStr = typeof q === 'string' ? q.trim() : '';
1616
if (!qStr) {
17-
ctx.throw(400, 'Missing or invalid query parameter "?q=" (must be a non-empty strin4g)');
18-
return;
17+
throw new errors.ValidationError('Missing or invalid query parameter "?q=" (must be a non-empty string)');
1918
}
2019

2120
await Promise.all(
2221
Object.entries(strapi.contentTypes).map(async ([uid, config]: [UID.CollectionType, any]) => {
2322
const hasWT = isContentTypeEnabled(config);
2423
if (!hasWT) return;
2524

26-
const mainField = await getMainField(uid);
25+
const mainField = await getPluginService('get-main-field').getMainField(uid);
2726
if (!mainField) return;
2827

2928
const entries = await (strapi as any).documents(uid).findMany({
@@ -54,20 +53,17 @@ export default {
5453
const { contentType, documentId } = ctx.params;
5554

5655
if (typeof contentType !== 'string' || !(contentType in strapi.contentTypes)) {
57-
ctx.throw(400, `Unknown or invalid content type: ${contentType}`);
58-
return;
56+
throw new errors.ValidationError(`Unknown or invalid content type: ${contentType}`);
5957
}
6058

61-
const mainField = await getMainField(contentType as UID.CollectionType);
62-
59+
const mainField = await getPluginService('get-main-field').getMainField(contentType as UID.CollectionType);
6360
const entry = await (strapi as any).documents(contentType as UID.CollectionType).findOne({
6461
documentId,
6562
fields: ['id', 'documentId', ...(mainField ? [mainField] : [])],
6663
});
6764

6865
if (!entry) {
69-
ctx.throw(404, 'Entry not found');
70-
return;
66+
throw new errors.ValidationError('Entry not found');
7167
}
7268

7369
ctx.body = {
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { UID } from '@strapi/strapi';
22

3-
export async function getMainField(uid: UID.CollectionType): Promise<string | null> {
3+
export const getMainField = async (uid: UID.CollectionType): Promise<string | null> => {
44
const coreStoreSettings = await strapi.query('strapi::core-store').findMany({
55
where: { key: `plugin_content_manager_configuration_content_types::${uid}` },
66
});
77
if (!coreStoreSettings?.[0]) return null;
88

99
const value = JSON.parse(coreStoreSettings[0].value);
10+
1011
return value?.settings?.mainField ?? null;
11-
}
12+
};
13+
14+
export default () => ({
15+
getMainField,
16+
});
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import urlAliasController from './url-alias';
2-
import urlPatternController from './url-pattern';
3-
import bulkGenerateController from './bulk-generate';
1+
import urlAliasService from './url-alias';
2+
import urlPatternService from './url-pattern';
3+
import bulkGenerateService from './bulk-generate';
4+
import getMainFieldService from './get-main-field';
45

56
export default {
6-
'url-alias': urlAliasController,
7-
'url-pattern': urlPatternController,
8-
'bulk-generate': bulkGenerateController,
7+
'url-alias': urlAliasService,
8+
'url-pattern': urlPatternService,
9+
'bulk-generate': bulkGenerateService,
10+
'get-main-field': getMainFieldService,
911
};

0 commit comments

Comments
 (0)