0

I have this json schema defined :

          "type": "object",
          "properties": {
            "kg": {
              "type": "number",
              "multipleOf": 0.01,
            },
            "lbs": {
              "type": "number",
              "multipleOf": 0.01,
            }
          }

I want a dependency between Kg and Lbs, so that when a user enters weight for kg, the weight for lbs should get auto populated with value in kg * 2.20462262 and vice-versa for kg, with weight equals to lbs * 0.45359237.

For eg if user enter 5 for field kg, the lbs field should reflect 11.02 as value, and when user enters 5 for field lbs, the kg field should reflect 2.26 as value.

I know about json dependency, but not how to access it's value if the referred value is not of type enum.

Flamingo09
  • 11
  • 1

1 Answers1

1

You can't use JSON Schema to express constraints between two different data values. Your application will need to do that.

I would suggest making either kg or lbs (note the inconsistency in pluralization there?) a required property, and then your application will convert into the preferred units based on that:

{ 
  type: object,
  anyOf: [
    { required: [ kg ] },
    { required: [ lbs ] }
  ],
  properties:
    ...
}
Ether
  • 53,118
  • 13
  • 86
  • 159