mixxorz / django-service-objects

Service objects for Django
MIT License
337 stars 29 forks source link

Add support for validating None declared fields. #65

Open jackton1 opened 3 years ago

jackton1 commented 3 years ago

The services shouldn't allow more fields than expected when calling execute

class MyService(Service):
    field_a = forms.CharField(...)

Call

MyService.execute({ 'field_y': 1, 'field_a'; 2 })

Expectations: Raises an exception

'Invalid field provided "field_y": Please provide one of the following fields: ({...})'
peterfarrell commented 1 year ago

The base Service object inherits from django.forms.BaseForm. Django Forms allow you pass anything to the data attribute of __init__ . Making this change likely breaks backwards compatibility as the Service object no longer behaves as expected.

@jackton1 What's the use case for things being more restrict? Base an additional attribute of Service called strict_inputs that defaults to False for backwards compatibility would be an acceptable PR?

jackton1 commented 1 year ago

A common issue here has been running into a bug where the field key provided when invoking the service didn’t match the defined field name and there wasn’t any errors reported since the field wasn’t required.

peterfarrell commented 1 year ago

@jackton1 makes sense - I think we'd have to add a new switch to indicate you wanted that behavior of strict inputs. Personally I know it would break some of the apps without it. How about a PR?