2017-08-29 3 views
1

Ich muss die folgenden Array-Elemente validieren.So überprüfen Sie jede Instanz in einem Array mit einer beliebigen Definition im JSON-Schema

{ 
    contents: [{ 
     type: "text", 
     content: "This is text context" 
    }, { 
     type: "image", 
     content: "http://image.url" 
    }] 
} 

Ich muss jedes Element im Inhaltsfeld überprüfen.

Inhalt Objekte sollten jeweils type und content Eigenschaften haben. type kann "Text", "Bild" oder "Video" sein. Für Bild oder Video sollte content eine gültige URL sein.

Dafür habe ich folgendes Schema geschrieben.

{ 
    "id": "post", 
    "description": "generell schema for a post", 
    "definitions": { 
     "contents": { 
      "type": "array", 
      "minItems": 1, 
      "items": { 
       "allOf": [ 
        { "$ref": "#/definitions/text" }, 
        { "$ref": "#/definitions/image" }, 
        { "$ref": "#/definitions/video" }, 
        { "$ref": "#/definitions/mention" } 
       ] 
      } 
     }, 
     "text": { 
      "properties": { 
       "type": {"enum": ["text"]}, 
       "content": {"type": "string"} 
      }, 
      "required": [ 
       "content", 
       "type" 
      ] 
     }, 
     "image": { 
      "properties": { 
       "type": {"enum": ["image"]}, 
       "content": { 
        "type": "string", 
        "format": "url" 
       } 
      }, 
      "required": [ 
       "content", 
       "type" 
      ] 
     }, 
     "video": { 
      "properties": { 
       "type": {"enum": ["video"]}, 
       "content": { 
        "type": "string", 
        "format": "url" 
       } 
      }, 
      "required": [ 
       "content", 
       "type" 
      ] 
     } 
    } 

} 

Aber über JSON ist nicht gültig mit meinem Schema. Es sagte data.contents[0].type should be equal to one of the allowed values

Wenn ich oneOf anstelle allOf verwende, ist es gültig. Der Bildinhalt kann jedoch ohne gültige URL Zeichenfolge sein.

Was ist das korrekte Schema?

+0

und die Frage ist ....? – Pedro

Antwort

0

Für den Anfang verwenden Sie allOf, wenn Sie oneOf verwenden sollten.

Das Stammelement benötigt auch eine Eigenschaftsdefinition. Hoffentlich helfen Ihnen die unten aufgeführten Änderungen, die von Ihnen gewünschte Lösung zu finden oder in die richtige Richtung zu weisen.

{ 
    "id": "post", 
    "description": "generell schema for a post", 
    "properties": { 
     "contents": { 
      "type": "array", 
      "minItems": 1, 
      "items": { 
       "oneOf": [ 
        { "$ref": "#/definitions/text" }, 
        { "$ref": "#/definitions/image" }, 
        { "$ref": "#/definitions/video" }, 
        { "$ref": "#/definitions/mention" } 
       ] 
      } 
     } 
    } 
    "definitions": { 
     "text": { 
      "properties": { 
       "type": {"enum": ["text"]}, 
       "content": {"type": "string"} 
      }, 
      "required": [ 
       "content", 
       "type" 
      ] 
     }, 
     "image": { 
      "properties": { 
       "type": {"enum": ["image"]}, 
       "content": { 
        "type": "string", 
        "format": "url" 
       } 
      }, 
      "required": [ 
       "content", 
       "type" 
      ] 
     }, 
     "video": { 
      "properties": { 
       "type": {"enum": ["video"]}, 
       "content": { 
        "type": "string", 
        "format": "url" 
       } 
      }, 
      "required": [ 
       "content", 
       "type" 
      ] 
     } 
    } 

} 
Verwandte Themen