首页 > 解决方案 > 使用 Joi 验证包含 mustache 模板的 uri

问题描述

我正在尝试验证一个可以包含小胡子语法的 uri:

{{ key }}

所以它看起来像这样(这首先是一个无效的 URI):

http(s)://whatever.com/.../{{ key }}/...

我正在使用Joi,似乎Joi.string().uri()不能允许更多的字母。我也检查了Joi.alternatives(),但这会导致我创建自己的正则表达式,它可能不如Joi.

有任何想法吗?

标签: javascriptnode.jsjoi

解决方案


确实没有办法在不完全重写的情况下扩充内部符合 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;
  });
}

推荐阅读