gpt4 book ai didi

java - 请放心 json-schema-validator

转载 作者:行者123 更新时间:2023-12-01 13:01:46 26 4
gpt4 key购买 nike

我正在使用放心的 json 模式 validator ,这是我的架构:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net#",
"type": "object",
"required": false,
"properties": {
"id": {
"id": "http://jsonschema.net/id#",
"type": "object",
"required": false,
"properties": {
"keyVal": {
"id": "http://jsonschema.net/id/keyVal#",
"type": "string",
"required": false
}
}
},
"name": {
"id": "http://jsonschema.net/name#",
"type": "string",
"required": false
},
"description": {
"id": "http://jsonschema.net/description#",
"type": "string",
"required": false
},
"updatedBy": {
"id": "http://jsonschema.net/updatedBy#",
"type": "string",
"required": false
},
"dateModified": {
"id": "http://jsonschema.net/dateModified#",
"type": "integer",
"required": false
},
"arePhrasesCaseSensitive": {
"id": "http://jsonschema.net/arePhrasesCaseSensitive#",
"type": "boolean",
"required": false
},
"phrasesList": {
"id": "http://jsonschema.net/phrasesList#",
"type": "array",
"required": false,
"items": [
{
"id": "http://jsonschema.net/phrasesList/0#",
"type": "object",
"required": false,
"properties": {
"value": {
"id": "http://jsonschema.net/phrasesList/0/value#",
"type": "string",
"required": false
},
"weight": {
"id": "http://jsonschema.net/phrasesList/0/weight#",
"type": "string",
"required": false
}
}
},
{
"id": "http://jsonschema.net/phrasesList/1#",
"type": "object",
"required": false,
"properties": {
"value": {
"id": "http://jsonschema.net/phrasesList/1/value#",
"type": "string",
"required": false
},
"weight": {
"id": "http://jsonschema.net/phrasesList/1/weight#",
"type": "string",
"required": false
}
}
},
{
"id": "http://jsonschema.net/phrasesList/2#",
"type": "object",
"required": false,
"properties": {
"value": {
"id": "http://jsonschema.net/phrasesList/2/value#",
"type": "string",
"required": false
},
"weight": {
"id": "http://jsonschema.net/phrasesList/2/weight#",
"type": "string",
"required": false
}
}
}
]
}
}
}

这是我的 JSON:

{
"id": {
"keyVal": "jkl3"
},
"name": "name",
"description": "description",
"type": "TYPE",
"updatedBy": "updatedBy",
"dateModified": 1398845197348,
"arePhrasesCaseSensitive": false
}

我正在使用 juint 来使用断言进行测试:

json = SimpleFileReader.getFileAsString("my.json");

assertThat(json, matchesJsonSchemaInClasspath("products-schema.json"));

我的 json-schema-validator 版本是:2.3.1

<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.3.1</version>
</dependency>

我错过了什么?

这是完整的错误:

com.jayway.restassured.module.jsv.JsonSchemaValidationException: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema
level: "fatal"

at com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:223)
at com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:75)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:65)
at org.junit.Assert.assertThat(Assert.java:772)
at org.junit.Assert.assertThat(Assert.java:738)
at com.websense.rest.assured.RestTestBase.testGetList(RestTestBase.java:56)
at com.websense.rest.assured.TestSample.testList(TestSample.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema
level: "fatal"

at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:86)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
at com.github.fge.jsonschema.main.JsonSchema.doValidate(JsonSchema.java:75)
at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:108)
at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:124)
at com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesSafely(JsonSchemaValidator.java:217)
... 30 more

最佳答案

(此处为 json-schema-validator 部分的作者)您的架构确实无效。 "required" 关键字根本不是这样使用的。

不幸的是,您没有与架构关联的错误消息,您可以尝试粘贴它 here查看错误。

您尝试使用 required ,因为它基本上是在 v3 草案中定义的,而您的“顶级”required 无论如何都是没有意义的。您想要的是将其作为架构:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net#",
"type": "object",
"properties": {
"id": {
"id": "http://jsonschema.net/id#",
"type": "object",
"properties": {
"keyVal": {
"id": "http://jsonschema.net/id/keyVal#",
"type": "string"
}
}
},
"name": {
"id": "http://jsonschema.net/name#",
"type": "string"
},
"description": {
"id": "http://jsonschema.net/description#",
"type": "string"
},
"updatedBy": {
"id": "http://jsonschema.net/updatedBy#",
"type": "string"
},
"dateModified": {
"id": "http://jsonschema.net/dateModified#",
"type": "integer"
},
"arePhrasesCaseSensitive": {
"id": "http://jsonschema.net/arePhrasesCaseSensitive#",
"type": "boolean"
},
"phrasesList": {
"id": "http://jsonschema.net/phrasesList#",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/phrasesList/0#",
"type": "object"
"properties": {
"value": {
"id": "http://jsonschema.net/phrasesList/0/value#",
"type": "string"
},
"weight": {
"id": "http://jsonschema.net/phrasesList/0/weight#",
"type": "string"
}
}
},
{
"id": "http://jsonschema.net/phrasesList/1#",
"type": "object"
"properties": {
"value": {
"id": "http://jsonschema.net/phrasesList/1/value#",
"type": "string"
},
"weight": {
"id": "http://jsonschema.net/phrasesList/1/weight#",
"type": "string"
}
}
},
{
"id": "http://jsonschema.net/phrasesList/2#",
"type": "object"
"properties": {
"value": {
"id": "http://jsonschema.net/phrasesList/2/value#",
"type": "string"
},
"weight": {
"id": "http://jsonschema.net/phrasesList/2/weight#",
"type": "string"
}
}
}
]
}
}
}

基本上,您的架构不再有任何“必需”。请注意,默认情况下,对象成员不需要存在;仅当它们存在于数据中时才会被验证。

如果您希望成员“foo”和“bar”出现在 JSON 对象中,那么您可以这样写:

{
"type": "object",
"required": [ "foo", "bar" ],
"etc": "etc"
}

关于java - 请放心 json-schema-validator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23466364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com