Pydantic模式可选
from Typing import Optional
class PostCreateSchema(BaseModel):
contact_phone:Optional[constr(strip_whitespace=True, min_length=8,max_length=10)]
我指定了可选的变量,但是当我提交表单而不填写contact_phone字段时,我仍会遇到错误“ contact_phone:确保此值至少具有8个字符”,
如果我删除'最小长度'约束,它将工作,它将工作按照预期的方式,我可以用一个空字段提交。
我该如何使该字段可选,但是如果填充却有最低值?
from Typing import Optional
class PostCreateSchema(BaseModel):
contact_phone:Optional[constr(strip_whitespace=True, min_length=8,max_length=10)]
I specified the variable to be Optional but when I submitted the form without filling in the contact_phone field I am still getting the Error "contact_phone: ensure this value has at least 8 characters"
If I remove 'min-length' constraint, it will work as intended and I can submit with an empty field.
How can I make the field optional but yet have a minimum value if filled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要指定一个默认值:
You just need to specify a default value: