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:
Jessica Chowdhury
2022-12-19 17:16:30 +00:00
committed by GitHub
parent 2e765a1e05
commit 9af4c1dde7
2 changed files with 27 additions and 2 deletions

View File

@@ -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');
};

View File

@@ -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: 'Тест' });