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.

Field validators as schema methods?

See original GitHub issue

Often, validators and preprocessors only apply to a single Schema, so it may make sense to define them within the Schema–rather than as separate functions–for better cohesion.

Method names could be passed to __validators__, et. al, like so:

class UserSchema(Schema):
    __validators__ = ['validate_schema']

    def validate_schema(self, data):
        # ...

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:37 (37 by maintainers)

github_iconTop GitHub Comments

1reaction
philtaycommented, May 11, 2015

@sloria What about something like this?

class User(Schema):
    name = fields.String()
    age = fields.Integer()

    @validator('age')
    def ensure_adult(self, value):
        if value < 18:
            raise ValidationError('Too young!')

It would be a nice convenience.

0reactions
taioncommented, Jun 14, 2015

Awesome! Sorry I didn’t get around to coding this up. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose v6.8.0: Validation
Mongoose registers validation as a pre('save') hook on every schema by default ... a field as invalid (causing validation to fail) by using...
Read more >
Schema Validation — MongoDB Manual
Schema validation lets you create validation rules for your fields, such as allowed data types and value ranges. MongoDB uses a flexible schema...
Read more >
How To Use Schema Validation in MongoDB - DigitalOcean
MongoDB has a feature called schema validation that allows you to apply constraints on your documents' structure. Schema validation is built ...
Read more >
Mongoose Schema Types, Validation & Queries Tutorial
Every Schema Types in Mongoose refers to a Collection and organizes the structure of a document. It supports data validation, queries, field ......
Read more >
Schema Validation in MongoDB Atlas - Topcoder
Schema validation is a key concept while developing any backend application. Sometimes clients provide inputs which are not according to our ...
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