question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Improved API for registering field validator methods

See original GitHub issue

Status 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’s validators attribute, there would be no need for this code.
  • Better introspection of fields. Again, since Field#validator() appends to Field#validators, code that introspects fields (e.g. apispec) can find all of a field’s validators in one place.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
immerrrcommented, Jan 20, 2016

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.

0reactions
sloriacommented, Sep 11, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found