fix: prevents special characters breaking relationship field search (#1710)
* fix: prevents special characters breaking relationship field search * test: add special char querying w/ like operator Co-authored-by: Elliot DeNolf <denolfe@gmail.com>
This commit is contained in:
committed by
GitHub
parent
2e765a1e05
commit
9af4c1dde7
@@ -4,9 +4,9 @@ export default (input: string): RegExp => {
|
||||
// Regex word boundaries that work for cyrillic characters - https://stackoverflow.com/a/47062016/1717697
|
||||
const wordBoundaryBefore = '(?:(?:[^\\p{L}\\p{N}])|^)'; // Converted to a non-matching group instead of positive lookbehind for Safari
|
||||
const wordBoundaryAfter = '(?=[^\\p{L}\\p{N}]|$)';
|
||||
|
||||
const regex = words.reduce((pattern, word, i) => {
|
||||
return `${pattern}(?=.*${wordBoundaryBefore}${word}.*${wordBoundaryAfter})${i + 1 === words.length ? '.+' : ''}`;
|
||||
const escapedWord = word.replace(/[\\^$*+?\\.()|[\]{}]/g, '\\$&');
|
||||
return `${pattern}(?=.*${wordBoundaryBefore}.*${escapedWord}.*${wordBoundaryAfter})${i + 1 === words.length ? '.+' : ''}`;
|
||||
}, '');
|
||||
return new RegExp(regex, 'i');
|
||||
};
|
||||
|
||||
@@ -373,6 +373,31 @@ describe('collections-rest', () => {
|
||||
expect(result.totalDocs).toEqual(1);
|
||||
});
|
||||
|
||||
|
||||
describe('like - special characters', () => {
|
||||
const specialCharacters = '~!@#$%^&*()_+-+[]{}|;:"<>,.?/})';
|
||||
|
||||
it.each(specialCharacters.split(''))('like - special characters - %s', async (character) => {
|
||||
const post1 = await createPost({
|
||||
title: specialCharacters,
|
||||
});
|
||||
|
||||
const query = {
|
||||
query: {
|
||||
title: {
|
||||
like: character,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const { status, result } = await client.find<Post>(query);
|
||||
|
||||
expect(status).toEqual(200);
|
||||
expect(result.docs).toEqual([post1]);
|
||||
expect(result.totalDocs).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('like - cyrillic characters', async () => {
|
||||
const post1 = await createPost({ title: 'Тест' });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user