我正在与json模式的json验证作斗争,请放心。问题是,我需要使用如果在其中的其他条件,出于某种原因,我的测试总是绿色的,即使我定义了错误的条件。我使用了json 7,它应该支持“否则”,但是当我定义假条件时,我的测试应该会失败,但会有一些错误,但实际上它通过了,我可以说它根本没有检查该条件。
这是我的儿子:
{
"courses": [{
"position": "1",
"previous_course_rank": "",
"location": "Weston, FL",
"established": "",
"course_name": "Weston Hills - Tour Course",
"course_id": "056",
"holes": [{
"hole": "1"
}]
}]
}
下面是我的json模式验证,其中包含了if Here条件。正如您可以从“如果是其他条件”中看到的,我希望只使用course_id: 056建立空字符串,但在其他情况下,我希望设置为像2022年那样的年份。问题是,当我试图测试假条件,并输入如果年份模式[0-9{4}它的绿色,我不明白为什么。
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"courses"
],
"properties": {
"courses": {
"type": "array",
"items": {
"type": "object",
"required": [
"position",
"previous_course_rank",
"location",
"established",
"course_name",
"course_id",
"holes"
],
"properties": {
"position": {
"type": "string"
},
"previous_course_rank": {
"type": "string"
},
"location": {
"type": "string"
},
"course_name": {
"type": "string"
},
"course_id": {
"type": "string"
},
"holes": {
"type": "array",
"items": {
"type": "object",
"required": [
"hole"
],
"properties": {
"hole": {
"type": "string"
}
}
}
}
},
"if": {
"properties": {
"course_id": {
"const": "056"
}
}
},
"then": {
"properties": {
"established": {
"pattern": "^$"
}
}
},
"else": {
"properties": {
"established": {
"pattern": "^[0-9]{4}$"
}
}
}
}
}
}
}
下面是我检查json与json模式的方法。
ValidatableResponse response = getResponse(feedUrl);
response
.body(matchesJsonSchemaInClasspath(pathToSchema))
.assertThat();
发布于 2022-02-20 21:14:46
"pattern": ""
的意思是“匹配一切”。也许你是说"const": ""
此外,如果您想精确匹配一个四位数字,而不允许任何前导字符或尾随字符,则需要显式锚定模式:"pattern": "^[0-9]{4}$"
。
发布于 2022-02-24 13:36:41
尝试下面的模式:json模式验证器
我使用属性类型对if
条件进行验证,我将类型设置为string
,maxLength
为0,而对于else
,我使用minimum
和maximum
属性将type
设置为number
,您可以根据需要更新这些属性
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"courses"
],
"properties": {
"courses": {
"type": "array",
"items": {
"type": "object",
"required": [
"position",
"previous_course_rank",
"location",
"established",
"course_name",
"course_id",
"holes"
],
"properties": {
"position": {
"type": "string"
},
"previous_course_rank": {
"type": "string"
},
"location": {
"type": "string"
},
"course_name": {
"type": "string"
},
"course_id": {
"type": "string"
},
"holes": {
"type": "array",
"items": {
"type": "object",
"required": [
"hole"
],
"properties": {
"hole": {
"type": "string"
}
}
}
}
},
"if": {
"properties": {
"course_id": {
"const": "056"
}
}
},
"then": {
"properties": {
"established": {
"type": "string",
"maxLength": 0
}
}
},
"else": {
"properties": {
"established": {
"type": "number",
"minimum": 1900,
"maximum": 3000
}
}
}
}
}
}
}
发布于 2022-03-30 04:23:14
因此,问题的根本原因是root默认情况下使用草案-03模式,他们不能支持版本草案-06和草案-07。
如果您想升级该草案版本,这就是我们所拥有的。因此,在我的理解中,如果有其他条件,我无法放心地使用。
JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(
ValidationConfiguration.newBuilder()
.setDefaultVersion(SchemaVersion.DRAFTV3)
.freeze()).freeze();
所以我的解决方案是使用everit模式https://github.com/everit-org/json-schema
Schema schema = getSchema(schemaPath);
SoftAssertions softAssertions = new SoftAssertions();
try {
schema.validate(new JSONObject(getResponseAsString(jsonEndPointUrl)));
} catch (ValidationException e) {
collectAllAssertions(softAssertions, e);
}
softAssertions.assertAll();
也许你可以使用草案-06和草案-07与放心,随时张贴您的解决方案。
https://stackoverflow.com/questions/71199295
复制相似问题