让我热衷于 JSON Schema 的oneOf.
我想生成 XML 格式的 JSON 版本,并使用 JSON 模式验证基本方面(我知道会有一些差异)。
我有一个 XML 架构概念,您可以在其中指定某个实体的名称或 ID:
<xs:element name="Entity" type="test:EntityType" />
<xs:complexType name="EntityType">
<xs:choice>
<xs:element name="EntityID" />
<xs:element name="EntityName" />
</xs:choice>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
在相应的 JSON 模式中,我无法确定将对象放在哪里oneOf。
在JSON 模式示例中,您似乎应该将完整的模式放入 中oneOf,对吗?在一般情况下这应该是什么样子?有谁记录了 XSD 和 JSON 模式之间的异同以供参考吗?
我有三个 json 模式定义。客户、地址和联系方式。
客户端.json
{
"$id": "client.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"$id": "/properties/name",
"type": "string"
},
"id": {
"$id": "/properties/id",
"type": "integer"
},
"contact": {
"$ref": "contact.json"
},
"address": {
"$ref": "address.json"
}
}
}
Run Code Online (Sandbox Code Playgroud)
地址.json
{
"$id": "address.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"$id": "/items",
"type": "object",
"properties": {
"addressId": {
"$id": "/items/properties/addressId",
"type": "integer"
},
"addressName": {
"$id": "/items/properties/addressName",
"type": "string"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
联系人.json
{
"$id": "contact.json", …Run Code Online (Sandbox Code Playgroud) 我有 JSON:
{"price" : 12}
Run Code Online (Sandbox Code Playgroud)
和架构:
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"}
},
}
Run Code Online (Sandbox Code Playgroud)
它的作用是验证 value 的类型validate({"price" : 12}, schema)。然而 JSON 喜欢:
{"price_blabla" : 'blabla'}
Run Code Online (Sandbox Code Playgroud)
也被认为是有效的。我应该如何更改架构以便它检查 JSON 是否包含特定键?基本上我有很多 JSON,我需要获取所有具有特定模式的 JSON。
我希望能够将答案字段验证为数值。下面的代码片段是一个答案,是更大的答案词典的一部分。每个答案都遵循通用格式,因此答案字段需要为字符串类型。
"1": {
"answer": "80035",
"web_validated": true,
"web_error_string": "",
"server_error_string": ""
},
Run Code Online (Sandbox Code Playgroud)
当我们使用 JSON Schema 来验证答案字典时,这会产生一个问题。我们需要将答案字段验证为数值,这是由字典必须遵守的 JSON 模板决定的。以下是字典中一个问题的上述答案的模板片段。
{
"id": "1",
"text": "KFI Number (Null required check)",
"type": "text",
"source": "phoebus",
"kfid_mapping": "KFID000",
"kfid_mapping_value": "",
"valid_answers": null,
"display_online": "readonly",
"required": "1",
"display_internal": "yes",
"hints": null,
"logic": null,
"rules": null,
"reason": null,
"conditional_explanation": null,
"conditional_question_id": null,
"conditional_question_answered": null,
"enabled": "1",
"order": "2",
"fk_section_id": "1",
"validated": false
}
Run Code Online (Sandbox Code Playgroud)
我们当前用于验证问题 ID 的 JSON 架构:1。
"definitions": {
"question1-1": {
"type": "object",
"properties": {
"answer": …Run Code Online (Sandbox Code Playgroud) 我有一个 JSON:
{
"i0": {
"j0": {
"a0": true
}
},
"i1": {
"j1": {
"a1": "stuff"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要一个验证:如果a0是真的,a1应该是必需的。
我的架构目前是:
{
"$schema": "http://json-schema.org/draft-07/schema",
"id": "id",
"type": "object",
"required": [
"i0",
"i1"
],
"allOf": [
{
"if": {
"properties": {
"i0": {
"j0": {
"a0": true
}
}
}
},
"then": {
"properties": {
"i1": {
"j1": {
"required": [
"a1"
]
}
}
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
该条件似乎实际上并未运行。required或者,如果我尝试将其置于与我正在检查的值相同的水平上,我已经看到了非常相似的条件。如:
"allOf": [
{ …Run Code Online (Sandbox Code Playgroud) 我正在努力创建一个复杂的 JSON 模式,并且在验证“oneOf”构造时遇到问题。
我使用“oneOf”创建了一个非常简单的模式和一个简单的 JSON 文件来演示该问题。
JSON 架构:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"oneOf":[
{"properties": {"test1": {"type": "string"}}},
{"properties": {"test2": {"type": "number"}}}
]
}
Run Code Online (Sandbox Code Playgroud)
JSON 文件:
{
"test2":4
}
Run Code Online (Sandbox Code Playgroud)
当我使用 jsonschema.validate 验证 JSON 文件与架构时,我希望这是有效的。但是我得到的错误响应是:
Traceback (most recent call last):
File "TestValidate.py", line 11, in <module>
jsonschema.validate(instance=file, schema=schema, resolver=resolver)
File "C:\Python36\lib\site-packages\jsonschema\validators.py", line 899, in validate
raise error
jsonschema.exceptions.ValidationError: {'test2': 4} is valid under each of {'properties': {'test2': {'type': 'number'}}}, {'properties': {'test1': {'type': 'string'}}}
Failed validating 'oneOf' in schema:
{'$schema': 'http://json-schema.org/draft-07/schema#',
'oneOf': …Run Code Online (Sandbox Code Playgroud) 我们有这些依赖关系:
libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % kafkaVersion
libraryDependencies += "io.confluent" % "kafka-streams-avro-serde" % confluentVersion
libraryDependencies += "io.confluent" % "kafka-schema-registry-client" % confluentVersion
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
libraryDependencies += "com.typesafe" % "config" % "1.4.0"
libraryDependencies += "com.sksamuel.avro4s" %% "avro4s-core" % "3.0.4"
Run Code Online (Sandbox Code Playgroud)
我们使用代码生成器从 AVRO 模式文件生成 Scala 案例类。一个这样生成的案例类具有 Either 值作为其字段之一。在 AVRO 模式中,这是用 type=[t1,t2] 表示的,因此生成看起来不错,这是一个总和类型:可以是类型 t1 或类型 t2。
问题变成从主题到案例类(二进制 -> Avro Map -> 案例类)的反序列化路径中缺少什么。
基本上我目前收到此错误:
could not find implicit value for parameter consumed: org.apache.kafka.streams.scala.kstream.Consumed[String, custom.UserEvent]
[error] .stream[String, UserEvent]("schma.avsc")
Run Code Online (Sandbox Code Playgroud)
第一个想法是 kafka-streams-avro-serde,但可能这个库只确保 AVRO …
我在我的 node.js 项目中使用AJV包。
我正在尝试根据几个架构文件验证一些数据。这两个架构文件都位于同一目录中:
/dir
|
parent_schema.json
|
sub_schema.json
/data
|
data.json
Run Code Online (Sandbox Code Playgroud)
我试图获得一个超级简单的$ref属性工作示例,但我遇到了麻烦。parent_schema.json好像:
{
"properties": {
"foo": { "type": "string" },
"bar": { "$ref": "sub_schema.json" }
}
}
Run Code Online (Sandbox Code Playgroud)
看起来sub_schema.json像:
{
"properties": {
"sub1": { "type": "string" },
}
}
Run Code Online (Sandbox Code Playgroud)
data.json为了完整起见,我正在尝试验证我的内容:
{
"foo": "whatever",
"bar": {
"sub1": "sometext"
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我的$ref道路。我从 AJV 收到此错误:
MissingRefError {
message: "can't resolve reference subschema1.json from id #"
missingRef: "subschema1.json"
missingSchema: "subschema1.json"
}
Run Code Online (Sandbox Code Playgroud)
有人看到我的道路有什么问题吗?我知道您还应该使用来# …
我正在测试 NewtonSoft 的 JsonSchema 包并有以下代码
string schemaJson = File.ReadAllText("c:\\temp\\schema.txt");
JsonSchema schema = JsonSchema.Parse(schemaJson);
Run Code Online (Sandbox Code Playgroud)
当我在https://www.jsonschemavalidator.net/上测试架构时,它可以正确执行,但是当我在本地运行上述代码时,我收到ArgumentException“无法将数组转换为布尔值”。
这是架构:
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"description": "The root schema is the schema that comprises the entire JSON document.",
"default": {},
"required": [
"checked",
"dimensions",
"id",
"name",
"price",
"tags"
],
"properties": {
"checked": {
"$id": "#/properties/checked",
"type": "boolean",
"title": "The Checked Schema",
"description": "An explanation about the purpose of this instance.",
"default": false,
"examples": [
false
] …Run Code Online (Sandbox Code Playgroud) 我使用 jsonschema 来验证 JSON blob,其中特定属性可以是日期或日期时间。因此,我希望format关键字允许多种潜在格式。
例如,以下代码有效:
import rfc3339_validator
schema = {
"title": "example",
"type": "object",
"required": ["example_property"],
"properties": {
"example_property": {
"type": "string",
"format": "date-time"
}
}
}
validate(instance, schema, format_checker=jsonschema.FormatChecker())
Run Code Online (Sandbox Code Playgroud)
但理想情况下,我会使用format潜在格式列表,例如["date-time", "date"].
有办法实现这一点吗?我的临时解决方法是使用pattern关键字来确保该属性至少包含日期。
jsonschema ×10
json ×5
python ×3
ajv ×2
c# ×2
apache-kafka ×1
avro ×1
javascript ×1
json.net ×1
scala ×1
schema ×1
xml ×1
xsd ×1