0

In my web application's backend code (written in Python using Flask and Marshmallow), I first call Marshmallow's Schema.validate() and if I get a dictionary of errors, I send the errors to the Frontend.

If there are no validation errors, I proceed to call Schema.load(). But all the custom validation functions get called once again by Marshmallow, perhaps because it tries to validate the Schema again as part of the load process.

Is there a way I can tell Schema.load() that the data is already validated and it need not trigger Schema.validate() again?

And if there is no way to skip the Schema.validate() during Schema.load(), then if I intend to load the Schema eventually, is there any benefit to call Schema.validate() explicitly; I may as well call Schema.load() directly and catch any ValidationErrors.

AllSolutions
  • 1,176
  • 5
  • 19
  • 40

1 Answers1

1

And if there is no way to skip the Schema.validate() during Schema.load(), then if I intend to load the Schema eventually, is there any benefit to call Schema.validate() explicitly; I may as well call Schema.load() directly and catch any ValidationErrors.

Exactly. This is the way to go. Load with the schema and catch validation errors.

We (marshmallow devs) provide validate as a convenience but I don't have a real-life use case in mind.

You may want to have a look at webargs, which provides a nice integration of marshmallow to the web framework. Specify the schema to use and webargs injects the values in the view func or returns a 422 with the validation errors as payload.

Jérôme
  • 13,328
  • 7
  • 56
  • 106