From 6ea25a96a365e9d047c1ff00984e9dda79d1ef90 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 7 May 2024 14:38:52 -0700 Subject: [PATCH] Fix: make backend monetary validation accept unpadded decimals (#6626) --- .../common/input/monetary/monetary.component.ts | 10 +++++++++- src/documents/serialisers.py | 2 +- src/documents/tests/test_api_custom_fields.py | 16 ---------------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src-ui/src/app/components/common/input/monetary/monetary.component.ts b/src-ui/src/app/components/common/input/monetary/monetary.component.ts index 4ed32e7a1..56f0dd28e 100644 --- a/src-ui/src/app/components/common/input/monetary/monetary.component.ts +++ b/src-ui/src/app/components/common/input/monetary/monetary.component.ts @@ -17,7 +17,15 @@ import { getLocaleCurrencyCode } from '@angular/common' }) export class MonetaryComponent extends AbstractInputComponent { public currency: string = '' - public monetaryValue: string = '' + + public _monetaryValue: string = '' + public get monetaryValue(): string { + return this._monetaryValue + } + public set monetaryValue(value: string) { + if (value) this._monetaryValue = value + } + defaultCurrencyCode: string constructor(@Inject(LOCALE_ID) currentLocale: string) { diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index b3eace7c9..213e3cc27 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -547,7 +547,7 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer): except Exception: # If that fails, try to validate as a monetary string RegexValidator( - regex=r"^[A-Z]{3}-?\d+(\.\d{2,2})$", + regex=r"^[A-Z]{3}-?\d+(\.\d{1,2})$", message="Must be a two-decimal number with optional currency code e.g. GBP123.45", )(data["value"]) elif field.data_type == CustomField.FieldDataType.STRING: diff --git a/src/documents/tests/test_api_custom_fields.py b/src/documents/tests/test_api_custom_fields.py index 2885d9071..0b2f99b35 100644 --- a/src/documents/tests/test_api_custom_fields.py +++ b/src/documents/tests/test_api_custom_fields.py @@ -499,22 +499,6 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase): self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) - resp = self.client.patch( - f"/api/documents/{doc.id}/", - data={ - "custom_fields": [ - { - "field": custom_field_money.id, - # Too few places past decimal - "value": "GBP12.1", - }, - ], - }, - format="json", - ) - - self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) - resp = self.client.patch( f"/api/documents/{doc.id}/", data={