Skip to content

The open alternative to Salesforce, designed for AI.

Leave rich text rules to the server until it computes the markdown (#27582)

Thomas Trompette585079f2445e
diff --git a/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts b/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts
index f127e992c07..8904082daf2 100644
--- a/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts
+++ b/packages/twenty-front/src/modules/validation-rules/utils/__tests__/computeDraftValidationRuleViolations.test.ts
@@ -19,6 +19,11 @@ const FIELDS = [
type: FieldMetadataType.TEXT,
universalIdentifier: 'tagline',
},
+ {
+ name: 'description',
+ type: FieldMetadataType.RICH_TEXT,
+ universalIdentifier: 'description',
+ },
{
name: 'createdAt',
type: FieldMetadataType.DATE_TIME,
@@ -96,6 +101,13 @@ const TAGLINE_RULE = {
message: 'A company needs a tagline',
};
+const DESCRIPTION_RULE = {
+ ...AMOUNT_RULE,
+ id: 'description-rule',
+ expression: 'not isEmpty(description)',
+ message: 'A deal needs a description',
+};
+
describe('computeDraftValidationRuleViolations', () => {
it('should report the rule when the draft violates it', () => {
expect(
@@ -217,6 +229,39 @@ describe('computeDraftValidationRuleViolations', () => {
).toEqual([]);
});
+ it('should leave a rich text rule to the server until the server computes its markdown', () => {
+ const descriptionAwaitingMarkdown = {
+ blocknote:
+ '[{"type":"paragraph","content":[{"type":"text","text":"Hello"}]}]',
+ markdown: null,
+ };
+
+ expect(
+ compute({ description: descriptionAwaitingMarkdown }, [DESCRIPTION_RULE]),
+ ).toEqual([]);
+ expect(
+ compute(
+ {
+ description: descriptionAwaitingMarkdown,
+ stage: 'CUSTOMER',
+ amount: { amountMicros: null, currencyCode: 'USD' },
+ },
+ [DESCRIPTION_RULE, AMOUNT_RULE],
+ ).map((violation) => violation.ruleId),
+ ).toEqual(['amount-rule']);
+ });
+
+ it('should still report an empty rich text', () => {
+ expect(
+ compute({}, [DESCRIPTION_RULE]).map((violation) => violation.ruleId),
+ ).toEqual(['description-rule']);
+ expect(
+ compute({ description: { blocknote: null, markdown: null } }, [
+ DESCRIPTION_RULE,
+ ]).map((violation) => violation.ruleId),
+ ).toEqual(['description-rule']);
+ });
+
it('should skip inactive rules', () => {
expect(
compute({ stage: 'CUSTOMER' }, [{ ...AMOUNT_RULE, isActive: false }]),
diff --git a/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts b/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts
index 14c86e89218..79f1069b6de 100644
--- a/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts
+++ b/packages/twenty-front/src/modules/validation-rules/utils/computeDraftValidationRuleViolations.ts
@@ -1,15 +1,17 @@
import { type FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { type DraftValidationRuleViolation } from '@/validation-rules/types/DraftValidationRuleViolation';
import { type ValidationRule } from '@/validation-rules/types/ValidationRule';
-import { isUndefined } from '@sniptt/guards';
+import { isNonEmptyString, isUndefined } from '@sniptt/guards';
import {
fieldMetadataDefaultValueFunctionName,
+ FieldMetadataType,
type ValidationRuleFieldDescriptor,
} from 'twenty-shared/types';
import {
compileValidationRuleExpression,
evaluateValidationRuleExpression,
isDefined,
+ isPlainObject,
} from 'twenty-shared/utils';
import { stripSimpleQuotesFromStringRecursive } from '~/utils/string/stripSimpleQuotesFromString';
@@ -28,6 +30,41 @@ const isServerFilledField = (
fieldMetadataItem.isSystem ||
FUNCTION_DEFAULT_VALUES.includes(fieldMetadataItem.defaultValue);
+const IS_DRAFT_VALUE_COMPLETED_BY_SERVER_BY_FIELD_TYPE: Partial<
+ Record<FieldMetadataType, (draftValue: unknown) => boolean>
+> = {
+ [FieldMetadataType.RICH_TEXT]: (draftValue) =>
+ isPlainObject(draftValue) &&
+ isNonEmptyString(draftValue.blocknote) &&
+ !isNonEmptyString(draftValue.markdown),
+};
+
+const computeFieldNamesResolvedByServer = ({
+ draftRecord,
+ fields,
+ fieldMetadataItems,
+}: {
+ draftRecord: Record<string, unknown>;
+ fields: ValidationRuleFieldDescriptor[];
+ fieldMetadataItems: DraftFieldMetadataItem[];
+}): string[] => [
+ ...fieldMetadataItems
+ .filter(
+ (fieldMetadataItem) =>
+ isServerFilledField(fieldMetadataItem) &&
+ !(fieldMetadataItem.name in draftRecord),
+ )
+ .map((fieldMetadataItem) => fieldMetadataItem.name),
+ ...fields
+ .filter(
+ (field) =>
+ IS_DRAFT_VALUE_COMPLETED_BY_SERVER_BY_FIELD_TYPE[field.type]?.(
+ draftRecord[field.name],
+ ) === true,
+ )
+ .map((field) => field.name),
+];
+
const withStaticDefaultValues = ({
draftRecord,
fieldMetadataItems,
@@ -58,12 +95,12 @@ const canEvaluateOnDraft = ({
expression,
fields,
draftRecord,
- serverFilledFieldNames,
+ fieldNamesResolvedByServer,
}: {
expression: string;
fields: ValidationRuleFieldDescriptor[];
draftRecord: Record<string, unknown>;
- serverFilledFieldNames: string[];
+ fieldNamesResolvedByServer: string[];
}): boolean => {
const compilationResult = compileValidationRuleExpression({
expression,
@@ -85,12 +122,11 @@ const canEvaluateOnDraft = ({
return typeof relatedRecord === 'object' && isDefined(relatedRecord);
});
- const isEveryServerFilledFieldInDraft = bindingPaths
+ const isNoReferencedFieldResolvedByServer = bindingPaths
.map((bindingPath) => bindingPath.split('.')[0])
- .filter((fieldName) => serverFilledFieldNames.includes(fieldName))
- .every((fieldName) => fieldName in draftRecord);
+ .every((fieldName) => !fieldNamesResolvedByServer.includes(fieldName));
- return isEveryReferencedRelationLoaded && isEveryServerFilledFieldInDraft;
+ return isEveryReferencedRelationLoaded && isNoReferencedFieldResolvedByServer;
};
const withRelationPresenceFromJoinColumns = ({
@@ -136,9 +172,11 @@ export const computeDraftValidationRuleViolations = ({
fieldMetadataItems,
});
- const serverFilledFieldNames = fieldMetadataItems
- .filter(isServerFilledField)
- .map((fieldMetadataItem) => fieldMetadataItem.name);
+ const fieldNamesResolvedByServer = computeFieldNamesResolvedByServer({
+ draftRecord: draftRecordWithDefaultValues,
+ fields,
+ fieldMetadataItems,
+ });
return validationRules
.filter((validationRule) => validationRule.isActive)
@@ -147,7 +185,7 @@ export const computeDraftValidationRuleViolations = ({
expression: validationRule.expression,
fields,
draftRecord: draftRecordWithDefaultValues,
- serverFilledFieldNames,
+ fieldNamesResolvedByServer,
}),
)
.filter(