Skip to content

Commit

Permalink
Reject unknown keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Dec 10, 2024
1 parent 59dad0f commit 7b9fb52
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions arches/app/models/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from copy import deepcopy

from django.db.models import F
from rest_framework.exceptions import ValidationError
from rest_framework import fields
from rest_framework import renderers
from rest_framework import serializers
Expand Down Expand Up @@ -82,6 +83,13 @@ def build_relational_field(self, field_name, relation_info):
)
return ret

def validate(self, data):
if hasattr(self, "initial_data") and (
unknown_keys := set(self.initial_data) - set(self.fields)
):
raise ValidationError({unknown_keys.pop(): "Unexpected field"})
return data


class ArchesModelSerializer(serializers.ModelSerializer):
legacyid = serializers.CharField(max_length=255, required=False, allow_null=True)
Expand Down Expand Up @@ -143,6 +151,13 @@ class Meta:
allow_null=True,
)

def validate(self, data):
if hasattr(self, "initial_data") and (
unknown_keys := set(self.initial_data) - set(self.fields)
):
raise ValidationError({unknown_keys.pop(): "Unexpected field"})
return data

def create(self, validated_data):
meta = self.__class__.Meta
instance_without_tile_data = super().create(validated_data)
Expand Down

0 comments on commit 7b9fb52

Please sign in to comment.