The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(currency-field): reject unknown currency codes on currency fields (#29126)
Etienne7ee323e20def
diff --git a/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/__tests__/validate-currency-field-or-throw.util.spec.ts b/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/__tests__/validate-currency-field-or-throw.util.spec.tsindex 32cea3a0846..eb9a80e60cd 100644--- a/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/__tests__/validate-currency-field-or-throw.util.spec.ts+++ b/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/__tests__/validate-currency-field-or-throw.util.spec.ts@@ -27,9 +27,30 @@ describe('validateCurrencyFieldOrThrow', () => {expect(result).toEqual(currencyValue);});++ it('should accept a null or empty currencyCode', () => {+ expect(+ validateCurrencyFieldOrThrow({ currencyCode: null }, 'testField'),+ ).toEqual({ currencyCode: null });+ expect(+ validateCurrencyFieldOrThrow({ currencyCode: '' }, 'testField'),+ ).toEqual({ currencyCode: '' });+ });});describe('invalid inputs', () => {+ it.each(['XYZ', 'usd', 'toString'])(+ 'should throw when currencyCode is %s',+ (currencyCode) => {+ expect(() =>+ validateCurrencyFieldOrThrow(+ { amountMicros: 1000000, currencyCode },+ 'testField',+ ),+ ).toThrow(CommonQueryRunnerException);+ },+ );+it('should throw when value is not an object', () => {expect(() =>validateCurrencyFieldOrThrow('not an object', 'testField'),diff --git a/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/validate-currency-field-or-throw.util.ts b/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/validate-currency-field-or-throw.util.tsindex 910f046af59..2b53dd319ff 100644--- a/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/validate-currency-field-or-throw.util.ts+++ b/packages/twenty-server/src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/validate-currency-field-or-throw.util.ts@@ -1,5 +1,6 @@import { msg } from '@lingui/core/macro';-import { isNull } from '@sniptt/guards';+import { isNonEmptyString, isNull } from '@sniptt/guards';+import { CurrencyCode } from 'twenty-shared/constants';import { validateNumericFieldOrThrow } from 'src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/validate-numeric-field-or-throw.util';import { validateRawJsonFieldOrThrow } from 'src/engine/api/common/common-args-processors/data-arg-processor/validator-utils/validate-raw-json-field-or-throw.util';@@ -25,9 +26,24 @@ export const validateCurrencyFieldOrThrow = (case 'amountMicros':validateNumericFieldOrThrow(subFieldValue, `${fieldName}.${subField}`);break;- case 'currencyCode':- validateTextFieldOrThrow(subFieldValue, `${fieldName}.${subField}`);+ case 'currencyCode': {+ const currencyCode = validateTextFieldOrThrow(+ subFieldValue,+ `${fieldName}.${subField}`,+ );++ if (+ isNonEmptyString(currencyCode) &&+ !Object.values<string>(CurrencyCode).includes(currencyCode)+ ) {+ throw new CommonQueryRunnerException(+ `Invalid currency code "${currencyCode}" for currency field "${fieldName}"`,+ CommonQueryRunnerExceptionCode.INVALID_ARGS_DATA,+ { userFriendlyMessage: msg`Invalid value for currency.` },+ );+ }break;+ }default:throw new CommonQueryRunnerException(