From 2da388f18c48a1d4f8a361fc7be7cea515ce4621 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 21 Dec 2023 09:17:05 +0000 Subject: [PATCH] fix: handle backslash in string literal --- .../nocodb/src/services/columns.service.ts | 8 ++- .../tests/unit/rest/tests/formula.test.ts | 54 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index aa9236779e..d0753f54f2 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -211,7 +211,7 @@ export class ColumnsService { table.columns, ); colBody.parsed_tree = await validateFormulaAndExtractTreeWithType({ - formula: colBody.formula_raw || colBody.formula, + formula: colBody.formula || colBody.formula_raw, columns: table.columns, column, clientOrSqlUi: source.type, @@ -1236,10 +1236,8 @@ export class ColumnsService { colBody.parsed_tree = await validateFormulaAndExtractTreeWithType({ // formula may include double curly brackets in previous version // convert to single curly bracket here for compatibility - formula: - colBody.formula_raw || - colBody.formula?.replaceAll('{{', '{').replaceAll('}}', '}'), - column:{ + formula: colBody.formula, + column: { ...colBody, colOptions: colBody, }, diff --git a/packages/nocodb/tests/unit/rest/tests/formula.test.ts b/packages/nocodb/tests/unit/rest/tests/formula.test.ts index 21148470a5..91e0c3c3f9 100644 --- a/packages/nocodb/tests/unit/rest/tests/formula.test.ts +++ b/packages/nocodb/tests/unit/rest/tests/formula.test.ts @@ -64,37 +64,37 @@ function formulaRegExpBased() { expect(unfilteredRecords.length).to.equal(100); }); - it('Type: REGEX_MATCH ', async () => { + it.only('Type: REGEX_MATCH ', async () => { const formulaList = [ - `REGEX_MATCH("123-45-6789", "\\\\d{3}-\\\\d{2}-\\\\d{4}")`, - 'REGEX_MATCH("123-45-6789", "\\\\d{3}-\\\\d{2}-\\\\d{4}")', - 'REGEX_MATCH("123-45-6789", "\\\\d{3}-\\\\d{2}-\\\\d{4}")', - 'REGEX_MATCH("ABC-45-6789", "\\\\w{3}-\\\\d{2}-\\\\d{4}")', - 'REGEX_MATCH("123-XY-6789", "\\\\d{3}-\\\\D{2}-\\\\d{4}")', - 'REGEX_MATCH("123-45-$#@!", "123-45-[\\\\s\\\\S]{4}")', + `REGEX_MATCH("123-45-6789", "\\d{3}-\\d{2}-\\d{4}")`, + 'REGEX_MATCH("123-45-6789", "\\d{3}-\\d{2}-\\d{4}")', + 'REGEX_MATCH("123-45-6789", "\\d{3}-\\d{2}-\\d{4}")', + 'REGEX_MATCH("ABC-45-6789", "\\w{3}-\\d{2}-\\d{4}")', + 'REGEX_MATCH("123-XY-6789", "\\d{3}-\\D{2}-\\d{4}")', + 'REGEX_MATCH("123-45-$#@!", "123-45-[\\s\\S]{4}")', 'REGEX_MATCH("123456789", "1?2?3?-?4?5-?6?7?8?9?")', - 'REGEX_MATCH("123-456789", "\\\\d{3}-?\\\\d{2}-?\\\\d{4}")', - 'REGEX_MATCH("123-45-6789", "123-\\\\d{2}-6789")', - 'REGEX_MATCH("abc123", "[a-z]{3}\\\\d{3}")', - 'REGEX_MATCH("A1B2C3", "[A-Z]\\\\d[A-Z]\\\\d[A-Z]\\\\d")', - 'REGEX_MATCH("hello123world", "\\\\w{5}\\\\d{3}\\\\w{5}")', - 'REGEX_MATCH("email@example.com", "[a-zA-Z]+@[a-zA-Z]+\\\\.[a-zA-Z]+")', - 'REGEX_MATCH("2023-12-14", "\\\\d{4}-\\\\d{2}-\\\\d{2}")', - 'REGEX_MATCH("USD 100.50", "USD \\\\d+\\\\.\\\\d{2}")', - 'REGEX_MATCH("http://www.example.com", "https?://[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}")', - 'REGEX_MATCH("555-1234", "\\\\d{3}-\\\\d{4}")', - 'REGEX_MATCH("username123", "[a-zA-Z]+\\\\d{3}")', - 'REGEX_MATCH("apple, orange, banana", "\\\\w+, \\\\w+, \\\\w+")', - 'REGEX_MATCH("aaaabbcc", "(\\\\w{2})\\\\1")', - 'REGEX_MATCH("1234567890", "\\\\d{10}")', - 'REGEX_MATCH("12.34", "\\\\d+\\\\.\\\\d{2}")', - 'REGEX_MATCH("123 Main St, City", "\\\\d+ [a-zA-Z]+ St, [a-zA-Z]+")', - 'REGEX_MATCH("X1Y2Z3", "[A-Z]\\\\d[A-Z]\\\\d[A-Z]\\\\d")', - 'REGEX_MATCH("555-555-5555", "\\\\d{3}-\\\\d{3}-\\\\d{4}")', - 'REGEX_MATCH("password123", "^(?=.*\\\\d)(?=.*[a-zA-Z]).{8,}$")', + 'REGEX_MATCH("123-456789", "\\d{3}-?\\d{2}-?\\d{4}")', + 'REGEX_MATCH("123-45-6789", "123-\\d{2}-6789")', + 'REGEX_MATCH("abc123", "[a-z]{3}\\d{3}")', + 'REGEX_MATCH("A1B2C3", "[A-Z]\\d[A-Z]\\d[A-Z]\\d")', + 'REGEX_MATCH("hello123world", "\\w{5}\\d{3}\\w{5}")', + 'REGEX_MATCH("email@example.com", "[a-zA-Z]+@[a-zA-Z]+\\.[a-zA-Z]+")', + 'REGEX_MATCH("2023-12-14", "\\d{4}-\\d{2}-\\d{2}")', + 'REGEX_MATCH("USD 100.50", "USD \\d+\\.\\d{2}")', + 'REGEX_MATCH("http://www.example.com", "https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}")', + 'REGEX_MATCH("555-1234", "\\d{3}-\\d{4}")', + 'REGEX_MATCH("username123", "[a-zA-Z]+\\d{3}")', + 'REGEX_MATCH("apple, orange, banana", "\\w+, \\w+, \\w+")', + 'REGEX_MATCH("aaaabbcc", "(\\w{2})\\1")', + 'REGEX_MATCH("1234567890", "\\d{10}")', + 'REGEX_MATCH("12.34", "\\d+\\.\\d{2}")', + 'REGEX_MATCH("123 Main St, City", "\\d+ [a-zA-Z]+ St, [a-zA-Z]+")', + 'REGEX_MATCH("X1Y2Z3", "[A-Z]\\d[A-Z]\\d[A-Z]\\d")', + 'REGEX_MATCH("555-555-5555", "\\d{3}-\\d{3}-\\d{4}")', + 'REGEX_MATCH("password123", "^(?=.*\\d)(?=.*[a-zA-Z]).{8,}$")', 'REGEX_MATCH("12345", "^[0-9]{5}$")', 'REGEX_MATCH("abc123!@#", "[a-zA-Z0-9!@#]+")', - 'REGEX_MATCH("12-December-2023", "\\\\d{2}-[a-zA-Z]+-\\\\d{4}")', + 'REGEX_MATCH("12-December-2023", "\\d{2}-[a-zA-Z]+-\\d{4}")', ]; for (let i = 0; i < formulaList.length; i++) {