Skip to content

Commit 70ccba7

Browse files
authored
fix: allow multiple spaces in item name (#2091)
1 parent 45ff12a commit 70ccba7

3 files changed

Lines changed: 7 additions & 27 deletions

File tree

src/schemas/regex.spec.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { EMPTY_OR_SPACED_WORDS_REGEX, ITEM_NAME_REGEX } from './regex';
3+
import { ITEM_NAME_REGEX } from './regex';
44

55
describe('Globals', () => {
66
describe('Item name regex', () => {
77
it('Name regex accepts words with spaces', () => {
88
expect(new RegExp(ITEM_NAME_REGEX).test('Course')).toBeTruthy();
99
expect(new RegExp(ITEM_NAME_REGEX).test('My Course')).toBeTruthy();
1010
expect(new RegExp(ITEM_NAME_REGEX).test('My Course from yesterday')).toBeTruthy();
11+
// double space is allowed (users have created content with it already)
12+
expect(new RegExp(ITEM_NAME_REGEX).test('My Course')).toBeTruthy();
1113
});
1214
it('Name regex rejects string ending with spaces', () => {
1315
expect(new RegExp(ITEM_NAME_REGEX).test('My Course from yesterday ')).toBeFalsy();
1416
expect(new RegExp(ITEM_NAME_REGEX).test(' Course')).toBeFalsy();
15-
expect(new RegExp(ITEM_NAME_REGEX).test('My Course')).toBeFalsy();
16-
});
17-
});
18-
19-
describe('Empty or spaced words regex', () => {
20-
it('Regex accepts words separated with one space', () => {
21-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('Bob')).toBeTruthy();
22-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('Bob and Alice')).toBeTruthy();
23-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('Bob and Alice')).toBeTruthy();
24-
});
25-
it('Regex accepts empty string', () => {
26-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('')).toBeTruthy();
27-
});
28-
it('Regex rejects string ending with spaces', () => {
29-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('Bob ')).toBeFalsy();
30-
});
31-
it('Regex rejects string starting with spaces', () => {
32-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test(' Bob')).toBeFalsy();
33-
});
34-
it('Regex rejects words separated with multiple spaces', () => {
35-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('Bob Alice')).toBeFalsy();
36-
expect(new RegExp(EMPTY_OR_SPACED_WORDS_REGEX).test('Bob Alice')).toBeFalsy();
3717
});
3818
});
3919
});

src/schemas/regex.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export const ITEM_NAME_REGEX = '^\\S+( \\S+)*$';
2-
// allow empty strings or words separated by one space
3-
export const EMPTY_OR_SPACED_WORDS_REGEX = /^(\S+( \S+)*)?$/;
1+
// allows words separated by spaces (any number), but not starting or ending with space
2+
export const ITEM_NAME_REGEX = '^\\S+( *\\S+)*$';

src/services/item/item.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,11 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
168168
'/:id/children',
169169
{ schema: getChildren, preHandler: optionalIsAuthenticated },
170170
async ({ user, params: { id }, query: { types, keywords } }) => {
171-
return itemService.getPackedChildren(db, user?.account, id, {
171+
const children = await itemService.getPackedChildren(db, user?.account, id, {
172172
types,
173173
keywords,
174174
});
175+
return children;
175176
},
176177
);
177178

0 commit comments

Comments
 (0)