polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

Bug: PydanticFactory - `ValidationError` when using `validation_alias` on a field

Open provinzkraut opened this issue 3 months ago • 1 comments

Description

Originally reported here: https://github.com/litestar-org/litestar/issues/4288

When using validation_alias, a PydanticFactory will raise a ValidationError when calling .build() on the factory

URL to code causing the issue

No response

MCVE

import pydantic
from polyfactory.factories.pydantic_factory import ModelFactory


class Test(pydantic.BaseModel):
    test_id: str = pydantic.Field(validation_alias="test_id_test")


class TestFactory(ModelFactory[Test]):
    __model__ = Test


TestFactory.build()

Steps to reproduce

Run the code

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs


Release Version

2.22.1

Platform

  • [ ] Linux
  • [ ] Mac
  • [ ] Windows
  • [ ] Other (Please specify in the description above)

provinzkraut avatar Sep 13 '25 18:09 provinzkraut

I think this is hard to support as alias may be anything. I think want to push on with v3 and support this more robustly with https://github.com/litestar-org/polyfactory/issues/717

adhtruong avatar Sep 14 '25 12:09 adhtruong