Module.exports将两个不同的Joi验证与Hapi.js混合在一起

时间:2017-08-15 22:24:25

标签: node.js validation hapijs joi

不确定标题是否能准确解释问题所在,所以希望这能让您更好地了解我遇到的问题。

我的路由使用的代码几乎与另一条路径完全相同,只有管理员才能访问该路由进行更新。所以路由“A”:route / update_fees有一组我想在路由“B”下使用的Joi验证:route / update_agent_fees。所以我创建了一个验证文件夹,并从路径“A”导出了验证,这是下面的文件:

"use strict";

const Joi = require("joi");

module.exports = {
    payload: {
        fees: Joi.object().keys({
            default: Joi.array().items(
                Joi.object({
                    period: Joi.number().integer().required().description("Days to consider the fee"),
                    fee: Joi.number().integer().required().description("Fee's to be charged")
                })
            ).description("Array of fees to be charge in any shipment status").min(1),
            fees_status: Joi.object().keys({
                101: Joi.array().items(
                        Joi.object({
                            period: Joi.number().integer().required().description("Days to consider the fee"),
                            fee: Joi.number().integer().required().description("Fee's to be charged")
                        })
                    ).description("Array of fees for 101 shipment status").min(1),
                102: Joi.array().items(
                    Joi.object({
                        period: Joi.number().integer().required().description("Days to consider the fee"),
                        fee: Joi.number().integer().required().description("Fee's to be charged")
                    })
                ).description("Array of fees for 102 shipment status").min(1),
            }).description("Fees customizable for 101 and 102 status").min(1)
        }).xor("default", "fees_status")
    },
    headers: Joi.object({
        "csrf-decorator": Joi.string().required(),
        "accept-language": Joi.string().required(),
        "user-agent": Joi.string().required()
    }).options({
        allowUnknown: true
    })
}

在第二个文件中,我需要路由A并添加和修改路由B工作所需的唯一更改:

"use strict";

const Joi = require("joi");
const set_agent_fee_rates_validation = require("./set_agent_fee_rates");

let admin_set_agent_fee_rates_validation = set_agent_fee_rates_validation;
admin_set_agent_fee_rates_validation.payload.agent_id = Joi.string().required();

module.exports = admin_set_agent_fee_rates_validation;

问题在于,当我进入我的招摇时,我可以看到两个api端点都需要agent_id,只有在路由B中才需要它。

任何帮助理解这里的问题将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

为什么不导出函数,而不是导出一个对象,然后为B修改它?该函数可以返回一个验证对象,一个用于A,一个用于B?

另一个选择是在修改验证对象之前对其进行深度克隆。

以下是使用函数的方法,而不是原始对象文字

const Joi = require("joi");

const make_validator = (optional) => {
  const payload = {
    fees: Joi.object().keys({
      default: Joi.array().items(
        Joi.object({
          period: Joi.number().integer().required().description("Days to consider the fee"),
          fee: Joi.number().integer().required().description("Fee's to be charged")
        })
      ).description("Array of fees to be charge in any shipment status").min(1),
      fees_status: Joi.object().keys({
        101: Joi.array().items(
          Joi.object({
            period: Joi.number().integer().required().description("Days to consider the fee"),
            fee: Joi.number().integer().required().description("Fee's to be charged")
          })
        ).description("Array of fees for 101 shipment status").min(1),
        102: Joi.array().items(
          Joi.object({
            period: Joi.number().integer().required().description("Days to consider the fee"),
            fee: Joi.number().integer().required().description("Fee's to be charged")
          })
        ).description("Array of fees for 102 shipment status").min(1),
      }).description("Fees customizable for 101 and 102 status").min(1)
    }).xor("default", "fees_status")
  };

  if (optional && optional.payload) {
    const new_payload = Object.assign({}, payload, optional.payload);
  } else {
    const new_payload = payload;
  }

  return {
    new_payload,
    headers: Joi.object({
      "csrf-decorator": Joi.string().required(),
      "accept-language": Joi.string().required(),
      "user-agent": Joi.string().required()
    }).options({
      allowUnknown: true
    })}
  }

module.exports = make_validator

然后第二个片段变为

const Joi = require("joi");
const make_validator = require("./set_agent_fee_rates");

const set_agent_fee_rates_validation = make_validator();

const admin_set_agent_fee_rates_validation = make_validator({
  payload: {
    agent_id: Joi.string().required()
  }
});

module.exports = admin_set_agent_fee_rates_validation;