繁体   English   中英

在JOI模式验证条件下如何访问数组外部的键

[英]How to access key outside of array in when condition of JOI schema validation

我有一个问题。

"Formula": {
    "Type": "Import/Export",
    "Params": {
        "ShippingSourceType": "System/Country/Group",
        ShippingDestinationType:"System/Country/Group"
    }
}

我上面的对象必须在Type上进行验证。 但是Type取决于params中的ShippingSourceTypeShippingDestinationType

如果ShippingSourceType为System,则Type应该为Export 如果ShippingDestinationType为System,则类型应为Import

我已经验证了以下类型:

Type: joi.alternatives().required()
    .when('Params.ShippingSourceType', { is: 'System', then: joi.string().valid('Export') })
    .when('Params.ShippingDestinationType', { is: 'System', then: joi.string().valid('Import') })

但这没有用。 您能否建议如何解决此问题?

原来这是Joi的错误

推送了一个提交来修复它,但它将在版本8中提供。

同时,您可以通过从特定提交安装Joi来使用它,如下所示:

npm install --save git+https://github.com/hapijs/joi.git#5b60525b861a3ab99123cd8349cbd9f6ed50e262

然后,您可以使用:

var joi = require('joi');

var schema = joi.object({
  Formula: joi.object().keys({
    Type: joi.string() // Use joi.string()
      .when('Params.ShippingSourceType', { is: 'System', then: joi.string().valid('Export')})
      .when('Params.ShippingDestinationType', { is: 'System', then: joi.string().valid('Import')}),
    Params: joi.object(), // or additional validation
  }),
});

现在一切都按预期工作:

joi.validate({
  Formula: {
    Type: "Export",
    Params: {
      ShippingSourceType: "System",
      ShippingDestinationType:"Country"
    }
  }
}, schema, function (err, value) {
  // If I understood correctly, this should be valid
  console.log(err ? 'object invalid' : 'object valid');
});

joi.validate({
  Formula: {
    Type: "Import",
    Params: {
      ShippingSourceType: "System",
      ShippingDestinationType:"Country"
    }
  }
}, schema, function (err, value) {
  // If I understood correctly, this should be invalid since
  // ShippingSourceType == "System" => Type == "Export"
  console.log(err ? 'object invalid' : 'object valid');
});

joi.validate({
  Formula: {
    Type: "Import",
    Params: {
      ShippingSourceType: "Country",
      ShippingDestinationType:"System"
    }
  }
}, schema, function (err, value) {
  // If I understood correctly, this should be valid
  console.log(err ? 'object invalid' : 'object valid');
});

joi.validate({
  Formula: {
    Type: "Export",
    Params: {
      ShippingSourceType: "Country",
      ShippingDestinationType:"System"
    }
  }
}, schema, function (err, value) {
  // If I understood correctly, this should be invalid since
  // ShippingDestinationType == "System" => Type == "Export"
  console.log(err ? 'object invalid' : 'object valid');
});

joi.validate({
  Formula: {
    Type: "Export",
    Params: {
      ShippingSourceType: "Country",
      ShippingDestinationType:"Country"
    }
  }
}, schema, function (err, value) {
  // Should be valid
  console.log(err ? 'object invalid' : 'object valid');
});

暂无
暂无

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

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