簡體   English   中英

如何從AJV錯誤中獲取“標題”?

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

我有一個看起來像這樣的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"
}

如您所見,我已經為所有屬性添加了title 但是,我返回的錯誤對象如下所示:

[
  {
    "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"
  }
]

如您所見, title不在其中。 如何獲得帶有錯誤的標題?

請注意,這個問題是針對AJV的

創建AJV對象時,將verbose選項設置為true

這將為原始模式的ajv錯誤添加一個parentSchema屬性。 它還將添加一個schema屬性,該架構屬性包含導致驗證失敗的特定架構屬性。

這是一個例子:

 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