我正在使用Spring Boot和json-schema-validator.我正在尝试读取jsonschema.json从该resources文件夹调用的文件.我尝试了几种不同的方法,但我无法让它发挥作用.这是我的代码.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("jsonschema.json").getFile());
JsonNode mySchema = JsonLoader.fromFile(file);
Run Code Online (Sandbox Code Playgroud)
这是文件的位置.
在这里,我可以在文件classes夹中看到该文件.
但是当我运行代码时,我得到以下错误.
jsonSchemaValidator error: java.io.FileNotFoundException: /home/user/Dev/Java/Java%20Programs/SystemRoutines/target/classes/jsonschema.json (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
我在代码中做错了什么?
我已经看到了另一个问题,但它并不完全相同,我觉得我的问题更简单,但只是不起作用.
我的数据看起来像这样:
[
{ "loc": "a value 1", "toll" : null, "message" : "message is sometimes null"},
{ "loc": "a value 2", "toll" : "toll is sometimes null", "message" : null}
]
Run Code Online (Sandbox Code Playgroud)
我想在Node.js项目中使用AJV进行JSON验证,我尝试了几个模式来尝试描述我的数据,但我总是将其作为错误:
[ { keyword: 'type',
dataPath: '',
schemaPath: '#/type',
params: { type: 'array' },
message: 'should be array' } ]
Run Code Online (Sandbox Code Playgroud)
我试过的架构看起来像这样:
{
"type": "array",
"items": {
"type": "object",
"properties": {
"loc": {
"type": "string"
},
"toll": {
"type": "string"
},
"message": {
"type": "string" …Run Code Online (Sandbox Code Playgroud) 我正在python中开发一个小型RESTful Web应用程序,并使用JSON作为接口.应用程序接受JSON数据,需要根据模式验证它们.根据请求,模式可能非常严格或灵活.此外,应用程序可能需要在稍后的改进中验证用户对象.
我在堆栈溢出以及其他网站上找到了一些建议.那些包括
但找不到合适的比较.
在简单性,灵活性和扩展能力方面,您认为我应该选择哪一个?
Cerberus似乎非常灵活,扩展能力从他们的文档和示例中看起来非常好.但我不知道其他图书馆.
编辑1:目前,应用程序的需求非常简单,基本的模式验证以及添加自定义类型和自定义验证规则的范围就足够了.因此,如果所有这些库都提供了基本要求,我会选择最简单的库.如果有的话,我也愿意接受其他建议.
希望能找到一些帮助.
我想生成JSON模式,其中"additionalProperties" : false将应用于我拥有的所有类.
假设我有以下课程:
class A{
private String s;
private B b;
public String getS() {
return s;
}
public B getB() {
return b;
}
}
class B{
private BigDecimal bd;
public BigDecimal getBd() {
return bd;
}
}
Run Code Online (Sandbox Code Playgroud)
当我按照下面的代码生成模式时,schema属性"additionalProperties" : false仅适用于该类A.
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
schema.rejectAdditionalProperties();
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
Run Code Online (Sandbox Code Playgroud)
如何生成"additionalProperties" : false将应用于所有类的架构?
例子 schema
{
"type" : "object",
"id" : "urn:jsonschema:com.xxx.xxx:A",
"additionalProperties" …
面对架构验证的问题.
架构:
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "#",
"required": true,
"patternProperties": {
"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$": {
"type": "object",
"required": true,
"properties": {
"_from": {
"id": "_from",
"type": "string",
"required": true
},
"message": {
"type": "object",
"id": "message",
"properties": {
"detail": {
"type": "string",
"id": "detail",
"required": true
},
"from": {
"type": "string",
"id": "from",
"required": true
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
杰森:
{
"tom@example.com": {
"_from": "giles@gmail.com",
"message": {
"from": "Giles@gmail.com",
"detail": "AnyonewanttomeetmeinParis"
}
},
"harry@example.com": {
"_from": "giles@gmail.com", …Run Code Online (Sandbox Code Playgroud) 我有一个json:
{
"itemType": {"food":22,"electrical":2},
"itemCount":{"NA":211}
}
Run Code Online (Sandbox Code Playgroud)
这里的itemType和itemCount将是常见的,但不是它们内部的值(食物,NA,电子),它们将不断变化,但将采用以下格式:地图
如何为这种通用结构定义Json Schema?
我试过了 :
"itemCount":{
"type": "object"
"additionalProperties": {"string", "integer"}
}
Run Code Online (Sandbox Code Playgroud) 我有3个架构:
子模式:
{
"title": "child_schema",
"type": "object",
"properties": {
"wyx":{
"type": "number"
}
},
"additionalProperties": false,
"required": ["wyx"]
}
Run Code Online (Sandbox Code Playgroud)
父模式:
{
"title": "parent",
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
},
"child": {
"$ref": "file:child.json"
}
}
}
Run Code Online (Sandbox Code Playgroud)
爷爷架构:
{
"type": "object",
"title": "grandpa",
"properties": {
"reason": {
"$ref": "file:parent.json"
}
},
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,gradpa有一个父母的参考,父母有一个参考孩子.所有这3个文件都在同一个文件夹中.当我使用python验证器验证爷爷架构时,我将继续收到一个名为RefResolutionError的错误.
但是,如果我没有爷爷,我只使用父模式和子模式,一切正常!所以问题是我不能指向ref(2级)的ref.但是我可以有一个指向模式的引用(只有1级).
我想知道为什么
我正在使用Spring Boot和[jackson-module-jsonSchema](https://github.com/FasterXML/jackson-module-jsonSchema)构建一个REST API,用于生成JSON模式.我正在寻找最好的方法来验证请求JSON有效负载到达我的API端点(Spring控制器)与针对公开资源定义的已定义JSON模式,验证包括检查必需字段,格式,最小值和最大值等.一切我们可以针对架构进行验证.
看起来jackson json模式模块对于模式生成很有用,但不适用于验证,对吗?关于如何实现我想要做的事情的任何建议?
使用自定义属性扩展JSON模式是否有效?
我问的原因是,因为我使用模式也为模式描述的JSON呈现一个表单(模式中描述的每个属性都用作带有标签和某种输入的表单元素).
能够使用我主要用于表单呈现的一些属性扩展模式会很有用,但是在使用模式验证JSON对象本身时会忽略这些属性.
我可以为JSON对象提供两种不同的表示形式(一种是模式,一种是具有自定义属性的对象的模式,我只是用于创建表单,但如果我可以将两者结合在一起,则更容易进行维护).
不幸的是,Google并没有太大的帮助,而且我没有大量使用JSON模式的经验,所以如果我遗漏了一些明显的东西,我会道歉.
编辑1:
示例架构代码段:
{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"CUSTOM_PROPERTY": "CUSTOM_VALUE"
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,上面只是一个片段,因此没有title,$schema等等.
我有一个定义 JSON 文档的 JSON 模式。有一个用例,我们希望弃用特定属性以支持引入新属性,并希望将旧属性标记为弃用。JSON 模式是否支持?
jsonschema ×8
json ×6
java ×3
spring-boot ×2
cerberus ×1
geojson ×1
jackson ×1
javascript ×1
python ×1
rest ×1
schema ×1
spring ×1
spring-mvc ×1
voluptuous ×1