JSON模式验证条件

问题描述

所以我有这个模式,我正在尝试验证它,但也包括条件。我发现了一些先前提出的问题,并按照提供的答案进行了操作,但是我无法使自己的模式正确验证。

我遵循了这篇文章的答案:Stack Overflow JSON Schema Validation,但它不起作用

这是我的架构。基本上,它应该需要基于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","title": "Is Vehicle Inoperable","description": "(Used for Vehicle commodities Only)","examples": [true,false,null],"default": false,"enum": [true,null]
    },"length": {
      "$id": "#/properties/length","type": ["integer","title": "Length of Freight in Feet","description": "(set to null for Vehicle commodities)","examples": [30],"make": {
      "$id": "#/properties/make","title": "Vehicle Make","description": "","examples": ["MAZDA"]
    },"model": {
      "$id": "#/properties/model","title": "Vehicle Model","examples": ["CX-7"]
    },"pickupStopNumber": {
      "$id": "#/properties/pickupStopNumber","title": "Pickup Stop Number","description": "At which stop will this commodity be picked up (only used for multi drop orders)","default": 0
    },"quantity": {
      "$id": "#/properties/quantity","title": "Quantity of this item","description": "i.e. Amount of Pallets (used for non-Vehicle commodities)","examples": [5],"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","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","title": "VIN","description": "Vehicle Identification Number","examples": ["JM3ER29L670150717"]
    },"weight": {
      "$id": "#/properties/weight","type": ["number","title": "Weight In Pounds","description": "Weight of commodity (used for non-Vehicle commodities)","examples": [3000],"year": {
      "$id": "#/properties/year","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","isInoperable","make","model","tariff","vehicleType","vin","year"
        ]
      },"else": false
    },{
      "if": { "properties": { "commodityType": { "const": "Pallets" } } },"description","length","quantity","weight"
        ]
      },{
      "if": { "properties": { "commodityType": { "const": "Crushed Cars" } } },{
      "if": { "properties": { "commodityType": { "const": "Bulk" } } },"else": false
    }
  ]
}

解决方法

"type": ["integer",null],之类的地方,应该为"type": ["integer","null"],type采用字符串数组。由于null不是字符串,因此无效。

,

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

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