JSON架构验证条件



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

我遵循了这篇文章的答案:堆栈溢出JSON模式验证,但它不起作用

这是我的模式。基本上,它应该需要基于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
}
]
}

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

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

在有类似"type": ["integer", null],的东西的地方,它应该是"type": ["integer", "null"],type采用字符串数组。它不起作用,因为null不是字符串。

最新更新