2013-07-29 10 views
13

पर सेटिंग की आवश्यकता है, मैं वस्तुओं की मेरी जेसन-स्कीमा सरणी पर required सेट करने का तरीका जानने का प्रयास कर रहा हूं। आवश्यक संपत्ति किसी वस्तु पर ठीक काम करती है, केवल सरणी नहीं।एक जेसन-स्कीमा सरणी

 "items": { 
     "type": "array", 
     "properties": { 
      "item_id": {"type" : "number"}, 
      "quantity": {"type": "number"}, 
      "price": {"type" : "decimal"}, 
      "title": {"type": "string"}, 
      "description": {"type": "string"} 
     }, 
     "required": ["item_id","quantity","price","title","description"], 
     "additionalProperties" : false 
    } 

यहाँ json सरणी मैं भेज रहा हूँ है:

यहाँ मेरी json स्कीमा का हिस्सा है। जेसन सत्यापन विफल होना चाहिए क्योंकि मैं इन वस्तुओं में विवरण नहीं दे रहा हूं।

 "items": [ 
     { 
      "item_id": 1, 
      "quantity": 3, 
      "price": 30, 
      "title": "item1 new name" 
     }, 
     { 
      "item_id": 1, 
      "quantity": 16, 
      "price": 30, 
      "title": "Test Two" 
     } 
    ] 

उत्तर

15

मुझे this validator का उपयोग करके किसी ऑब्जेक्ट के अंदर सरणी तत्वों के लिए स्कीमा के भाग को घोंसले करके नाम से काम करने के लिए मिला। स्कीमा अब दो नेस्टेड items फ़ील्ड होते हैं, लेकिन क्योंकि एक JSONSchema में एक कीवर्ड और अन्य है कि आपके JSON वास्तव में एक क्षेत्र items

JSONSchema कहा जाता है क्योंकि:

{ 
    "type":"object", 
    "properties":{ 
     "items":{ 
     "type":"array", 
     "items":{ 
      "properties":{ 
       "item_id":{ 
        "type":"number" 
       }, 
       "quantity":{ 
        "type":"number" 
       }, 
       "price":{ 
        "type":"number" 
       }, 
       "title":{ 
        "type":"string" 
       }, 
       "description":{ 
        "type":"string" 
       } 
      }, 
      "required":[ 
       "item_id", 
       "quantity", 
       "price", 
       "title", 
       "description" 
      ], 
      "additionalProperties":false 
     } 
     } 
    } 
} 

JSON:

{ 
    "items":[ 
     { 
     "item_id":1, 
     "quantity":3, 
     "price":30, 
     "title":"item1 new name" 
     }, 
     { 
     "item_id":1, 
     "quantity":16, 
     "price":30, 
     "title":"Test Two" 
     } 
    ] 
} 

[ { 
    "level" : "error", 
    "schema" : { 
    "loadingURI" : "#", 
    "pointer" : "/properties/items/items" 
    }, 
    "instance" : { 
    "pointer" : "/items/0" 
    }, 
    "domain" : "validation", 
    "keyword" : "required", 
    "message" : "missing required property(ies)", 
    "required" : [ "description", "item_id", "price", "quantity", "title" ], 
    "missing" : [ "description" ] 
}, { 
    "level" : "error", 
    "schema" : { 
    "loadingURI" : "#", 
    "pointer" : "/properties/items/items" 
    }, 
    "instance" : { 
    "pointer" : "/items/1" 
    }, 
    "domain" : "validation", 
    "keyword" : "required", 
    "message" : "missing required property(ies)", 
    "required" : [ "description", "item_id", "price", "quantity", "title" ], 
    "missing" : [ "description" ] 
} ] 
:

वर्णन फ़ील्ड लापता के बारे में दो त्रुटियों के साथ आउटपुट

उपरोक्त आउटपुट को देखने के लिए उपरोक्त को here में चिपकाएं।

5

शायद आपका सत्यापनकर्ता JSONSchema v3 का समर्थन करता है?

तरह से काम करता है required v3 और v4 के बीच बदल दिया है: (अपने उदाहरण की तरह) http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.7

  • v4 required में तार की एक सरणी है:

  • +0

    json-schema.org v4 का उपयोग कर प्रतीत होता है। – ipengineer

    4

    मुझे पता है यह एक पुराने धागा है, लेकिन चूंकि इस सवाल jsonschema.net से जुड़ा हुआ है, मैंने सोचा कि यह में जुटाए रखने लायक हो सकती है ...

    अपने मूल उदाहरण के साथ समस्या यह है कि आप

    घोषणा कर रहे है सरणी के लिए "आइटम" घोषित करने के बजाय, "ऑब्जेक्ट" प्रकार के लिए "गुण", और उसके बाद "ऑब्जेक्ट" प्रकार ("गुण" के साथ) घोषित करना जो सरणी को पॉप्युलेट करता है।

    "items": { 
        "type": "array", 
        "items": { 
         "type": "object", 
         "properties": { 
          "item_id": {"type" : "number"}, 
          "quantity": {"type": "number"}, 
          "price": {"type" : "decimal"}, 
          "title": {"type": "string"}, 
          "description": {"type": "string"} 
         }, 
         "required": ["item_id","quantity","price","title","description"], 
         "additionalProperties" : false 
        } 
    } 
    

    मैं सरणी के नाम के लिए शब्द "आइटम" का उपयोग, भ्रम से बचने के खिलाफ की सिफारिश करेंगे, लेकिन वहाँ है कि ऐसा करने से आप को रोकने के लिए कुछ भी नहीं है ...

    : यहाँ मूल स्कीमा स्निपेट की एक संशोधित संस्करण है