标签: jsonschema

我如何需要一个或另一个领域或(另外两个)中的一个但不是全部?

我在设置JSON模式时遇到问题,该模式将验证JSON是否包含:

  • 只有一个领域
  • 另一个领域
  • (仅限于其他两个领域之一)

但是当它们的倍数存在时不匹配.

在我的具体情况下,我想要一个

  • copyAll
  • fileNames
  • matchesFiles 和/或 doesntMatchFiles

验证,但我不想接受超过那个.

这是我到目前为止所得到的:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "unrelatedA" ],
    "properties": {
    "unrelatedA": {
        "type": "string"
    },
    "fileNames": {
        "type": "array"
    },
    "copyAll": {
        "type": "boolean"
    },
    "matchesFiles": {
        "type": "array"
    },
    "doesntMatchFiles": {
        "type": "array"
        }
    },
    "oneOf": [
         {"required": ["copyAll"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["fileNames"]}},
         {"required": ["fileNames"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["copyAll"]}},
         {"anyOf": [
               {"required": ["matchesFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}},
               {"required": ["doesntMatchFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}}]}
    ]
} ;
Run Code Online (Sandbox Code Playgroud)

这比我想要的更多.我希望这匹配以下所有内容:

{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": …
Run Code Online (Sandbox Code Playgroud)

validation json jsonschema

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

根据Swagger API模式验证JSON

我从一些JSON文件创建了一个API规范,我试图测试文件是否根据API规范进行验证.

有一些很好的工具可以验证JSON Schema,但我没有机会找到一个工具来验证Swagger中创建的规范(用于创建API模式的工具).我找到的唯一解决方案是在Swagger-Editor中生成客户端/服务器,这非常麻烦.

是否已有一个现有工具来验证针对Swagger Schema的JSON?

json jsonschema swagger swagger-ui swagger-editor

42
推荐指数
1
解决办法
4万
查看次数

JSON Schema中"required"与"optional"之间的区别是什么?

有时,我注意到以下JSON模式:

{
    "type": "object",   
    "properties": {
        "address": {
                   "type": "string",
                   "required": true
            }
     }

}
Run Code Online (Sandbox Code Playgroud)

VS

{
    "type": "object",   
    "properties": {
        "address": {
                   "type": "string",
                   "optional": false
            }
     }

}
Run Code Online (Sandbox Code Playgroud)

那么在上面的例子中requiredvs 之间的区别是什么optional

json jsonschema

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

从java类生成JSON模式

我有一个POJO课程

public class Stock{
 int id;
 String name;
 Date date;
}
Run Code Online (Sandbox Code Playgroud)

是否有任何注释或开发框架/ api可以将POJO转换为JSON模式,如下所示

{"id":
      {             
        "type" : "int"
      },
"name":{   
        "type" : "string"
       }
"date":{
        "type" : "Date"
      }
}
Run Code Online (Sandbox Code Playgroud)

并且我可以通过在POJO上指定一些注释或配置来扩展模式以添加诸如"必需"之类的信息:"是",每个字段的描述等,并且可以生成如下的JSON模式.

{"id":
      {             
        "type" : "int",
        "Required" : "Yes",
        "format" : "id must not be greater than 99999",
        "description" : "id of the stock"
      },
"name":{   
        "type" : "string",
        "Required" : "Yes",
        "format" : "name must not be empty and must be 15-30 characters length",
        "description" : "name of the …
Run Code Online (Sandbox Code Playgroud)

json jsonschema jackson fasterxml jackson-modules

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

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 Schema示例

我试图通过构建验证两种不同对象类型的模式来弄清楚oneOf是如何工作的.例如一个人(名字,姓氏,运动)和车辆(类型,成本).

以下是一些示例对象:

{"firstName":"John", "lastName":"Doe", "sport": "football"}

{"vehicle":"car", "price":20000}
Run Code Online (Sandbox Code Playgroud)

问题是我做错了什么,我该如何解决它.这是架构:

{
    "description": "schema validating people and vehicles", 
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "oneOf" ],
    "properties": { "oneOf": [
        {
            "firstName": {"type": "string"}, 
            "lastName": {"type": "string"}, 
            "sport": {"type": "string"}
        }, 
        {
            "vehicle": {"type": "string"}, 
            "price":{"type": "integer"} 
        }
     ]
   }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在此解析器中验证它时:

https://json-schema-validator.herokuapp.com/
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

   [ {
  "level" : "fatal",
  "message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n  \"level\" : \"error\",\n  \"schema\" : {\n    \"loadingURI\" : \"#\",\n    \"pointer\" : \"/properties/oneOf\"\n  },\n  \"domain\" …
Run Code Online (Sandbox Code Playgroud)

schema json jsonschema

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

使用json模式指定值可以是字符串或null

希望这对其他人来说并不明显,因为我发现http://json-schema.org/上的文档缺乏更精细的细节.我正在获得一个json块,其中包含一些可以为null或字符串的属性.如何在json模式中指定(由json.NET的JsonSchema.Parse方法解析)一个值可以是null类型还是类型字符串?

有什么简单的我缺少像为类型提供数组?例如;

  "member_region": { "type": [ "string", null ] } // this throws an exception
Run Code Online (Sandbox Code Playgroud)

另外,有没有人比json-schema.org有更好的json架构细节来源?我在哪里可以找到更多的例子?我不想阅读一篇大文档/规范来找到一些可以在10行示例中轻松演示的内容.

.net c# json json.net jsonschema

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

如何定义需要至少一个属性的JSON模式

我想知道我是否可以定义一个JSON模式(草案4),该模式至少需要一个对象的许多属性中的一个.我已经知道了allOf,anyOf并且oneOf只是无法弄清楚如何我想要的方式使用它们.

以下是一些示例JSON来说明:

// Test Data 1 - Should pass
{

    "email": "hello@example.com",
    "name": "John Doe"
}
// Test Data 2 - Should pass
{
    "id": 1,
    "name": "Jane Doe"
}
// Test Data 3 - Should pass
{
    "id": 1,
    "email": "hello@example.com",
    "name": "John Smith"
}
// Test Data 4 - Should fail, invalid email
{
    "id": 1,
    "email": "thisIsNotAnEmail",
    "name": "John Smith"
}

// Test Data 5 - Should fail, missing one of …
Run Code Online (Sandbox Code Playgroud)

json jsonschema

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

如何管理多个JSON模式文件?

我正在尝试使用commonjs-utils中的node.js + json-schema.js来验证我的JSON API.只需单个验证就很容易,但无法找到正确的方法来管理多个模式文件以实现相互引用.

假设我有两个模型和两个API.

// book
{
  "type": "object",
  "properties": {
      "title": { "type": "string" },
      "author": { "type": "string" }
  }
}
// author
{
  "type": "object",
  "properties": {
      "first_name": { "type": "string" },
      "last_name": { "type": "string" }
  }
}  
// authors API
{
  "type": "array",
  "items": { "$ref": "author" }
}
// books API: list of books written by same author
{
  "type": "object",
  "properties": {
    "author": { "$ref": "author" } 
    "books": { "type": "array", "items": …
Run Code Online (Sandbox Code Playgroud)

schema json jsonschema node.js

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

Json Schema验证:不允许除模式中声明的字段之外的字段

假设我有类似的架构

fname: string
lname: string
age: string
Run Code Online (Sandbox Code Playgroud)

它们都不是必需的.用户可以向我发送上述任何属性,但任何其他未声明的属性.他们可以通过我fname,lnameage或全部.但是,如果他们通过我所有和middle_name消息的其他财产应该被拒绝.

我该如何定义这样的架构?

validation json jsonschema

32
推荐指数
1
解决办法
1万
查看次数