Skip to content

Add skip_on_field_errors to field validators#2987

Open
lin-hongkuan wants to merge 1 commit into
marshmallow-code:devfrom
lin-hongkuan:codex/validates-skip-field-errors
Open

Add skip_on_field_errors to field validators#2987
lin-hongkuan wants to merge 1 commit into
marshmallow-code:devfrom
lin-hongkuan:codex/validates-skip-field-errors

Conversation

@lin-hongkuan

Copy link
Copy Markdown

Fixes #2961.

Summary

  • add an opt-in skip_on_field_errors parameter to @validates, matching the existing schema-validator option while preserving the current default behavior
  • skip only validators for fields that already have field errors, so validators for other valid fields can still run
  • add regression coverage for nested fields whose post_load transformation should not be bypassed into a parent field validator when the nested field already has errors

Tests

  • uv run --with tzdata pytest tests\test_decorators.py tests\test_schema.py -q
  • uv run ruff check src\marshmallow\decorators.py src\marshmallow\schema.py tests\test_decorators.py tests\test_schema.py
  • uv run ruff format --check src\marshmallow\decorators.py src\marshmallow\schema.py tests\test_decorators.py tests\test_schema.py
  • uv run mypy --show-error-codes

@lafrech lafrech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing.

This looks good to me.

Just a nitpick about the test.

Comment thread tests/test_schema.py
@validates("inner", skip_on_field_errors=True)
def validates_inner(self, value, **kwargs):
assert isinstance(value, ChildModel)
validated_values.append(value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem useful to me.
The assert isinstance is enough to demonstrate we went there only on valid input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent value type when validating nested schema fields

2 participants