2014-03-12 20 views
2

我试图定义一个引用的模式以用作具有文本字段的Cassandra CQL地图类型。具体来说,我想将URI映射到字符串。使用json-schema v4定义地图类型

现在我有:

"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/understanding-json-schema/reference/object.html数据的模式有一个解决方案:

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

这个问题是,我不得不定义一个正则表达式的内置格式。看看URI的正则表达式的例子让我想避免这种情况。

由于我拥有的URI数量有限,因此将枚举映射到enum也是一个解决方案。这是可行的吗?

回答

1

如果我可以被允许回答我自己的问题,我相信解决方案是使用一个PatternProperties定义的密钥,一个非常具体的正则表达式。该值可以是由json-schema支持的任何类型,包括另一个正则表达式。就我而言,这是一个枚举。

所以定义看起来是喜欢 -

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