Skip to content

Commit d6c8c91

Browse files
committed
feat(serializer): ACLExtendedRule: Change validation to support multiple sources; Add validation to restrict to a single Source
1 parent 9f6463d commit d6c8c91

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

netbox_acls/api/serializers.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,26 +356,26 @@ def validate(self, data):
356356
"""
357357
Validate the ACLExtendedRule django model's inputs before allowing it to update the instance:
358358
- Check if action set to remark, but no remark set.
359-
- Check if action set to remark, but source_prefix set.
359+
- Check if action set to remark, but source set.
360360
- Check if action set to remark, but source_ports set.
361-
- Check if action set to remark, but destination_prefix set.
362361
- Check if action set to remark, but destination_ports set.
363362
- Check if action set to remark, but protocol set.
364-
- Check if action set to remark, but protocol set.
363+
- Check not more than one source is set.
365364
"""
366365
error_message = {}
367366

367+
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
368+
368369
if data.get("action") == "remark":
369370
# Check if action set to remark, but no remark set.
370371
if data.get("remark") is None:
371372
error_message["remark"] = [
372373
error_message_no_remark,
373374
]
374-
# Check if action set to remark, but source_prefix set.
375-
if data.get("source_prefix"):
376-
error_message["source_prefix"] = [
377-
error_message_action_remark_source_prefix_set,
378-
]
375+
# Check if action set to remark, but source set.
376+
if any(data.get(source) for source in sources):
377+
for source in sources:
378+
error_message[source] = [error_message_action_remark_source_set]
379379
# Check if action set to remark, but source_ports set.
380380
if data.get("source_ports"):
381381
error_message["source_ports"] = [
@@ -396,6 +396,12 @@ def validate(self, data):
396396
error_message["protocol"] = [
397397
"Action is set to remark, Protocol CANNOT be set.",
398398
]
399+
400+
# Check not more than one source is set.
401+
if sum(bool(data.get(source)) for source in sources) > 1:
402+
for source in sources:
403+
error_message[source] = [error_message_sources_more_than_one]
404+
399405

400406
if error_message:
401407
raise serializers.ValidationError(error_message)

0 commit comments

Comments
 (0)