我从 Typescript 中知道了一个概念,称为“受歧视的联合”。这是你放置 2 个结构体(类等)的地方,并且类型是根据结构体的值决定的。我正在尝试通过Pydantic验证在FastAPI中实现类似的目标。我可以收到两种不同的请求负载。是其中之一还是另一个取决于变量。如果是,则应由 验证,如果是,则应由 验证。我该如何实现这一目标?找不到任何其他解决方案。accountTypecreativeRegistrationPayloadCreativebrandRegistrationPayloadBrand
问题是它要么返回
unexpected value; permitted: 'creative' (type=value_error.const; given=brand; permitted=('creative',))
或者它根本不起作用。
class RegistrationPayloadBase(BaseModel):
first_name: str
last_name: str
email: str
password: str
class RegistrationPayloadCreative(RegistrationPayloadBase):
accountType: Literal['creative']
class RegistrationPayloadBrand(RegistrationPayloadBase):
company: str
phone: str
vat: str
accountType: Literal['brand']
class A(BaseModel):
b: Union[RegistrationPayloadBrand, RegistrationPayloadCreative]
def main():
A(b={'first_name': 'sdf', 'last_name': 'sdf', 'email': 'sdf', 'password': 'sdfds', 'accountType': 'brand'})
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud) 我正在尝试ul将每秒移动大约40px到顶部.我在stackoverflow上尝试了很多解决方案,但没有任何帮助.
那是我的代码
setInterval(function() {
$("#ul_news").animate({
marginTop: -40
}, 300);
}, 1000);Run Code Online (Sandbox Code Playgroud)
#ul_news {
/*position: absolute;
top: 0;
left: 100px;
z-index: 20;*/
}
#ul_news li {
z-index: 20;
color: black;
list-style: none;
padding-bottom: 50px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<ul id="ul_news">
<li class="active">ahoj2</li>
<li class="non_active1">ahoj3</li>
<li class="non_active2">ahoj4</li>
<li class="non_active3">ahoj5</li>
</ul>Run Code Online (Sandbox Code Playgroud)
当我尝试在Jinja2模板中从wtforms写入错误时,它将返回未解码的报价。我该如何解决?
{% if registrationForm.errors %}
<script>swal("Error!", "{{ registrationForm.errors['password'] }}", "error")</script>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
误差等于
{'email': ['This field is required.'], 'username': ['This field is required.'], 'acceptTOS': ['This field is required.'], 'csrf_token': ['CSRF token missing'], 'password': ['This field is required.']}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 React-Native、React Hooks、MomentJS 和 (setTimeout/setInterval) 制作一个倒计时器。无论我尝试使用什么方法,它都会失败。问题是组件永远不会重新渲染。
我尝试遵循官方 React Hooks 文档、Medium 上的几篇文章,例如The Iceberg of React Hooks,但没有任何效果。
一种可能性是它需要 MomentJS 对象的深度克隆,但我认为这是一种低效的方法。
这是我尝试过的可重现的示例之一。
const Timer = () => {
const [time, setTime] = useState(moment.duration(30, 'seconds'))
const intervalRef = useRef()
useEffect(() => {
intervalRef.current = setTimeout(() => {
setTime(prevTime => prevTime.subtract(1, 'second'))
}, 1000)
return () => {
clearInterval(intervalRef.current)
intervalRef.current = null
}
})
return (
<View>
{time.asSeconds()}
</View>
)
Run Code Online (Sandbox Code Playgroud) javascript ×2
python ×2
css ×1
fastapi ×1
flask ×1
html ×1
jinja2 ×1
jquery ×1
pydantic ×1
react-hooks ×1
react-native ×1
reactjs ×1
validation ×1