我在邮递员中遇到数组 json 模式验证的问题。
var schema = {
"type": "array",
"items": [{
"id": {
"type":"long"
},
"name": {
"type":"string"
},
"email": {
"type":"string"
}
}]
};
pm.test('Response schema type nodes verification', function() {
pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});
Run Code Online (Sandbox Code Playgroud)
响应主体是:
[
{
"id": 1,
"name": "test1",
"email": "a@a.com"
},
{
"id": 2,
"name": "test2",
"email": "a@a.com"
},
.
.
.
]
Run Code Online (Sandbox Code Playgroud)
我一直通过结果。我也试过删除[].
问题出在哪儿?
如何自定义无效输入的错误消息?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"username": {
"type": "string",
"pattern": "^[A-Za-z0-9-_.]+$",
"minLength": 3
},
"password": {
"type": "string",
"minLength": 8,
"pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d\\W]$"
}
},
"required": [
"username",
"password"
],
"errors": [
{
"property": "username",
"message": "min 3 characters, do not use spaces or special characters"
}
]
}
Run Code Online (Sandbox Code Playgroud)
例如,如果用户名输入不是必需的最小长度或不满足正则表达式模式,则显示一条自定义消息min 3 characters, do not use spaces or special characters
我正在研究一个 JSON 模式模式来排除字符串中的数字和特殊字符,这是我现在所拥有的:
"properties": {
"applicationName": {
"description": "TPG Application Name",
"type": "string",
"pattern": "[^0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?\\s\\n]"
},
Run Code Online (Sandbox Code Playgroud)
这没有按预期工作,例如它不允许空格。
输入 JSON:
{
"applicationName": "TestName",
}
Run Code Online (Sandbox Code Playgroud) 所以我有一个 JSON 模式,其additionalProperties规则设置为falselike。
{
"type": "object",
"properties": {
"metadata": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
}
}
},
"street_type": {
"type": "string",
"enum": [
"Street",
"Avenue",
"Boulevard"
]
}
},
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)
和一个像这样的有效负载
{
"metadata": {
"a": "aa",
"b": "bb",
"c": "cc",
"d": "dd"
}
}
Run Code Online (Sandbox Code Playgroud)
我是否应该期望我的 JSON 模式解析器/验证器通过验证,我正在使用的 JSON 模式解析器会通过com.github.fge.jsonschema.main.JsonSchema验证,但在设置为 的metadata/d模式中不存在,additionalPropertiesfalse
这是非常误导的,有人可以指导我正确的方向吗?
JSON 架构定义是否additionalProperties …
我有一个管理一些系统配置的服务,这些配置作为 .json 文件存储在文件系统中。每次启动时,该服务都会启动,读取配置并使用这些值来修改所述系统配置。
在使用这些值之前,我使用 JSON 模式验证器来确认数据是否正常。
现在我的问题是,如何实现 JSON 模式的语义版本控制并检查 json 数据是否使用正确的版本。
目前,如果架构发生变化并且数据没有变化,验证器会退出并显示错误代码,这很好。但不太好的是我收到的错误消息。
我喜欢做的是,在验证之前检查数据的版本是否与架构的版本匹配,如果不匹配,则抛出一个期望,告诉用户由于版本不匹配,数据与架构不兼容。
经过一些研究后,我在 JSON 模式中发现了以下键:
"$id": "https://my-company.org/schemas/config/0.1.0/config.schema.json"
Run Code Online (Sandbox Code Playgroud)
此 URI 可用于控制架构本身的版本,但我如何检查数据是否使用该架构的正确版本?
我正在从github查看2.2.6版本的验证器代码.我没有更改回购" https://github.com/fge/json-schema-validator.git "中的任何代码
当我针对引用第二个模式文件的json模式测试它时,我无法运行示例1(当我使用硬编码的URI时,我可以使它工作).
我简单地重新命名了"com.github.fge.jsonschema.examples.Example1.java"以使用我的团队json架构和json文件.我已经构建了项目并将我的json模式文件复制到"json-schema-validator\bin\com\github\fge\jsonschema\examples"(全部在同一文件夹中,类似于fstab示例)
顶层的一部分附上,
},
"MovingWindow": {
"description": "Is this an moving window measure?",
"type": "boolean"
}
},
"minItems": 1,
"uniqueItems": true
},
"RealTimeProfile": {
"$ref": "rtp.json#"
}
},
"required": [
"MeasureTemplateId",
"MeasureInstanceId",Run Code Online (Sandbox Code Playgroud)
但我无法获得较低级别的读取,第二个模式文件("rtp.json")被识别并正常工作.我看到以下错误:
线程"main"中的异常com.github.fge.jsonschema.core.exceptions.ProcessingException:fatal:URI"rtp.json#"不是绝对级别:"致命"uri:"rtp.json#"
我的代码片段:
File jsonFile = new File("CumulativeCountBad.json");
File jsonSchemaFile = new File("main.json");
JsonNode good = JsonLoader.fromFile(jsonFile);
JsonNode mainSchema = JsonLoader.fromFile(jsonSchemaFile);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(mainSchema);
ProcessingReport report;
report = schema.validate(good);
System.out.println("good: " + report);Run Code Online (Sandbox Code Playgroud)
我的问题似乎与下面的问题相似,但是当我将引用设置为:"$ ref":"rtp.json#"时,我似乎无法运行该东西.
https://github.com/fge/json-schema-validator/issues/94
任何帮助赞赏.PS - …
你能帮我吗,我如何验证以下 json 中列表项的“键”(例如“1”、“2”、“3”):
{
"list" : {
"1" : {
"element1" : "1",
"element2" : "2"
},
"2" : {
"element1" : "1",
"element2" : "2"
},
....
"512" : {
"element1" : "1",
"element2" : "2"
}
}
}
Run Code Online (Sandbox Code Playgroud)
请给我一些常见的例子来验证 json 中的密钥。
假设我有以下数据结构
Option A
Option B
Option B1
Option B2.1
Option B2.2
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个允许这样做的枚举结构,以便我可以根据先前选择的下拉列表的值生成一个下拉列表。
例子 :
Select option A -> No additional dropdowns
Select option B -> Generate dropdown with Option B1
Select option B1 -> Generate dropdown with Option B2.1 and Option B2.2
Run Code Online (Sandbox Code Playgroud)
当你想用 childeren 创建一个数组时,这会起作用 enums 是否有任何类似的可能
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"medicine": {
"type": "object",
"properties": {
"value": { "type": "string" },
"categories": {
"type": "array",
"items": { "$ref": "#/definitions/medicine" }
}
}
}
},
"properties": {
"person": { …Run Code Online (Sandbox Code Playgroud) 我希望能够将答案字段验证为数值。下面的代码片段是一个答案,是更大的答案词典的一部分。每个答案都遵循通用格式,因此答案字段需要为字符串类型。
"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 ×8
jsonschema ×7
validation ×2
c# ×1
dropdown ×1
github ×1
java ×1
postman ×1
regex ×1
schema ×1