【问题标题】:Validate an uri which contains mustache template with Joi使用 Joi 验证包含 mustache 模板的 uri
【发布时间】:2019-05-17 02:22:59
【问题描述】:
我正在尝试验证一个可以包含 mustache 语法的 uri:
{{ key }}
所以它看起来像这样(这首先是一个无效的 URI):
http(s)://whatever.com/.../{{ key }}/...
我正在使用Joi,似乎Joi.string().uri() 不能允许更多字母。
我还检查了Joi.alternatives(),但这会导致我创建自己的正则表达式,它可能不如Joi 中的安全。
有什么想法吗?
【问题讨论】:
标签:
javascript
node.js
joi
【解决方案1】:
确实没有办法在不完全重写的情况下扩充内部符合 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;
});
}