javascript - 使用 Joi 验证包含 mustache 模板的 uri
问题描述
我正在尝试验证一个可以包含小胡子语法的 uri:
{{ key }}
所以它看起来像这样(这首先是一个无效的 URI):
http(s)://whatever.com/.../{{ key }}/...
我正在使用Joi
,似乎Joi.string().uri()
不能允许更多的字母。我也检查了Joi.alternatives()
,但这会导致我创建自己的正则表达式,它可能不如Joi
.
有任何想法吗?
解决方案
确实没有办法在不完全重写的情况下扩充内部符合 RFC 的正则表达式。
正如您所指出的,这首先是一个无效的URI。由于您似乎使用“简单”的 Mustache 持有者并且没有条件标签,因此一种解决方法是为 Joi 提供一个已经渲染的 URI(那么我们应该说第二个吗?),例如:
const Mustache = require('mustache');
const Joi = require('joi');
// assuming objectToValidate.mustacheString holds a "potentially mustached URI"
/*async */function validateMyStuff(objectToValidate, myJoiSchema) {
// to catch standard and variable-like tags: {{standard}}, {{{variable}}} or {{&variable}}
const mustacheTypesToReplace = ['name', '&'];
// render into the object to validate
objectToValidate.mustacheString = Mustache.render(
objectToValidate.mustacheString,
// build a dummy view object
Mustache.parse(objectToValidate.mustacheString).reduce((accumulator, parsedItem) => {
if (mustacheTypesToReplace.includes(parsedItem[0])) {
accumulator[parsedItem[1]] = 'x'; // dummy data that won't break the RFC
}
return accumulator;
}, {})
);
// here the Joi.string().uri() shouldn't break unless URI is wrong
return Joi.validate(objectToValidate, myJoiSchema).then((validatedObject) => {
validatedObject.mustacheString = objectToValidate.mustacheString ; // set back the mustached data
return validatedObject;
});
}
推荐阅读
- python - 在 Django 中为我的 PostgreSQL 连接设置纯文本密码是否安全?
- javascript - 如何使用 for 循环进行绑定
- python - 我的 HoloViews 散点图上未显示垂直线,因为它超出了我的散点图的 x 范围
- ubuntu - UFW 规则被忽略
- php - 导航栏图标应该在某些页面上改变颜色
- python - pyistaller - OSError:无法加载 AutoItX
- bash - 在makefile中,当我在bash函数中使用if语句时会抛出错误
- java - 使 Json 成为 Angular 对象
- java - 如何以字符串 Base64 格式存储 PDF 内容
- spyder - PyMC3:Spyder 中的 RuntimeError