Validate list in marshmallow

2024/9/8 11:19:50

currently I am using marshmallow schema to validate the request, and I have this a list and I need to validate the content of it.

class PostValidationSchema(Schema):checks = fields.List(fields.String(required=True))

the checks field is a list it should only contain these specific values ["booking", "reservation", "flight"]

Answer

If you mean to check the list only has those three elements in that order, then use Equal validator.

from marshmallow import Schema, fields, validateclass PostValidationSchema(Schema):checks = fields.List(fields.String(required=True),validate=validate.Equal(["booking", "reservation", "flight"]))schema = PostValidationSchema()schema.load({"checks": ["booking", "reservation", "flight"]})  # OK
schema.load({"checks": ["booking", "reservation"]})  # ValidationError

If the list can have any number of elements and those can only be one of those three specific values, then use OneOf validator.

from marshmallow import Schema, fields, validateclass PostValidationSchema(Schema):checks = fields.List(fields.String(required=True,validate=validate.OneOf(["booking", "reservation", "flight"])),)schema = PostValidationSchema()schema.load({"checks": ["booking", "reservation", "flight"]})  # OK
schema.load({"checks": ["booking", "reservation"]})  # OK
schema.load({"checks": ["booking", "dummy"]})  # ValidationError
https://en.xdnf.cn/q/73333.html

Related Q&A

Save unicode in redis but fetch error

Im using mongodb and redis, redis is my cache.Im caching mongodb objects with redis-py:obj in mongodb: {uname: umatch, usection_title: u\u6d3b\u52a8, utitle: u\u6bd4\u8d5b, usection_id: 1, u_id: Objec…

Authentication with public keys and cx_Oracle using Python

Ive Googled a bit but I havent found any substantial results. Is it possible to use key-based authentication to connect to an Oracle server using Python? My objective is to be able to automate some re…

No luck pip-installing pylint for Python 3

Im interested in running a checker over my Python 3 code to point out possible flaws. PyChecker does not work with Python 3. I tried to pip-install Pylint, but this fails. The error message does not he…

How to use tf.nn.embedding_lookup_sparse in TensorFlow?

We have tried using tf.nn.embedding_lookup and it works. But it needs dense input data and now we need tf.nn.embedding_lookup_sparse for sparse input.I have written the following code but get some erro…

Saving Python SymPy figures with a specific resolution/pixel density

I am wondering if there is a way to change the pixel density/resolution of sympy plots. For example, lets consider the simple code snippet below:import sympy as sypx = syp.Symbol(x) miles_to_km = x * 1…

Matplotlib boxplot width in log scale

I am trying to plot a boxplot with logarithmic x-axis. As you can see on the example below width of each box decreases because of the scale. Is there any way to make the width of all boxes same?

How to enable and disable Intel MKL in numpy Python?

I want to test and compare Numpy matrix multiplication and Eigen decomposition performance with Intel MKL and without Intel MKL. I have installed MKL using pip install mkl (Windows 10 (64-bit), Python …

getdefaultlocale returning None when running sync.db on Django project in PyCharm

OSX 10.7.3, PyCharm version 2.5 build PY 117.200Ill run through how I get the error:I start a new project Create a new VirtualEnv and select Python 2.7 as my base interpreter (leave inherit global pack…

Redirecting an old URL to a new one with Flask micro-framework

Im making a new website to replace a current one, using Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).The core functionality and many pages are the same. However by using…

python decimals - rounding to nearest whole dollar (no cents) - with ROUND_HALF_UP

Im trying to use Decimal.quantize() to achieve the following: -For any amount of money, expressed as a python decimal of default precision, I want to round it using decimal.ROUND_HALF_UP so that it has…