Validation happens on load and loads too
No, it doesn't.
@lafrech I might be confused, but then what is the meaning of this? Is that not Schema validation on load and loads?
I'm the one who was confused. Validation happens on load and not on dump. (Validation on dump is a recurrent question, hence my confusion. Sorry about that.)
I think the doc is wrong. It should read load instead of dump.
Summary:
Thanks for pointing this out.
Gotcha, no worries.
I'm afraid I'm still stuck though. I'm using flask-marshmallow for my Schemas, and the error messages are not changing when I add an error_messages class variable. This is on load.
For reference, my code is this:
class UserLoginSchema(ma.Schema):
email = ma.String(required=True)
password = ma.String(required=True)
error_messages = {
"required": "input required",
}
Originally I was trying to do this in a BaseSchema I've created (to overload the error_messages in all my Schemas) but that wasn't working in for some reason, and neither is specifying it directly in this derived class as shown in the docs. Can you confirm that you are able to overload the error_messages using this class variable method?
I thought it could somehow be the fact that I'm using flask-marshmallow, but it didn't work when I tried deriving my schema from marshmallow.Schema directly either.
Really though I want to be able to do it in a base class using SchemaOpts + OPTIONS_CLASS.
@acnebs
"Required" validation is field-level, not schema-level, so you need to set the default error messages on Field.
from marshmallow import Schema, fields
fields.Field.default_error_messages["required"] = "boom!"
class ArtistSchema(Schema):
name = fields.Str(required=True)
print(ArtistSchema().validate({})) # {'name': ['boom!']}
@lafrech The docs appear correct. Am I missing something?
This part:
https://marshmallow.readthedocs.io/en/latest/extending.html#custom-error-messages
You can customize the error messages that dump and dumps uses when raising a ValidationError. You do this by overriding the error_messages class variable:
Should read load/loads.
Ah thanks. Fixed