diff --git a/doc/release/RELEASE-NOTES.md b/doc/release/RELEASE-NOTES.md index 957406e59..90d7140cb 100644 --- a/doc/release/RELEASE-NOTES.md +++ b/doc/release/RELEASE-NOTES.md @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html * [OSDEV-1747](https://opensupplyhub.atlassian.net/browse/OSDEV-1747) - All SLC pages have been made accessible only to authorized users. * [OSDEV-1777](https://opensupplyhub.atlassian.net/browse/OSDEV-1777) - A consistent URL style was established across all pages of the SLC workflow. After the changes, the URL begins from `/contribute/single-location/`. * [OSDEV-1678](https://opensupplyhub.atlassian.net/browse/OSDEV-1678) - Added asterisks next to each required form field (Name, Address, and Country) on the "Production Location Information" page. Highlighted an empty field and displayed an error message if it loses focus. +* [OSDEV-1778](https://opensupplyhub.atlassian.net/browse/OSDEV-1778) - Fixed the validation for number of workers field in POST, PATCH production locations API. The min field must be less than or equal to the max field. ### What's new * *Describe what's new here. The changes that can impact user experience should be listed in this section.* diff --git a/src/django/api/serializers/v1/number_of_workers_serializer.py b/src/django/api/serializers/v1/number_of_workers_serializer.py index 00f84ff14..7dfa6fd29 100644 --- a/src/django/api/serializers/v1/number_of_workers_serializer.py +++ b/src/django/api/serializers/v1/number_of_workers_serializer.py @@ -4,19 +4,66 @@ class NumberOfWorkersSerializer(serializers.Serializer): min = serializers.IntegerField( min_value=1, - required=True, - error_messages={ - 'required': 'The min field is required!', - } ) max = serializers.IntegerField( min_value=1, - required=True, - error_messages={ - 'required': 'The max field is required!', - } ) - @staticmethod - def validate_object(value): - return isinstance(value, dict) + def validate(self, data): + min_value = data.get('min') + max_value = data.get('max') + + if ( + min_value is not None + and max_value is not None + and min_value > max_value + ): + raise serializers.ValidationError( + {"min": ('The min field must be less than or equal' + ' to the max field.')} + ) + + return data + + def to_internal_value(self, data): + if not isinstance(data, dict): + raise serializers.ValidationError( + "Invalid data. Expected a dictionary(object).") + + min_value = data.get('min') + max_value = data.get('max') + errors = [] + + if min_value is None: + errors.append({ + "field": "min", + "detail": + "The min field is required!" + }) + + if max_value is None: + errors.append({ + "field": "max", + "detail": + "The max field is required!" + }) + + if min_value is not None and not isinstance(min_value, int): + errors.append({ + "field": "min", + "detail": + "The min field must be an integer." + }) + + if max_value is not None and not isinstance(max_value, int): + errors.append({ + "field": "max", + "detail": + "The max field must be an integer." + }) + + if len(errors) > 0: + raise serializers.ValidationError({"field": self.field_name, + "errors": errors}) + + return super().to_internal_value(data) diff --git a/src/django/api/serializers/v1/production_location_schema_serializer.py b/src/django/api/serializers/v1/production_location_schema_serializer.py index 1c298ed3f..0c0f5a648 100644 --- a/src/django/api/serializers/v1/production_location_schema_serializer.py +++ b/src/django/api/serializers/v1/production_location_schema_serializer.py @@ -52,10 +52,6 @@ class ProductionLocationSchemaSerializer(serializers.Serializer): processing_type = StringOrListField(required=False) number_of_workers = NumberOfWorkersSerializer( required=False, - validators=[NumberOfWorkersSerializer.validate_object], - error_messages={ - 'invalid': 'Invalid data. Expected a dictionary(object).' - }, ) coordinates = CoordinatesSerializer( required=False,