将此与客户端集成在很大程度上取决于您在客户端使用的内容。我正在使用它在 Angular 2+ 和 AJV 中使用动态创建的表单来开发一个项目,它运行得非常好。
它还取决于您使用了多少 JSON Schema。例如,我希望我的表单能够使用$data 引用,以便一个输入的有效性可以取决于其他输入的值。这基本上意味着我必须验证表单中的任何更改,因为没有有效的方法来判断$data 引用的目标是什么值。
此外,如果您的模型数据有可能在与表单交互的用户之外发生变化(例如,从其他用户从服务器中提取的新数据等),那么在其验证模式和模型中更具弹性。整体。
一般来说,即使在我的更复杂的表单上,输入值多达 30-40 个,ajv 也需要不到 10 毫秒的时间来验证整个表单,包括我自己的函数,以将 ajv 的错误与我的输入进行匹配以进行显示。所以我不会担心性能受到影响。
编辑:至于异步验证器添加某种去抖动将取决于您在客户端使用什么,但不应该太难,而且AJV 的文档真的很完整。
编辑: 这是我遇到的错误的循环,以匹配它们并稍微清理它们(AJV 的大多数错误都是用户可读的,但是像模式匹配这样的一些错误需要一些帮助,而不是向用户吐出正则表达式):
errs.forEach((err) => {
// Is this a value that is being matched to another input?
if (err.dataPath === dataPath && err.keyword === 'const' && err.schema.$data) {
return messages.push('Does not match')
}
// Don't show regex to people.
else if (err.dataPath === dataPath && err.keyword === 'pattern') {
return messages.push('Not valid format')
}
// Is the keyword 'required' and the parentPath is a match and the property is matched to err.params.missingProperty
else if (err.keyword === 'required' && err.dataPath === parentPath && err.params.missingProperty === propertyName) {
return messages.push('Required')
}
// Is the dataPath a match and no other special criteria apply
else if (err.dataPath === dataPath) {
// Cap first letter
return messages.push(err.message.charAt(0).toUpperCase() + err.message.slice(1))
}
})