Simple data validation

一曲冷凌霜 提交于 2019-11-28 20:53:13

问题


I'm writing a python module that will contain some functions that will manipulate a mongodb database.

How can I go about validating input data passed to that function before saving it in database?

For example, lets say one of the function in module is createUser(user) which accepts a python dictionary as argument. This dictionary contains user information to save in the database. I want to create an automated validation routine which checks that the dictionary structure matches the database structure.


回答1:


I released "pyvaru" (https://github.com/daveoncode/pyvaru) a couple of days ago, it is a simple, flexible and unobtrusive data validation library for Python 3 (3.4+), based on the concept of validation rules.

Quote from the doc:

Given an existing model to validate, like the one below (but it could be a simple dictionary or any data structure since pyvaru does not make any assumption on the data format):

class User:
    def __init__(self, first_name: str, last_name: str, date_of_birth: datetime, sex: str):
        self.first_name = first_name
        self.last_name = last_name
        self.date_of_birth = date_of_birth
        self.sex = sex

We have to define a validator, by implementing the get_rules() method and for each field we want to validate we have to provide one or more proper rule(s).

from pyvaru import Validator
from pyvaru.rules import TypeRule, FullStringRule, ChoiceRule, PastDateRule

class UserValidator(Validator):
    def get_rules(self) -> list:
        user = self.data # type: User
        return [
            TypeRule(apply_to=user,
                     label='User',
                     valid_type=User,
                     error_message='User must be an instance of user model.',
                     stop_if_invalid=True),
            FullStringRule(user.first_name, 'First name'),
            FullStringRule(user.last_name, 'Last name'),
            ChoiceRule(user.sex, 'Sex', choices=('M', 'F')),
            PastDateRule(user.date_of_birth, 'Date of birth')
        ]

Finally we have two choices regarding how to use our custom validator:

As a context processor:

with UserValidator(user):
    # do whatever you want with your valid model

In this case the code inside with will be executed only if the validation succeed, otherwise a ValidationException (containing a validation_result property with the appropriate report) is raised.

By invoking the validate() method (which returns a ValidationResult)

validation = UserValidator(user).validate()
if validation.is_successful():
    # do whatever you want with your valid model
else:
    # you can take a proper action and access validation.errors
    # in order to provide a useful message to the application user,
    # write logs or whatever

Assuming we have a instance of an User configured as the one below:

user = User(first_name=' ',
            last_name=None,
            date_of_birth=datetime(2020, 1, 1),
            sex='unknown')

By running a validation with the previous defined rules we will obtain a ValidationResult with the following errors:

{
    'First name': ['String is empty.'],
    'Last name': ['Not a string.'],
    'Sex': ['Value not found in available choices.'],
    'Date of birth': ['Not a past date.']
}


来源:https://stackoverflow.com/questions/12235120/simple-data-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!