Building Your Own Custom Validator

Welcome back! In this lesson, we will focus on building custom validators to handle specific data validation needs that built-in validators in Marshmallow may not cover. By creating custom validators, you can enforce business rules and data integrity specific to your application.

Let's get started!

Basic Setup

Before we dive into custom validators, let's briefly recap our basic Flask setup using a mock database:

Now, we are ready to define and use custom validators.

Custom Validators with Marshmallow

Marshmallow allows us to create custom validators using the @validates decorator. This decorator is applied to a function within your schema that will validate a specific field. If the value doesn’t meet the criteria, the function raises a ValidationError.

Here is a generic example:

In this example:

  • example_field is a simple string field.
  • We use the @validates decorator on the validate_example_field function to indicate that it validates the example_field.
  • The function checks a condition and raises a ValidationError if the condition is not met.

Let’s now create custom validators for our specific use case.

Creating a Custom Validator for the Username Field

Let's start by building a custom validator for the username field. This validator will ensure that the username is at least three characters long and contains only alphanumeric characters (letters and numbers).

  • In the code above we define a custom validator for the username field using the @validates decorator.
  • The validate_username method performs multiple checks by chaining if statements. It first checks if the username is at least three characters long, and then checks if it contains only alphanumeric characters.
  • If the username fails any of these checks, a ValidationError is raised with an appropriate error message.
Creating a Custom Validator for the Email Field

Next, let's create a custom validator for the email field. This validator will ensure that email addresses belong to a specific domain, @example.com.

  • Here we define a custom validator for the email field using the @validates decorator.
  • The validate_email method checks if the email value ends with the domain @example.com.
  • If the email does not meet this condition, a ValidationError is raised with an error message specifying the requirement.
Response from Custom Validation

When a validation error occurs, the response will indicate the specific reasons for the failure. For example:

Summary and Practice Section

In this lesson, we learned how to build custom validators using Marshmallow in a Flask application. We started with a basic setup, then created a custom validator for the username field to ensure it is at least three characters long and alphanumeric. We also developed a custom validator for the email field to ensure the email addresses belong to the specific domain @example.com. By integrating these custom validators into our schema, we can enforce specific business rules and data integrity.

Now, it's your turn to practice creating and validating custom fields in the upcoming exercises. Keep experimenting and happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal