繁体   English   中英

用自定义类型扩展Joi以填充字符串

[英]Extend Joi with a custom type for populated strings

我想通过使用.extend(..)为基于joi.string()的类型创建一个针对populatedString的自定义Joi类型:

  • 修剪字符串
  • 如果trimmed string === ''将值更改为undefined那么经过验证的输出将根本不包含密钥
  • 覆盖.required() ,使其作用于修剪后的字符串,并使用我自己的语言创建错误。 当在我的类型上设置.required()时,这意味着它需要一个不仅包含空格或为空的字符串

到目前为止,我的尝试接近:

    const StandardJoi = require("joi");

    const Joi = StandardJoi.extend(joi => ({
      base: joi.string(),
      name: "populatedString",
      language: {
        required: "needs to be a a string containing non whitespace characters"
      },
      pre(value, state, options) {
        value = value.trim();
        return value === "" ? undefined : value;
      },
      rules: [
        {
          name: "required",
          validate(params, value, state, options) {
            if (value === undefined) {
              return this.createError(
                "populatedString.required",
                { v: value },
                state,
                options
              );
            }

            return value;
          }
        }
      ]
    }));

它工作的例子

    Joi.populatedString().validate(" x "); // $.value === 'x'
    Joi.populatedString().validate("  "); // $.value === undefined

    // $.error.details
    //
    // [ { message: '"value" needs to be a a string containing non whitespace characters',​​​​​
    // ​​​​​    path: [],​​​​​
    // ​​​​​    type: 'populatedString.required',​​​​​
    // ​​​​​    context: { v: undefined, key: undefined, label: 'value' } } ]​​​​​
    Joi.populatedString()
      .required()
      .validate("  ");

    // $.value
    //
    // { inObj1: 'o' }
    Joi.object()
      .keys({
        inObj1: Joi.populatedString()
      })
      .validate({ inObj1: " o " });

但是它不会失败(应该如此)

    // ​​​​​{ error: null, value: {}, then: [λ: then], catch: [λ: catch] }​​​​​
    Joi.object()
      .keys({
        inObj2: Joi.populatedString(),
        inObj3: Joi.populatedString().required()
      })
      .validate({ inObj2: "  " });

即使inObj3inObj3 .required()且未提供,它也不会失败。

我设法解决了:

    const BaseJoi = require("joi");

    const Joi = BaseJoi.extend(joi => ({
      base: joi.string(),
      name: "populatedString",
      language: {
        required: "needs to be a a string containing non whitespace characters"
      },
      pre(value, state, options) {
        value = value.trim();
        return value === "" ? undefined : value;
      },
      rules: [
        {
          name: "required",
          setup(params) {
            return this.options({ presence: "required" });
          },
          validate(params, value, state, options) {
            if (value === undefined) {
              return this.createError(
                "populatedString.required",
                { v: value },
                state,
                options
              );
            }

            return value;
          }
        }
      ]
    }));

解决的方法是添加setup并让它设置选项presence = required如果调用了required()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM