6

3 つのパラメーターがあります。latitude, longitude, zipcode

私はjoi検証が必要です

  • いずれかが存在する場合、または郵便番号が欠落している場合は、緯度と経度が必要です
  • 緯度または経度のいずれかが欠落している場合、郵便番号が必要です。

このようなもの?

Joi.object().keys({
    latitude: Joi.number().when('zipcode', { is: undefined, then: Joi.required() }),
    longitude: Joi.number().when('zipcode', { is: undefined, then: Joi.required() }),
    zipcode: Joi.number().when(['latitude', 'longitude'], { is: undefined, then: Joi.required() })
});

おそらくobject.and()を使用するよりエレガントなソリューションがあると思います

4

2 に答える 2

1

この解決策は役に立つかもしれません:

schema = Joi.object().keys({
  location: Joi.object().keys({
    lat: Joi.number(),
    long: Joi.number()
  }).and('lat', 'long'),
  timezone: Joi.alternatives()
    .when('location', {
        is: null,
        then: Joi.number().required(),
        otherwise: Joi.number()
    })
});

于 2017-01-01T06:49:49.330 に答える