marshmallow-code / marshmallow-code/marshmallow

Default error messages not getting overridden

Open
#1,910 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'm trying to add a customized error message for a class field in Marshamallow's schema. The field representation looks like this:

```
phone_number = fields.String(validate=Length(max=20), error_messages={'invalid': 'Phone number must be a string shorter than 20 letters.'})
```

When loading the request data to a schema with a longer phone number like:

```
try:
request_data = EditInvestorSchema().load(request.json)
except ValidationError as error:
return get_response(400, list(error.messages.values())[0])
```

The default error message "Longer than maximum length 20." arises instead of the customized error message.

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 by reproducing the schema load using fields.String, Length(max=20), and the custom error_messages shown in the issue. Inspect how validation errors from Length are converted into ValidationError messages; done means an overlong phone number produces the customized message instead of the default.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.