marshmallow-code / marshmallow-code/marshmallow

Is there a way to pre-define built-in validators that can be used multiple times?

Open
#2,829 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
7.2k
Forks
739
Avg merge
1d 23h
Merged PRs (30d)
7

Description

I'm working on an API based flask app with flask-smorest and marshmallow validations. The issue I'm running into, is that many api endpoints require the same type of data, and I don't want to continually copy/paste the same validations over and over. Something like below would happen....

from marshmallow import Schema, fields, validate

class api1():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate.Regexp(regex=r"^REQUEST\d{3,9}$", error="Input string didn't match required format - REQUEST12345"))	
		
class api2():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate.Regexp(regex=r"^REQUEST\d{3,9}$", error="Input string didn't match required format - REQUEST12345"))		

What I want to do is create some sort of function or class BEFORE I define every api field/schema that I could recall.

Below is the idea, though I don't believe it works.

from marshmallow import Schema, fields, validate

class repetative_validators():
	request_number = validate.Regexp(regex=r"^REQUEST\d{3,9}$", error="Input string didn't match required format - REQUEST12345"))

class api1():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=repetative_validators().request_number)
		
class api2():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=repetative_validators().request_number)	

I was able to successfully test creating my own function for validation as described here, but was hoping to use the built in validators.

from marshmallow import Schema, fields, validate, ValidationError
import re

def validate_request_number(value):
	if not re.match(r"^REQUEST\d{3,9}$"):
		raise ValidationError("Input string didn't match required format - REQUEST12345")
		
class api1():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate_request_number)
		
class api2():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate_request_number)		

If anyone can link to appropriate documentation or an example that would be helpful. I haven't found anything like this in the marshmallow docs yet

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked marshmallow.validate documentation and the existing validator entry points. Determine whether reusable built-in validators are already supported and document an appropriate example for applying one across schemas; done means the recommended pattern is clear from the documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.