我正在为REST Web服务构建JSON.然而,架构已经增长到超过1300行,我想将其拆分为多个文件.我使用json-schema-validator版本2.1.8 对JSON样本响应的文件夹进行单元测试.
我知道您在当前文件中定义了导入类型{ "$ref": "#/definitions/MyBool" },但是如果我想将定义移动MyBool到另一个文件,比如Common.schema.json,我将如何引用它?
我想{ "$ref": "Common.schema.json/definitions/MyBoolean" },{ "$ref": "./Common.schema.json/defintion/MyBoolean" }和{ "$ref": "file://./Common.schema.json/definitions/MyBoolean" }但他们没有工作.
关于" common-js utils中的JSON Schema验证是否支持引用? " 的答案似乎应该可行,但我似乎无法正确使用语法.
我正在加载架构:
JsonNode mySchema = JsonLoader.fromReader( new InputStreamReader( JsonSchemaTest.class.getResourceAsStream( "/json/schema/MySchema.schema.json" ) ) );
Run Code Online (Sandbox Code Playgroud)
然后用以下方法验证它:
JsonSchemaFactory.byDefault().getValidator().validate( schema, new InputStreamReader( getClass().getResourceAsStream( "/json/sample/MyJsonSample.json" ) ) ).isSuccess();
Run Code Online (Sandbox Code Playgroud)
FWIW MyBool看起来像:
"MyBool": {
"type": "object",
"properties": {
"value" :{
"type": "string",
"enum": [ "true", "false", "file not found" ] …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的json架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Operation",
"description": "The schema of an operation",
"type": "object",
"properties": {
"id":{
"description": "Unique identifier of the service",
"type": "string"
},
"description":{
"type": "string"
},
"dateDebut":{
"type": "string",
"format": "date-time"
},
"dateFin":{
"type": "string",
"format": "date-time"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在我的架构中说dateFin必须大于dateDebut?
看起来两者都适用于我的输入验证代码.那究竟是什么区别?
具有oneof的架构
[{
"id": "MyAction",
"oneOf": [{ "$ref": "A1" },
{ "$ref": "A2" }]
},
{
"id": "A1",
"properties": {
"class1": { "type": "string"},
"class2": { "type": "string"}
}
},
{
"id": "A2",
"properties": {
"class2": { "type": "string"},
"class3": { "type": "string"}
}
}
]
Run Code Online (Sandbox Code Playgroud)
架构与任何
[{
"id": "MyAction",
"anyOf": [{ "$ref": "A1" },
{ "$ref": "A2" }]
},
{
"id": "A1",
"properties": {
"class1": { "type": "string"},
"class2": { "type": "string"}
}
},
{
"id": "A2",
"properties": { …Run Code Online (Sandbox Code Playgroud) 如何验证两个字段,例如:
("quantity" == 0 && "actualQuantity" == 0) || ("quantity" > 0 && "actualQuantity" > 0)
我的架构中有两个字段-一个是称为“ name”的必需属性,另一个是名为“ nameSort”的可选属性(用于定义排序属性),我想表达
如果定义了“ nameSort”字段,则“名称”字段也应定义为相同的值。
是否可以用JSON模式表达这样的“元素间”约束?我在http://json-schema.org/latest/json-schema-validation.html上对JSON Schema的粗读显示拒绝。
所以我有一个类似的问题(请参阅:如何在json模式中执行模式引用的嵌套列表(数组)),但现在我的结构发生了一些变化,似乎无法让它进行验证.
data = {
'VIN': '1234567',
'Vehicle color': blue,
'inspections': [
{'expected': 'MVA',
'found': 0.0,
'inspection': 'Fascia',
'location': 'rear_left',
'state': None},
{'expected': 'MVA',
'found': 0.0,
'inspection': 'Fascia',
'location': 'rear_right',
'state': None},
{'expected': 'UNKNOWN',
'found': 'CPW7',
'inspection': 'liftGateHandle',
'location': 'center_bottom',
'state': True},
{'expected': 'tinted',
'found': 'tinted',
'inspection': 'rearWindowtint',
'location': 'center_top',
'state': True},
],
'model': 'racecar',
'timestamp': '2016-03-03 01:44:00.616000'
}
Run Code Online (Sandbox Code Playgroud)
我使用的是与上一个链接中列出的相同的模式:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type" : "string" …Run Code Online (Sandbox Code Playgroud) 我正在使用JSON 架构来验证文件。这有点类似于 XML XSD。
我有几个关于id字段的问题。
我对这个问题有点迷失。我知道最好的做法是使用id属性作为每个模式的唯一标识符,并且在创建具有相互引用的不同模式的复杂模式时,这非常有用。
但我不确定我们是否需要为 id 字段分配一个 URL。我也迷失了这个架构的 URL 的含义。
非常感谢您的帮助
我正在使用 JSON 模式模板来验证在线表单收到的数据。验证器的要求之一是它允许根据对其他问题给出的答案要求一些问题。
例如,如果问题是Do you want a loan?并且用户回答yes,则What is the loan to be used for?需要将问题的答案设置为 required 以便用户必须提供答案。如果答案是,no则不需要第二个问题。
我使用定义来定义我的问题,然后在下面的主要问题模式中引用它们。我通过使用 Draft-07 中提供的 if-then-else 功能读到了这一点,我可以使用它根据其他问题的答案来设置某些问题。
在这个特定的例子中,我想要发生的是,如果用户输入Home improvements (General)问题 9的答案,那么问题 257 将被设置为必需且必须回答,否则应该抛出错误。
目前,当我将此验证器输入https://www.jsonschemavalidator.net/ 时,它无法按预期工作。实际情况是,即使问题 9 的答案是“家庭装修(一般)”,问题 257 的答案也可以留空
如何更改我的架构以提供我想要的行为?
JSON 架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"question3-9": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"enum": [
"Home improvements (General)",
"Other"
]
}
}
},
"question3-257": {
"type": "object",
"properties": { …Run Code Online (Sandbox Code Playgroud) 使用 json 模式验证器验证 json 会引发加载 json 模式文件的异常:
at com.networknt.schema.JsonSchemaFactory.findMetaSchemaForSchema(JsonSchemaFactory.java:267)
at com.networknt.schema.JsonSchemaFactory.createValidationContext(JsonSchemaFactory.java:258)
at com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:252)
at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:296)
at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:304)
at com.example.demojsonschemavalidation07.JsonSchemaValidator.validate(JsonSchemaValidator.java:28)
at com.example.demojsonschemavalidation07.JsonSchemaValidatorTest.test_valid_message(JsonSchemaValidatorTest.java:19)
...
Run Code Online (Sandbox Code Playgroud)
https://github.com/nusmanov/spring-boot-json-schema-validation/
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be …Run Code Online (Sandbox Code Playgroud) 我的 Json 架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"userInfo": {
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" },
"emailAddress":{ "type": "string" }
},
"required": ["firstName", "lastName", "emailAddress"]
},
"userPassword": {
"type": "object",
"properties": {
"password": { "type": "string" },
"confirmPassword": { "type": "string" }
}
}
},
"type": "object",
"properties": {
"standaloneDeveloper": {
"$ref": "#/definitions/userInfo",
"$ref": "#/definitions/userPassword"
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据总是被#/definitions/userPassword覆盖
我使用此模式得到以下输出
{
"standaloneDeveloper": {
"password": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
"confirmPassword": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
}
}
Run Code Online (Sandbox Code Playgroud)
预期产出
{ …Run Code Online (Sandbox Code Playgroud) jsonschema ×10
json ×7
node.js ×2
java ×1
javascript ×1
schema ×1
spring-boot ×1
validation ×1
xsd ×1