2017-11-21 8 views
1

Ich habe ein JSON Schema bekam die wie folgt aussieht:Wie bekomme ich "Titel" von AJV Fehler?

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

Wie Sie sehen können, ich title s zu allen Eigenschaften hinzugefügt haben. das Fehlerobjekt I sieht erhalten jedoch zurück wie:

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

Wie Sie sehen können, die title nicht da ist. Wie bekomme ich die Titel mit den Fehlern?

Bitte beachten Sie, dass diese Frage spezifisch für AJV ist.

Antwort

1

Wenn Sie Ihr AJV-Objekt erstellen, setzen Sie verbose option to true.

Dies wird eine parentSchema-Eigenschaft zu dem Ajv-Fehler mit dem ursprünglichen Schema hinzufügen. Außerdem wird eine schema-Eigenschaft hinzugefügt, die das spezifische Schemaattribut enthält, das den Validierungsfehler verursacht hat.

Hier ist ein Beispiel:

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>

Verwandte Themen