1

問題があります。

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

上記のオブジェクトで、Type を検証する必要があります。しかし、パラメータTypeに依存していShippingSourceTypeますShippingDestinationType

ShippingSourceTypeが System の場合は であるType必要がありますExport。が System の場合ShippingDestinationType、 Type は である必要があります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') })

しかし、うまくいきませんでした。これを修正する方法を教えてください。

4

1 に答える 1

0

これは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');
});
于 2016-01-18T19:34:25.563 に答える