The patient sign-up form cannot show "Already registered." next to the email input. The API reports that error under email, while the form posts the field as emailAddress and every other error for it arrives under emailA…
The patient sign-up form cannot show "Already registered." next to the email input. The API reports that error under email, while the form posts the field as emailAddress and every other error for it arrives under emailAddress. The difference: this check lives in a @validates_schema method.
from marshmallow import Schema, ValidationError, fields, validates_schema
class SignupSchema(Schema):
email = fields.Email(data_key="emailAddress")
@validates_schema
def check_unique(self, data, **kwargs):
raise ValidationError("Already registered.", field_name="email")
SignupSchema().load({"emailAddress": "a@b.co"})
ValidationError: {'emailAddress': ['Already registered.']}
ValidationError: {'email': ['Already registered.']}
data_key when it has one. Field errors and @validates methods already work this way.skip_on_field_errors=False), both messages belong in one list under the data key.only or exclude.data_key, a field_name that is not a field of the schema (used as given), an error without a field_name (reported under _schema), and an error raised with a dict of messages (its keys are used as given).Adapted from marshmallow-code/marshmallow (MIT).
Visible test command
pytest testsThe repository’s own tests. Run all tests includes them, and so does every submission.
Repository
schema-validator-errors-ignore-data-key
Changes under src/ are evaluated.