Skip to content Skip to sidebar Skip to footer

How Do I Return A Custom Rule-name/error-code Using Cerberus?

Am validating .csv file and I want to give the results of the validation in a format the user is accustomed to. To make use of Cerberus, I've let the user to define the validation

Solution 1:

I was at the same space as you are, I'll tell you what I did.

Created a custom error_handler and prepend the error messages with human readable keys.

from cerberus.errors import BasicErrorHandler

classCustomErrorHandler(BasicErrorHandler):
        def__init__(self, schema):
            self.custom_defined_schema = schema

        def_format_message(self, field, error):
            return self.custom_defined_schema[field].get('meta', {}).get('rule_name', field) + ': ' + super(CustomErrorHandler, self)._format_message(field, error)

val = Validator(schema, error_handler=CustomErrorHandler(schema))

This is what I did, hopefully it can be of use to you.

Post a Comment for "How Do I Return A Custom Rule-name/error-code Using Cerberus?"