これは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');
});