กำหนดประเภทแผนที่โดยใช้ json-schema v4

ฉันกำลังพยายามกำหนดสคีมาอ้างอิงเพื่อใช้เป็นประเภทแผนที่ Cassandra CQL พร้อมช่องข้อความ โดยเฉพาะฉันต้องการแมป URI กับสตริง

ตอนนี้ฉันมี:

"scope": {
      "type": "object",
      "properties": {
        "uri": {
          "type": "string",
          "format": "uri"
        },
        "permission": {
          "type": "string",
          "enum": ["read_only", "read_write", "write_only"]
        }
      },
      "required": ["uri", "permission"],
      "additionalProperties": false
    }

นี่เป็นสิ่งที่ดีสำหรับข้อมูลเช่น

{"uri":"http://example.com",
  "permission": "read_only"}

แต่ฉันต้องการสคีมาสำหรับข้อมูลเช่น

{"http://example.com": "read_only"}

http://spacetelescope.github.io/understand-json-schema/reference/object.html มีวิธีแก้ปัญหา:

{
  "type": "object",
  "patternProperties": {
    "^S_": { "type": "string" },
    "^I_": { "type": "integer" }
  }
}

ปัญหาคือฉันต้องกำหนดรูปแบบบิวท์อินด้วยนิพจน์ทั่วไป การดูตัวอย่าง regex สำหรับ URI ทำให้ฉันต้องการหลีกเลี่ยงสิ่งนี้

เนื่องจากจำนวน URI ที่ฉันมีมีจำกัด การแมป enum กับ enum จึงเป็นวิธีแก้ปัญหาเช่นกัน เป็นไปได้ไหม?


person EthernetCable    schedule 12.03.2014    source แหล่งที่มา


คำตอบ (1)


หากฉันได้รับอนุญาตให้ตอบคำถามของตัวเองได้ ฉันเชื่อว่าวิธีแก้ปัญหาคือใช้คำจำกัดความ PatternProperties สำหรับคีย์ โดยมี regex ที่เฉพาะเจาะจงมาก ค่านี้สามารถเป็นประเภทใดก็ได้ที่ json-schema รองรับ รวมถึง regex อื่นด้วย ในกรณีของฉัน มันคือแจงนับ

ดังนั้นคำจำกัดความดูเหมือน-

  "patternProperties": {
    "^https:\/\/www.example.com\/auth\/\\w+$": {
      "type": "string",
      "enum": ["read_only", "read_write", "write_only"]
    }
  },
  "additionalProperties": false
person EthernetCable    schedule 07.11.2014