Home > Mobile >  Optional Properties json schema
Optional Properties json schema

Time:12-13

the items are only required for the order_type: ORDER, but I can't make a conditional that only requires this property for that specific case, that is, if any order_type different from ORDER comes without items it can be validated correctly, but if an ORDER comes without them I mark it incorrectly.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://json-schema.org/draft-07/schema#",
  "title": "Validaciones sobre el esquema Order",
  "type": "object",
  "properties": {
    "warehouse_id": {
      "type": [
        "string"
      ]
    },
    "operation_type": {
      "type": [
        "string"
      ]
    },
    "order_id": {
      "type": [
        "number"
      ]
    },
    "items": {
      "type": [
        "array"
      ],
      "minItems": 1,
      "items": {
        "type": [
          "object"
        ],
        "properties": {
          "sku": {
            "type": [
              "string"
            ],
            "minLength": 1,
            "maxLength": 50
          },
          "quantity": {
            "type": [
              "integer"
            ],
            "minimum": 1
          }
        },
        "required": [
          "sku",
          "quantity"
        ],
        "additionalProperties": false
      }
    },
    "attributes": {
      "type": [
        "object"
      ],
      "properties": {
        "order_type": {
          "type": [
            "string"
          ],
          "enum": [
            "ORDER",
            "TRANSFER",
            "WITHDRAWAL",
            "DISPOSAL",
            "FRESH"
          ]
        },
        "etd": {
          "type": [
            "string"
          ],
          "pattern": "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(?:T)(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?:Z)$"
        },
        "volume": {
          "type": [
            "integer", "null"
          ],
          "minimum": 0
        },
        "typology": true
      },
      "required": [
        "order_type"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "ORDER"
              }
            },
            "required": [
              "order_type",
              "items"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true,
              "typology": {
                "type": [
                  "string", "null"
                ],
                "enum": [
                  "CONVEYABLE",
                  "NON_CONVEYABLE"
                ]
              }
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "TRANSFER"
              }
            },
            "required": [
              "order_type"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "WITHDRAWAL"
              }
            },
            "required": [
              "order_type"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "DISPOSAL"
              }
            },
            "required": [
              "order_type"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        }
      ]
    }
  },
  "required": [
    "warehouse_id",
    "operation_type",
    "attributes"
  ],
  "additionalProperties": false
}

try to make a conditional with an if at the end of the properties like this

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://json-schema.org/draft-07/schema#",
  "title": "Validaciones sobre el esquema Order",
  "type": "object",
  "properties": {
    "warehouse_id": {
      "type": [
        "string"
      ]
    },
    "operation_type": {
      "type": [
        "string"
      ]
    },
    "order_id": {
      "type": [
        "number"
      ]
    },
    "items": {
      "type": [
        "array"
      ],
      "minItems": 1,
      "items": {
        "type": [
          "object"
        ],
        "properties": {
          "sku": {
            "type": [
              "string"
            ],
            "minLength": 1,
            "maxLength": 50
          },
          "quantity": {
            "type": [
              "integer"
            ],
            "minimum": 1
          }
        },
        "required": [
          "sku",
          "quantity"
        ],
        "additionalProperties": false
      }
    },
    "attributes": {
      "type": [
        "object"
      ],
      "properties": {
        "order_type": {
          "type": [
            "string"
          ],
          "enum": [
            "ORDER",
            "TRANSFER",
            "WITHDRAWAL",
            "DISPOSAL",
            "FRESH"
          ]
        },
        "etd": {
          "type": [
            "string"
          ],
          "pattern": "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(?:T)(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?:Z)$"
        },
        "volume": {
          "type": [
            "integer", "null"
          ],
          "minimum": 0
        },
        "typology": true
      },
      "required": [
        "order_type"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "ORDER"
              }
            },
            "required": [
              "order_type",
              "items"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true,
              "typology": {
                "type": [
                  "string", "null"
                ],
                "enum": [
                  "CONVEYABLE",
                  "NON_CONVEYABLE"
                ]
              }
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "TRANSFER"
              }
            },
            "required": [
              "order_type"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "WITHDRAWAL"
              }
            },
            "required": [
              "order_type"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        },
        {
          "if": {
            "properties": {
              "order_type": {
                "const": "DISPOSAL"
              }
            },
            "required": [
              "order_type"
            ]
          },
          "then": {
            "properties": {
              "order_type": true,
              "etd": true,
              "volume": true
            },
            "required": [
              "order_type",
              "etd"
            ],
            "additionalProperties": false
          }
        }
      ]
    }
  },
  "allOf": [
    
    {
      "if": {
        "properties": {
          "order_type": {
            "const": "ORDER"
          }
        },
        "required": [
          "order_type"
        ]
      },
      "then": {
        "properties": {
          "items": true,
          "order_type": true,
          "etd": true,
          "volume": true
        },
        "required": [
          "order_type",
          "etd",
          "items"
        ],
        "additionalProperties": false
      }
    }
    
  ],
  "required": [
    "warehouse_id",
    "operation_type",
    "attributes"
  ],
  "additionalProperties": false
}

this request should be marked as incorrect, cause dont have any items, that are required:

{
    "warehouse_id": "ARTW01",
    "operation_type": "outbound",
    "order_id": 41789301078,
    "attributes": {
        "volume": 1350,
        "etd": "2022-11-11T18:25:00Z",
        "order_type": "ORDER"
    }
}

CodePudding user response:

Here's the assertion you described with all the unrelated parts removed. The trick is that the if need to describe the nested path to the value being checked.

{
  "$schema": "http://json-schema.org/draft-07/schema#",

  "type": "object",
  "properties": {
    "attributes": {
      "type": "object",
      "properties": {
        "order_type": { "enum": ["ORDER", "TRANSFER", "WITHDRAWAL", "DISPOSAL", "FRESH"] }
      },
      "required": ["order_type"]
    },
    "items": { "type": "array" }
  },

  "allOf": [
    {
      "if": {
        "type": "object",
        "properties": {
          "attributes": {
            "type": "object",
            "properties": {
              "order_type": { "const": "ORDER" }
            },
            "required": ["order_type"]
          }
        },
        "required": ["attributes"]
      },
      "then": { "required": ["items"] }
    }
  ]
}
  • Related