RFC: Improved API for registering field validator methods
See original GitHub issueStatus quo
Validator methods are registered by decorating them with @validates('my_field_name')
.
from marshmallow import fields, Schema, ValidationError, validates
class ItemSchema(Schema):
quantity = fields.Int()
@validates('quantity')
def validate_quantity(self, value):
if value > 30:
raise ValidationError('Quantity must not be greater than 30')
Proposed API
Validator methods are registered by decorating them with @my_field.validator
.
from marshmallow import fields, Schema, ValidationError
class ItemSchema(Schema):
quantity = fields.Int()
@quantity.validator
def validate_quantity(self, value):
if value > 30:
raise ValidationError('Quantity must not be greater than 30')
Benefits
- Less prone to user error (no magic strings).
- Removes redundant code. Since
Field#validator()
decorator would just append to a field’svalidators
attribute, there would be no need for this code. - Better introspection of fields. Again, since
Field#validator()
appends toField#validators
, code that introspects fields (e.g. apispec) can find all of a field’s validators in one place.
Issue Analytics
- State:
- Created 8 years ago
- Comments:16 (16 by maintainers)
Top Results From Across the Web
RFC 9048 - Improved Extensible Authentication Protocol ...
Improved Extensible Authentication Protocol Method for 3GPP Mobile Network Authentication and Key Agreement (EAP-AKA') (RFC 9048, October 2021)
Read more >PHP RFC: Add validation functions to filter module
This RFC encourage users to proper secure coding by adding more suitable functions and filter for input validations. Secure coding basics. A ...
Read more >RFC 9205: Building Protocols with HTTP
Applications often use HTTP as a substrate to create HTTP-based APIs. This document specifies best practices for writing specifications that use HTTP to ......
Read more >RFC: V8 · Discussion #7433 · react-hook-form/react ... - GitHub
This seems to fill in the gaps I'm noticing with the field array API. Not suggesting these exact methods. Just the idea is...
Read more >Validation - Laravel - The PHP Framework For Web Artisans
As you can see, the validation rules are passed into the validate method. Don't worry - all available validation rules are documented. Again,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Less magic strings is good, so it seems a nice idea to replace
@validates('quantity')
with@validates(quantity)
.I would however like to point out that schema-level validators may check how two or more fields interact with each other. A recently introduced feature allows skipping schema-level validation if the validation of the field itself failed, it can be generalized to multiple fields, too. There’s not much sense in checking how two, say, integer parameters interact with each other if one of those is unexpectedly a dictionary. Current API could support that naturally with
@validates([quantity, quality, speed])
, but it is not that obvious to me how to do that with the proposed one.Discussed offline: it’s not clear that the OP was a good idea. This API gets confusing when fields are inherited.
validates('string')
looks magic on the surface, but at least it’s clear what its behavior is.Closing this for now.