我正在编写一个 gradle 插件来json-schema在我的项目中生成一个for beans。我的目标是在构建过程中生成和发布模式,这样我就不需要手动更新它。
目前,我传递project.compileJava.inputs.files给我的任务并使用它来构建一个URLClassLoader. 然后我将这些类传递给模式生成器。
为了确保正确生成 json 模式,我想使用ObjectMapper来自目标应用程序的 。不幸的是,我在该模块的深处ObjectMapper添加了 jackson Hibernate5Module.,它尝试检查我的类是否可以分配给 aHibernateProxy,并且因为HibernateProxy它加载了 Gradle 的类加载器,并且我的类加载了URLClassLoader所有内容都会爆炸。
我可以删除 Hibernate 模块,但这仍然会在其他类似情况下触发该错误,并且它使我的插件不易分发。所以我需要一种在运行时向 gradle 的类路径或默认类加载器添加新条目的方法。
作为参考,以下是相关代码片段:
目标项目的 build.gradle
jsonSchema {
classpath = project.compileJava.inputs.files
}
Run Code Online (Sandbox Code Playgroud)
公共类 WriteJsonSchema 扩展了 DefaultTask
@TaskAction
public void write() throws ClassNotFoundException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate5Module());
mapper.registerModule(new Jdk8Module());
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
Set<File> files = getClasspath().getFiles();
List<URL> …Run Code Online (Sandbox Code Playgroud) 我有一个 JSON 架构,如下所示:
{
"required": [
"instructions"
],
"properties": {
"instructions": {
"title": "Instructions",
"minItems": 3,
"type": "array",
"items": {
"required": [
"typeId",
"teamId",
"disciplineId"
],
"properties": {
"typeId": {
"minimum": 1,
"title": "Appointment Type",
"type": "integer"
},
"teamId": {
"minimum": 1,
"title": "Team",
"type": "integer"
},
"disciplineId": {
"minimum": 1,
"title": "Discipline",
"type": "integer"
},
"prefClinicianId": {
"title": "Pref. Clinician",
"anyOf": [
{
"type": "null"
},
{
"minimum": 1,
"type": "integer"
}
]
},
"prefTime": {
"title": "Pref. Time", …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 swagger 合约,但我的数据模型是使用 JSONSchemas 定义的。
\n\n创建模型后,我正在 swagger UI 中测试它们。现在我在使用“oneOf”作为参考时遇到问题。如下...
\n\n...\n "socio":{\n "type":{\n "oneOf":[\n {\n "properties":{\n "pessoaFisica":{\n //"pessoaFisica": "object",\n "$ref":"http://soa-mds/XXXXXX/apps/SOA/JSONSchemas/Corporativo/pessoaFisica.json"\n },\n "pessoaJuridica":{ \n //"pessoaJuridica": "object",\n "$ref":"http://soa-mds/dataprev.gov.br/apps/SOA/JSONSchemas/Corporativo/pessoaJuridica.json"\n },\n "estrangeiro":{\n //"estrangeiro": "object",\n "$ref":"http://soa-mds/XXXXXX/apps/SOA/JSONSchemas/Corporativo/estrangeiro.json"\n }\n }\n } \n ] \n }\n } \n },\n....\nRun Code Online (Sandbox Code Playgroud)\n\n经过研究,我没有找到\xc2\xb4t 找到将 oneOF 与 $ref 一起使用的正确方法。
\n\n谁能帮我?
\n\n\n我想知道一个属性是否可以有多个规则。所有的例子似乎都表明你不能。
所以我想要这样的东西:
"properties": {
"track": {
"type": "string",
"pattern": "(exclusive)"
},
"track": {
"type": "string",
"pattern": "(featuring)"
}
}
Run Code Online (Sandbox Code Playgroud)
我知道在这个例子中要做的显而易见的事情是拥有模式"(exclusive)|(featuring)",但我想我想知道哪条规则失败了。同样,我以后可能想要更复杂的模式,这可能无法通过|.
我正在学习 mongodb 大学的课程以学习 3.6 版中的新功能,但我无法解决我的验证器无效的原因。
这就是我创建集合的方式:
db.getSiblingDB("TSA").createCollection("claims", {
validator: {
$jsonSchema: {
bsonType: "object",
properties: {
_id: { },
airportCode: { type: "string", minLength: 3 },
airportName: { type: "string" },
airlineName: { type: "string", minLength: 5 },
claims: {
bsonType: "object",
properties: {
itemCategory: { bsonType: "array", maxItems: 3 },
amount: { type: "string", pattern: "^\$.*" }
}
}
},
required: ["airportCode", "airlineName", "claims"],
additionalProperties: false
}
}
})
Run Code Online (Sandbox Code Playgroud)
然后,我尝试插入这个对象:
db.getSiblingDB("TSA").claims.insertOne({
"airportCode": "ABE",
"airportName": "Lehigh Valley International Airport, Allentown", …Run Code Online (Sandbox Code Playgroud) 在jsonschema中,我们如何根据外部存在的字段值来制作所需的一些内部属性。
在下面的示例中,
{
"type" : "a/b/c",
"args": {
"propA1": "",
"propA2": "",
"propB1": "",
"propC1": "",
"propC2": "",
"c1": "",
"c2": "",
"c3": ""
}
}
Run Code Online (Sandbox Code Playgroud)
type可以是a或b或c。args总是需要的。
条件是,
propA1,propA2如果类型为a,则为必填项propB1如果类型为band,则为必填项,propC1如果类型为,则propC2需要c, 。c1,c2并且c3始终需要
到目前为止我所做的是,
"properties": {
"type": {"enum": ["a", "b", "c"]},
"args": {
"type": "object",
"properties": {
"propA1": {"type":"string" },
"propA2": {"type":"string" }, …Run Code Online (Sandbox Code Playgroud) 我正在使用 mule 验证 JSON 架构组件来验证传入的 json 请求。它验证类型但不验证必需的字段属性
{
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema",
"properties": {
"Employees": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"BirthDate": {
"type": "string",
"format": "date-time"
},
"EmpNum": {
"type": "number"
},
"FirstName": {
"type": "string"
},
"Gender": {
"type": "string"
},
"LastName": {
"type": "string"
},
"LicenseNumber": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": ["EmpNum", "LastName", "FirstName", "Street", "ZipCode", "BirthDate" ]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有 json 如下所示:
{ …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用Ajv。我正在尝试在 api 的帮助下添加自定义关键字ajv.addKeyword。我可以通过这样做添加关键字(借自文档):
var ajv = new Ajv({
$data: true
});
ajv.addKeyword('range', {
type: 'number',
compile: function(sch, parentSchema) {
var min = sch[0];
var max = sch[1];
return parentSchema.exclusiveRange === true ? function(data) {
return data > min && data < max;
} : function(data, dataPath, parentData, parentDataProperty) {
return data >= min && data <= max;
}
}
});
var schema = {
"properties": {
"smaller": {
"type": "number"
},
"larger": {
"type": "number",
"range": …Run Code Online (Sandbox Code Playgroud) 我试图找出为什么模式验证在 Fastify 中不起作用。我有以下代码:
const postOptions = {
schema: {
body: {
type: 'object',
properties: {
name: { type: 'string' },
parentId: { type: 'number' },
requiredKey: { foo: { type: 'string'} }
}
},
response: {
201: {
type: 'object',
properties: {
id: { type: 'number'},
name: { type: 'string'},
parentId: { type: 'number' }
}
}
}
}
}
fastify.post('/sponsor', postOptions, async (request, reply) => {
console.log(`POST /sponsor called`)
return { id: 2, name: 'Zenotis', parentId: 1 }
}) …Run Code Online (Sandbox Code Playgroud) 我在多个系统之间有一个现有的 JSON 数据源,我无法控制也无法更改。我的任务是为此提要编写一个架构。现有的 JSON部分如下所示:
"ids": [
{ "type": "payroll", "value": "011808237" },
{ "type": "geid", "value": "31826" }
]
Run Code Online (Sandbox Code Playgroud)
当我尝试为此定义 JSON 架构时,我最终得到了如下所示的架构片段:
"properties": {
"type": { <====================== PROBLEM!!!!
"type": "string",
"enum": [ "payroll", "geid" ]
},
"value": {
"type": [ "string", "null" ],
"pattern": "^[0-9]*$"
}
}
Run Code Online (Sandbox Code Playgroud)
正如您可能猜到的那样,当 JSON 验证器在标记为“PROBLEM!!!”的行上命中该“类型”时 它会感到不安并抛出一个关于类型必须是字符串或数组的错误。
jsonschema ×10
json ×3
ajv ×2
javascript ×2
classpath ×1
fastify ×1
gradle ×1
java ×1
mongodb ×1
mongodb-3.6 ×1
mule ×1
node.js ×1
swagger ×1
swagger-ui ×1