穰扩展与人口字符串自定义类型

问题描述 投票:1回答:1

我想通过使用populatedString基于.extend(..)其上创建一个类型来创建一个自定义穰类型joi.string()s:

  • 修剪字符串
  • 的值变化undefined如果trimmed string === ''所以验证输出将不包含关键在所有
  • 覆盖.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: "  " });

尽管inObj3.required()并没有提供它不会失败。

javascript hapijs joi
1个回答
0
投票

我设法解决这个问题:

    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()被调用。

© www.soinside.com 2019 - 2024. All rights reserved.