Skip to content

Commit ae2b79e

Browse files
committed
feat(configuration-validation): allow indexName to be a string
to avoid breaking change
1 parent 0c08017 commit ae2b79e

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,22 @@ To link a single collection to multiple indexes, you can assign an array of inde
221221

222222
**Example 1: Linking a Single Collection to a Single Index**
223223

224-
In the following example, the `restaurant` content-type in Meilisearch is called `my_restaurant` instead of the default `restaurant`.
224+
In the following examples, the `restaurant` content-type in Meilisearch is called `my_restaurant` instead of the default `restaurant`.
225+
226+
```js
227+
// config/plugins.js
228+
229+
module.exports = () => ({
230+
//...
231+
meilisearch: {
232+
config: {
233+
restaurant: {
234+
indexName: "my_restaurants",
235+
}
236+
}
237+
}
238+
})
239+
```
225240

226241
```js
227242
// config/plugins.js

server/__tests__/configuration-validation.test.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Test plugin configuration', () => {
113113
expect(strapiMock.log.error).toHaveBeenCalledTimes(0)
114114
})
115115

116-
test('Test indexName with string', async () => {
116+
test('Test indexName with empty string', async () => {
117117
validatePluginConfig({
118118
restaurant: {
119119
indexName: '',
@@ -122,14 +122,50 @@ describe('Test plugin configuration', () => {
122122
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
123123
expect(strapiMock.log.error).toHaveBeenCalledTimes(1)
124124
expect(strapiMock.log.error).toHaveBeenCalledWith(
125-
'The "indexName" option of "restaurant" should be a non-empty array of strings',
125+
'The "indexName" option of "restaurant" should be a non-empty string or an array of non-empty strings',
126126
)
127127
})
128128

129-
test('Test indexName with non-empty array', async () => {
129+
test('Test indexName with non-empty string', async () => {
130130
validatePluginConfig({
131131
restaurant: {
132-
indexName: ['hello'],
132+
indexName: 'validName',
133+
},
134+
})
135+
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
136+
expect(strapiMock.log.error).toHaveBeenCalledTimes(0)
137+
})
138+
139+
test('Test indexName with empty array', async () => {
140+
validatePluginConfig({
141+
restaurant: {
142+
indexName: [],
143+
},
144+
})
145+
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
146+
expect(strapiMock.log.error).toHaveBeenCalledTimes(1)
147+
expect(strapiMock.log.error).toHaveBeenCalledWith(
148+
'The "indexName" option of "restaurant" should be a non-empty string or an array of non-empty strings',
149+
)
150+
})
151+
152+
test('Test indexName with non-empty array containing empty string', async () => {
153+
validatePluginConfig({
154+
restaurant: {
155+
indexName: [''],
156+
},
157+
})
158+
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
159+
expect(strapiMock.log.error).toHaveBeenCalledTimes(1)
160+
expect(strapiMock.log.error).toHaveBeenCalledWith(
161+
'The "indexName" option of "restaurant" should be a non-empty string or an array of non-empty strings',
162+
)
163+
})
164+
165+
test('Test indexName with non-empty array of strings', async () => {
166+
validatePluginConfig({
167+
restaurant: {
168+
indexName: ['validName1', 'validName2'],
133169
},
134170
})
135171
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)

server/configuration-validation.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,21 @@ function CollectionConfig({ collectionName, configuration }) {
182182

183183
return {
184184
validateIndexName() {
185-
// indexName is either undefined or a non empty array of string
186-
if (indexName !== undefined && !Array.isArray(indexName)) {
185+
// indexName is either undefined, a non-empty string, or a non-empty array of non-empty strings
186+
const isStringAndNotEmpty =
187+
typeof indexName === 'string' && indexName !== ''
188+
189+
const isArrayWithNonEmptyStrings =
190+
Array.isArray(indexName) &&
191+
indexName.length > 0 &&
192+
indexName.every(name => typeof name === 'string' && name !== '')
193+
194+
if (
195+
indexName !== undefined &&
196+
!(isStringAndNotEmpty || isArrayWithNonEmptyStrings)
197+
) {
187198
log.error(
188-
`The "indexName" option of "${collectionName}" should be a non-empty array of strings`,
199+
`The "indexName" option of "${collectionName}" should be a non-empty string or an array of non-empty strings`,
189200
)
190201
} else if (indexName !== undefined) {
191202
options.indexName = indexName

0 commit comments

Comments
 (0)