Skip to content

The open alternative to Salesforce, designed for AI.

Explain validation rule syntax errors in plain words (#28103)

Thomas Trompette3ee6de108e07
diff --git a/packages/twenty-front/src/modules/validation-rules/utils/__tests__/getValidationRuleErrorMessage.test.ts b/packages/twenty-front/src/modules/validation-rules/utils/__tests__/getValidationRuleErrorMessage.test.ts
index eb4bd6eb06c..875cb9589bc 100644
--- a/packages/twenty-front/src/modules/validation-rules/utils/__tests__/getValidationRuleErrorMessage.test.ts
+++ b/packages/twenty-front/src/modules/validation-rules/utils/__tests__/getValidationRuleErrorMessage.test.ts
@@ -56,6 +56,37 @@ describe('getValidationRuleErrorMessage', () => {
).toBe('Expression did not return true or false');
});
+ it('should return the message for each syntax error code', () => {
+ expect(
+ getValidationRuleErrorMessage({
+ errorCode: 'INCOMPLETE_EXPRESSION',
+ errorMessage: 'raw message',
+ }),
+ ).toBe('Expression is incomplete');
+
+ expect(
+ getValidationRuleErrorMessage({
+ errorCode: 'UNCLOSED_TEXT',
+ errorMessage: 'raw message',
+ }),
+ ).toBe('A text value is missing its closing quote');
+
+ expect(
+ getValidationRuleErrorMessage({
+ errorCode: 'INVALID_SYNTAX',
+ errorParams: { token: '?' },
+ errorMessage: 'raw message',
+ }),
+ ).toBe('Unexpected "?"');
+
+ expect(
+ getValidationRuleErrorMessage({
+ errorCode: 'INVALID_SYNTAX',
+ errorMessage: 'raw message',
+ }),
+ ).toBe('Expression is not valid');
+ });
+
it('should put the error params back into the message', () => {
expect(
getValidationRuleErrorMessage({
@@ -88,6 +119,10 @@ describe('getValidationRuleErrorMessage', () => {
'isDefined(company.size)',
'isDefined(company.people)',
'stage',
+ 'stage ==',
+ 'stage == "WON',
+ 'stage = "WON"',
+ 'amount amount',
];
for (const expression of expressions) {
diff --git a/packages/twenty-front/src/modules/validation-rules/utils/getValidationRuleErrorMessage.ts b/packages/twenty-front/src/modules/validation-rules/utils/getValidationRuleErrorMessage.ts
index 8bd206e089f..30d717d77d0 100644
--- a/packages/twenty-front/src/modules/validation-rules/utils/getValidationRuleErrorMessage.ts
+++ b/packages/twenty-front/src/modules/validation-rules/utils/getValidationRuleErrorMessage.ts
@@ -1,4 +1,5 @@
import { t } from '@lingui/core/macro';
+import { isDefined } from 'twenty-shared/utils';
import {
type ValidationRuleErrorCode,
type ValidationRuleErrorParams,
@@ -18,6 +19,7 @@ export const getValidationRuleErrorMessage = ({
const path = errorParams?.path ?? '';
const relationFieldName = errorParams?.relationFieldName ?? '';
const subfieldName = errorParams?.subfieldName ?? '';
+ const token = errorParams?.token;
switch (errorCode) {
case 'EMPTY_EXPRESSION':
@@ -44,6 +46,14 @@ export const getValidationRuleErrorMessage = ({
return t`"${path}" goes more than one relation deep`;
case 'NON_BOOLEAN_RESULT':
return t`Expression did not return true or false`;
+ case 'INCOMPLETE_EXPRESSION':
+ return t`Expression is incomplete`;
+ case 'UNCLOSED_TEXT':
+ return t`A text value is missing its closing quote`;
+ case 'INVALID_SYNTAX':
+ return isDefined(token)
+ ? t`Unexpected "${token}"`
+ : t`Expression is not valid`;
default:
return errorMessage;
}
diff --git a/packages/twenty-shared/src/types/ValidationRuleErrorCode.ts b/packages/twenty-shared/src/types/ValidationRuleErrorCode.ts
index ad05544e5f9..ada6cbe026b 100644
--- a/packages/twenty-shared/src/types/ValidationRuleErrorCode.ts
+++ b/packages/twenty-shared/src/types/ValidationRuleErrorCode.ts
@@ -10,4 +10,7 @@ export type ValidationRuleErrorCode =
| 'NOT_A_TO_ONE_RELATION'
| 'UNKNOWN_RELATION_TARGET_FIELD'
| 'RELATION_TOO_DEEP'
- | 'NON_BOOLEAN_RESULT';
+ | 'NON_BOOLEAN_RESULT'
+ | 'INCOMPLETE_EXPRESSION'
+ | 'UNCLOSED_TEXT'
+ | 'INVALID_SYNTAX';
diff --git a/packages/twenty-shared/src/types/ValidationRuleErrorParams.ts b/packages/twenty-shared/src/types/ValidationRuleErrorParams.ts
index 29ab87d94b6..fefffd1b0e3 100644
--- a/packages/twenty-shared/src/types/ValidationRuleErrorParams.ts
+++ b/packages/twenty-shared/src/types/ValidationRuleErrorParams.ts
@@ -4,4 +4,5 @@ export type ValidationRuleErrorParams = {
path?: string;
relationFieldName?: string;
subfieldName?: string;
+ token?: string;
};
diff --git a/packages/twenty-shared/src/utils/index.ts b/packages/twenty-shared/src/utils/index.ts
index 2895434871f..a5efa60ed28 100644
--- a/packages/twenty-shared/src/utils/index.ts
+++ b/packages/twenty-shared/src/utils/index.ts
@@ -329,6 +329,7 @@ export { buildValidationRuleEvaluationContext } from './validation-rule/buildVal
export { compileValidationRuleExpression } from './validation-rule/compileValidationRuleExpression';
export { createValidationRuleEvaluator } from './validation-rule/createValidationRuleEvaluator';
export { evaluateValidationRuleExpression } from './validation-rule/evaluateValidationRuleExpression';
+export { getValidationRuleSyntaxError } from './validation-rule/getValidationRuleSyntaxError';
export { hasValidationRuleBracketAccess } from './validation-rule/hasValidationRuleBracketAccess';
export { isValidationRuleReservedName } from './validation-rule/isValidationRuleReservedName';
export { isValidationRuleValueDefined } from './validation-rule/isValidationRuleValueDefined';
diff --git a/packages/twenty-shared/src/utils/validation-rule/__tests__/compileValidationRuleExpression.test.ts b/packages/twenty-shared/src/utils/validation-rule/__tests__/compileValidationRuleExpression.test.ts
index f302d14ded3..5c5c160ec54 100644
--- a/packages/twenty-shared/src/utils/validation-rule/__tests__/compileValidationRuleExpression.test.ts
+++ b/packages/twenty-shared/src/utils/validation-rule/__tests__/compileValidationRuleExpression.test.ts
@@ -188,8 +188,35 @@ describe('compileValidationRuleExpression', () => {
expect(compile('stage = "WON"').isValid).toBe(false);
});
- it('should reject a syntax error', () => {
- expect(compile('stage ==').isValid).toBe(false);
+ it('should report an incomplete expression', () => {
+ expect(compile('stage ==')).toEqual({
+ isValid: false,
+ errorMessage: 'Expression is incomplete',
+ errorCode: 'INCOMPLETE_EXPRESSION',
+ });
+ expect(compile('(stage == "WON"')).toMatchObject({
+ errorCode: 'INCOMPLETE_EXPRESSION',
+ });
+ });
+
+ it('should report a text value missing its closing quote', () => {
+ expect(compile('stage == "WON')).toEqual({
+ isValid: false,
+ errorMessage: 'A text value is missing its closing quote',
+ errorCode: 'UNCLOSED_TEXT',
+ });
+ });
+
+ it('should name the unexpected character or operator', () => {
+ expect(compile('stage = "WON"')).toEqual({
+ isValid: false,
+ errorMessage: 'Unexpected "="',
+ errorCode: 'INVALID_SYNTAX',
+ errorParams: { token: '=' },
+ });
+ expect(compile('stage == and "WON"')).toMatchObject({
+ errorParams: { token: 'and' },
+ });
});
it('should reject an empty expression', () => {
diff --git a/packages/twenty-shared/src/utils/validation-rule/compileValidationRuleExpression.ts b/packages/twenty-shared/src/utils/validation-rule/compileValidationRuleExpression.ts
index b14b4232fc4..ed9b8ee6f28 100644
--- a/packages/twenty-shared/src/utils/validation-rule/compileValidationRuleExpression.ts
+++ b/packages/twenty-shared/src/utils/validation-rule/compileValidationRuleExpression.ts
@@ -6,6 +6,7 @@ import { type ValidationRuleCompilationResult } from '@/types/ValidationRuleComp
import { type ValidationRuleFieldDescriptor } from '@/types/ValidationRuleFieldDescriptor';
import { hasValidationRuleBracketAccess } from '@/utils/validation-rule/hasValidationRuleBracketAccess';
import { evaluateValidationRuleExpression } from '@/utils/validation-rule/evaluateValidationRuleExpression';
+import { getValidationRuleSyntaxError } from '@/utils/validation-rule/getValidationRuleSyntaxError';
import { parseValidationRuleExpression } from '@/utils/validation-rule/parseValidationRuleExpression';
import { resolveValidationRuleIdentifierPath } from '@/utils/validation-rule/resolveValidationRuleIdentifierPath';
import { tokenizeValidationRuleExpression } from '@/utils/validation-rule/tokenizeValidationRuleExpression';
@@ -41,7 +42,7 @@ export const compileValidationRuleExpression = ({
} catch (error) {
return {
isValid: false,
- errorMessage: error instanceof Error ? error.message : String(error),
+ ...getValidationRuleSyntaxError({ expression, error }),
};
}
diff --git a/packages/twenty-shared/src/utils/validation-rule/getValidationRuleSyntaxError.ts b/packages/twenty-shared/src/utils/validation-rule/getValidationRuleSyntaxError.ts
new file mode 100644
index 00000000000..c9610d8f0b3
--- /dev/null
+++ b/packages/twenty-shared/src/utils/validation-rule/getValidationRuleSyntaxError.ts
@@ -0,0 +1,63 @@
+import { type ValidationRuleErrorCode } from '@/types/ValidationRuleErrorCode';
+import { type ValidationRuleErrorParams } from '@/types/ValidationRuleErrorParams';
+import { tokenizeValidationRuleExpression } from '@/utils/validation-rule/tokenizeValidationRuleExpression';
+import { isDefined } from '@/utils/validation/isDefined';
+
+const UNKNOWN_CHARACTER_PATTERN = /Unknown character "(.+)"/;
+const UNEXPECTED_TOKEN_PATTERN = /^unexpected T[A-Z]+: (.+)$/;
+const MISSING_CLOSING_PARENTHESIS_MESSAGE = 'Expected )';
+const END_OF_EXPRESSION_TOKEN_TYPE = 'TEOF';
+
+type ValidationRuleSyntaxError = {
+ errorMessage: string;
+ errorCode: ValidationRuleErrorCode;
+ errorParams?: ValidationRuleErrorParams;
+};
+
+export const getValidationRuleSyntaxError = ({
+ expression,
+ error,
+}: {
+ expression: string;
+ error: unknown;
+}): ValidationRuleSyntaxError => {
+ const parserMessage = error instanceof Error ? error.message : String(error);
+
+ const hasUnclosedText = tokenizeValidationRuleExpression(expression).some(
+ (token) => token.type === 'unclosedString',
+ );
+
+ if (hasUnclosedText) {
+ return {
+ errorMessage: 'A text value is missing its closing quote',
+ errorCode: 'UNCLOSED_TEXT',
+ };
+ }
+
+ if (
+ parserMessage.includes(END_OF_EXPRESSION_TOKEN_TYPE) ||
+ parserMessage.endsWith(MISSING_CLOSING_PARENTHESIS_MESSAGE)
+ ) {
+ return {
+ errorMessage: 'Expression is incomplete',
+ errorCode: 'INCOMPLETE_EXPRESSION',
+ };
+ }
+
+ const token =
+ parserMessage.match(UNKNOWN_CHARACTER_PATTERN)?.[1] ??
+ parserMessage.match(UNEXPECTED_TOKEN_PATTERN)?.[1];
+
+ if (isDefined(token)) {
+ return {
+ errorMessage: `Unexpected "${token}"`,
+ errorCode: 'INVALID_SYNTAX',
+ errorParams: { token },
+ };
+ }
+
+ return {
+ errorMessage: 'Expression is not valid',
+ errorCode: 'INVALID_SYNTAX',
+ };
+};