reactjs - 使用 Formik 影响 Yup 中的每个字符串测试的正确方法是什么
问题描述
(这都是通过Formik完成的)
我通过从多个文件中导入来编译一个 Yup Schema:
一个典型的模式:
export const PersonalSchema = {
name: Yup
.string('Name must be a string')
.required('Name is required')
.min(3,'Name must be at least 3 characters')
.max(250,'Name must be less than 250 characters'),
altname: Yup
.string('Alternative name must be a string')
.max(250,'Alternative name must be less than 250 characters'),
}
还有一些主文件,包括两个模式,每个来自不同的文件:
const MasterSchema = Yup.object().shape({
...PersonalSchema,
...WorkSchema
})
一些架构使用.when
.
我的任务是确保没有字符串包含 html 标记。实际上,大约有 30 个字符串字段。
到目前为止,我使用过:
Yup.addMethod(Yup.string, 'noHTML', function(args) {
return this.matches(/^(?!(?:.|[\n\r])*<[a-z][a-z0-9]*\b[^>]*>)/i, args)
})
export const ExtraSchema = {
bio: Yup
.string()
.max(4000,'Must be less than 4000 characters')
.noHTML("HTML tags are not accepted"),
}
这变得很痛苦。
(是的,我知道表达并不完美)
我正在寻找一种方法来影响.string
以禁止 HTML 或扩展它的方式。后者更可取,以防有一天某个领域可能允许它。
解决方案
推荐阅读
- birt - 从父数据集中的子数据集中聚合列
- reactjs - 按日期和部门排序/过滤
- c++ - 解码 rsa 字符串时输出不正确
- javascript - 几个月之间需要完整的高线图
- android - 如何在填充列表视图之前预览列表?
- python - Django Python Dropdown 提交失败
- ios - 表格视图/滚动视图:在函数 scrollViewDidScroll 中调整 Content Inset 属性时滚动不平滑
- sed - 如何在 sed 可能多次出现的两个标记模式之间选择数据?
- python - 如何使用 Selenium 和 Python 在新选项卡中打开新链接(单击网页中的元素后生成)?
- tfs - Azure DevOps Server 2019 Update 1 RC1 - 部署到部署组中的失败目标