简体   繁体   English

如何从AJV错误中获取“标题”?

[英]How to get “title” from AJV error?

I've got a JSON Schema that looks like this: 我有一个看起来像这样的JSON模式:

{
  "required": [
    "instructions"
  ],
  "properties": {
    "instructions": {
      "title": "Instructions",
      "minItems": 3,
      "type": "array",
      "items": {
        "required": [
          "typeId",
          "teamId",
          "disciplineId"
        ],
        "properties": {
          "typeId": {
            "minimum": 1,
            "title": "Appointment Type",
            "type": "integer"
          },
          "teamId": {
            "minimum": 1,
            "title": "Team",
            "type": "integer"
          },
          "disciplineId": {
            "minimum": 1,
            "title": "Discipline",
            "type": "integer"
          },
          "prefClinicianId": {
            "title": "Pref. Clinician",
            "anyOf": [
              {
                "type": "null"
              },
              {
                "minimum": 1,
                "type": "integer"
              }
            ]
          },
          "prefTime": {
            "title": "Pref. Time",
            "anyOf": [
              {
                "type": "null"
              },
              {
                "type": "integer"
              }
            ]
          },
          "childRequired": {
            "title": "Child Req'd",
            "type": "boolean"
          }
        },
        "type": "object"
      }
    }
  },
  "type": "object"
}

As you can see, I've added title s to all the properties. 如您所见,我已经为所有属性添加了title However, the error object I get back looks like: 但是,我返回的错误对象如下所示:

[
  {
    "keyword": "minItems",
    "dataPath": ".instructions",
    "schemaPath": "#/properties/instructions/minItems",
    "params": {
      "limit": 3
    },
    "message": "should NOT have less than 3 items"
  },
  {
    "keyword": "type",
    "dataPath": ".instructions[0].typeId",
    "schemaPath": "#/properties/instructions/items/properties/typeId/type",
    "params": {
      "type": "integer"
    },
    "message": "should be integer"
  },
  {
    "keyword": "type",
    "dataPath": ".instructions[0].teamId",
    "schemaPath": "#/properties/instructions/items/properties/teamId/type",
    "params": {
      "type": "integer"
    },
    "message": "should be integer"
  },
  {
    "keyword": "type",
    "dataPath": ".instructions[0].disciplineId",
    "schemaPath": "#/properties/instructions/items/properties/disciplineId/type",
    "params": {
      "type": "integer"
    },
    "message": "should be integer"
  }
]

As you can see, the title is not in there. 如您所见, title不在其中。 How can I get the titles with the errors? 如何获得带有错误的标题?

Please note that this question is specific to AJV . 请注意,这个问题是针对AJV的

When you create your AJV object set the verbose option to true . 创建AJV对象时,将verbose选项设置为true

This will add a parentSchema property to the ajv error with the original schema. 这将为原始模式的ajv错误添加一个parentSchema属性。 It will also add a schema property that contains the specific schema attribute that caused the validation failure. 它还将添加一个schema属性,该架构属性包含导致验证失败的特定架构属性。

Here's an example: 这是一个例子:

 var ajv = new Ajv({ $data: true, verbose: true }); let schema = { title: 'object title', type: 'object', properties: { str: { title: "A string property", type: "string" } } }; let data = { str: 3 }; ajv.validate(schema, data) console.log('ERRORS: ', this.ajv.errors) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/5.3.0/ajv.bundle.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM