简体   繁体   中英

How to get “title” from AJV error?

I've got a JSON Schema that looks like this:

{
  "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. 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. How can I get the titles with the errors?

Please note that this question is specific to AJV .

When you create your AJV object set the verbose option to true .

This will add a parentSchema property to the ajv error with the original schema. It will also add a schema property that contains the specific schema attribute that caused the validation failure.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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