javascript - 如何在 Yup 结果中跳过空字符串?
问题描述
我想跳过空字符串作为是的结果。
我使用这个架构:
const schema = yup.object().shape({
req: yup.string().required(),
notReq: yup.string().notRequired().transform(v => v || undefined)
}).required();
const result = schema.cast({ req: 'Hello', notReq: '' });
我想看看下一个结果:
{ req: 'Hello', notReq: 'Bob' } -> { req: 'Hello', notReq: 'Bob' }
{ req: 'Hello', notReq: '' } -> { req: 'Hello' }
但是第二个例子我得到了错误
TypeError: The value of notReq could not be cast to a value that satisfies the schema type: "string".
解决方案
推荐阅读
- java - 为什么在链表的节点中插入值时需要一个临时节点?
- javascript - npm 中的 ERR_SSL_WRONG_VERSION_NUMBER
- python - TypeError: __init__(): 构造函数参数与 face_recognition 调用不兼容
- sql - 如何在 sqlite 中过滤 SUM(flag) OVER (...)
- c++ - 为什么这种替换失败会产生错误?
- flutter - 根据警报对话框响应更改开关按钮颜色
- c - 在IDE中为什么不自动出现;(分号)?
- javascript - 更改文本时可以触发 contenteditable 文本吗?HTML Javascript
- javascript - Discord.js v12 - deleteMessage 抛出旧消息错误
- arrays - 计算数组矩阵的问题