JSON 模式验证条件

JSON Schema Validation Conditional

所以我有这个模式,我正在尝试验证它,但也包括条件。我发现了一些以前提出的问题并遵循了提供的答案,但是我无法正确验证我的架构。

我遵循了这个 post 的答案: 但它不起作用

这是我的架构。基本上它应该需要基于 commodityType 的某些字段,但它不起作用并且模式验证器会抛出各种错误。我不确定我做错了什么...

我还应该补充一点,我正在使用 npm jsonschema 来处理模式...

  {
  "$id": "/commodity",
  "type": "object",
  "title": "Commodity Schema",
  "description": "Element of the Commodities Array",
  "examples": [
    {
      "commodityType": "Vehicles",
      "deliveryStopNumber": 1,
      "description": "Describes Freight (not used for vehicle commodities",
      "isInoperable": true,
      "length": 30,
      "make": "MAZDA",
      "model": "CX-7",
      "quantity": 3,
      "tariff": 260.34,
      "vehicleType": "SUV",
      "vin": "JM3ER29L670150717",
      "weight": 3331,
      "year": "2007"
    }
  ],
  "properties": {
    "commodityType": {
      "$id": "#/properties/commodityType",
      "type": ["string"],
      "title": "Commodity Type",
      "description": "Enum: Vehicles, Crushed Cars, Pallets, Bulk",
      "examples": ["Vehicles"],
      "enum": ["Vehicles", "Crushed Cars", "Pallets", "Bulk"]
    },
    "deliveryStopNumber": {
      "$id": "#/properties/deliveryStopNumber",
      "type": ["integer"],
      "title": "Delivery Stop Number",
      "description": "At which stop will this commodity be dropped off (ONLY used for multi drop orders)",
      "examples": [1],
      "default": 1
    },
    "description": {
      "$id": "#/properties/description",
      "type": ["string", "null"],
      "title": "Describes the Commodity",
      "description": "Freight Description (set to null for Vehicle commodities)",
      "examples": ["Heavy Tables"],
      "default": null
    },
    "isInoperable": {
      "$id": "#/properties/isInoperable",
      "type": ["boolean", "null"],
      "title": "Is Vehicle Inoperable",
      "description": "(Used for Vehicle commodities Only)",
      "examples": [true, false, null],
      "default": false,
      "enum": [true, false, null]
    },
    "length": {
      "$id": "#/properties/length",
      "type": ["integer", "null"],
      "title": "Length of Freight in Feet",
      "description": "(set to null for Vehicle commodities)",
      "examples": [30],
      "default": null
    },
    "make": {
      "$id": "#/properties/make",
      "type": ["string", "null"],
      "title": "Vehicle Make",
      "description": "",
      "examples": ["MAZDA"]
    },
    "model": {
      "$id": "#/properties/model",
      "type": ["string", "null"],
      "title": "Vehicle Model",
      "description": "",
      "examples": ["CX-7"]
    },
    "pickupStopNumber": {
      "$id": "#/properties/pickupStopNumber",
      "type": ["integer", "null"],
      "title": "Pickup Stop Number",
      "description": "At which stop will this commodity be picked up (only used for multi drop orders)",
      "examples": [1],
      "default": 0
    },
    "quantity": {
      "$id": "#/properties/quantity",
      "type": ["integer", "null"],
      "title": "Quantity of this item",
      "description": "i.e. Amount of Pallets (used for non-Vehicle commodities)",
      "examples": [5],
      "default": 0
    },
    "tariff": {
      "$id": "#/properties/tariff",
      "type": "number",
      "title": "Tariff",
      "description": "Amount Being Payed To RCG For This Commodity",
      "default": 0,
      "examples": [260.23]
    },
    "vehicleType": {
      "$id": "#/properties/vehicleType",
      "type": ["string"],
      "title": "Vehicle Type",
      "description": "Describes Vehicle (use Picklist)",
      "examples": ["JM3ER29L670150717"],
      "enum": [
        "Sedan",
        "Coupe",
        "Convertible",
        "SUV",
        "Minivan",
        "Pickup Truck (2 Door)",
        "Pickup Truck (4 Door)",
        "Motorcycle",
        "ATV",
        "Boat",
        "RV",
        "Trailer (5th Wheel)",
        "Trailer (Bumper Pull)",
        "Trailer (Gooseneck)",
        "Cargo Van",
        "Box Truck",
        "Pickup Dually",
        "Other"
      ]
    },
    "vin": {
      "$id": "#/properties/vin",
      "type": ["string", "null"],
      "title": "VIN",
      "description": "Vehicle Identification Number",
      "examples": ["JM3ER29L670150717"]
    },
    "weight": {
      "$id": "#/properties/weight",
      "type": ["number", "null"],
      "title": "Weight In Pounds",
      "description": "Weight of commodity (used for non-Vehicle commodities)",
      "examples": [3000],
      "default": null
    },
    "year": {
      "$id": "#/properties/year",
      "type": ["string", "null"],
      "title": "The year schema",
      "description": "Vehicle Year",
      "examples": ["2007"],
      "default": null
    }
  },
  "additionalProperties": false,
  "required": [
    "commodityType",
    "deliveryStopNumber",
    "pickupStopNumber",
    "tariff"
  ],
  "anyOf": [
    {
      "if": { "properties": { "commodityType": { "const": "Vehicles" } } },
      "then": {
        "required": [
          "commodityType",
          "deliveryStopNumber",
          "isInoperable",
          "make",
          "model",
          "pickupStopNumber",
          "tariff",
          "vehicleType",
          "vin",
          "year"
        ]
      },
      "else": false
    },
    {
      "if": { "properties": { "commodityType": { "const": "Pallets" } } },
      "then": {
        "required": [
          "commodityType",
          "deliveryStopNumber",
          "description",
          "length",
          "pickupStopNumber",
          "quantity",
          "tariff",
          "weight"
        ]
      },
      "else": false
    },
    {
      "if": { "properties": { "commodityType": { "const": "Crushed Cars" } } },
      "then": {
        "required": [
          "commodityType",
          "deliveryStopNumber",
          "description",
          "length",
          "pickupStopNumber",
          "quantity",
          "tariff",
          "weight"
        ]
      },
      "else": false
    },
    {
      "if": { "properties": { "commodityType": { "const": "Bulk" } } },
      "then": {
        "required": [
          "commodityType",
          "deliveryStopNumber",
          "description",
          "length",
          "pickupStopNumber",
          "quantity",
          "tariff",
          "weight"
        ]
      },
      "else": false
    }
  ]
}

在你有类似 "type": ["integer", null], 的地方,它应该是 "type": ["integer", "null"],type 接受一个字符串数组。它不起作用,因为 null 不是字符串。

您可以将 anyOf 更改为 allOf,并删除所有 else: false 子句,这至少会使错误(发出时)更有帮助。

此外,if/then/else 直到第 7 版草案才引入,因此如果您使用的评估器不支持该版本,这些关键字将被完全忽略。