相关疑难解决方法(0)

JSON模式 - 根据另一个字段的值指定字段

想知道这是否可以使用模式草案03.我已经在其他地方使用了依赖项,我认为可能只需要一些创造性的使用它们来使用它们来指定required某些字段的属性.

我目前最好的尝试(不起作用)应该让你知道我在追求什么.我想要一个默认值所需的值,并且当另一个字段具有特定值时可选.

{
    "description"   : "An address...",
    "type" : "object",
    "properties" : {
        "postcode": {
            "type" : "string",
            // postcode should be required by default
            "required" : true,      
            // postcode shouldn't be required if the country is new zealand 
            "dependencies" : {
                "country" : {
                    "enum" : ["NZ", "NZL", "NEW ZEALAND"]
                },
                "postcode" : {
                    "required" : false      
                }
            }
        },
        "country": {
            "type" : "string",
            "enum" : [
                // various country codes and names...
            ],
            "default" : …
Run Code Online (Sandbox Code Playgroud)

schema dependencies json jsonschema

36
推荐指数
3
解决办法
3万
查看次数

如何在JSON模式中使用定义(draft-04)

我正在使用的其余服务响应类似于以下示例,我在此处仅包含3个字段,但还有更多:

{
    "results": [
        {
            "type": "Person",
            "name": "Mr Bean",
            "dateOfBirth": "14 Dec 1981"
        },
        {
            "type": "Company",
            "name": "Pi",
            "tradingName": "Pi Engineering Limited"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想为上面的(draft-04)编写一个JSON模式文件,它将明确指定:

if type == Person then list of required properties is ["type", "name", "dateOfBirth", etc] 
OR
if type == "Company" then list of required properties is ["type", "name", "tradingName", etc]
Run Code Online (Sandbox Code Playgroud)

但是我无法找到任何文档或如何执行此操作的示例.

目前我的JSON架构如下所示:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "required": ["results" ],
    "properties": {
        "results": {
            "type": "array",
            "items": {
                "type": "object",
                "required": ["type", "name"], …
Run Code Online (Sandbox Code Playgroud)

json jsonschema

20
推荐指数
2
解决办法
2万
查看次数

标签 统计

json ×2

jsonschema ×2

dependencies ×1

schema ×1