簡體   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