Few question about using pydantic factory #616
Unanswered
idan-rahamim-lendbuzz
asked this question in
Q&A
Replies: 1 comment
-
hi! /ᐠ. ̫ .ᐟ\ฅ class Email(BaseModel):
email: EmailStr
class EmailFactory(ModelFactory[Email]): ...
def test_factory():
email = EmailFactory.build()
assert isinstance(email, Email)
assert isinstance(email.email, str) But it fails if I add the constraint to the field: class Email(BaseModel):
email: EmailStr = Field(min_length=5, max_length=50) It seems I'd probably recommend getting rid of the Field validator though. Pydantic already takes care of validating the length via email-validator As for faker, the factory already comes with it, so you could do something like this: class UserSchemaFactory(ModelFactory[UserSchema]):
username = Use(lambda: UserSchemaFactory.__faker__.user_name())
email = Use(lambda: UserSchemaFactory.__faker__.email())
first_name = Use(lambda: UserSchemaFactory.__faker__.first_name())
last_name = Use(lambda: UserSchemaFactory.__faker__.last_name()) I'm not sure if it matters much, it seemed to work both ways. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I have a few questions and would really appreciate any help.
when i create a factory from the pydantic model above I'm getting an error
polyfactory.exceptions.ParameterException: received constraints for unsupported type <class 'pydantic.networks.EmailStr'>
What should i do?
If i wanted to use Faker to generate data would this be a correct use?

Thanks !
Beta Was this translation helpful? Give feedback.
All reactions