使用 AJV 编译 JSON 模式时,我无法在 JSON 模式的顶部解析这一行: "$schema": "node_modules/ajv/lib/refs/json-schema-draft-04#"
我也尝试过这条线工作,但无济于事: "$schema": " http://json-schema.org/draft-04/schema# "
(以及上述的许多其他排列。)
无论我输入什么,AJV 都会说:“错误:没有带有密钥或引用的模式...”
这个房产到底需要什么?
谢谢(顺便说一句,AJV 很棒,谢谢。)
我是 JSON 架构验证的新手,正在为配置构建自定义架构。我正在构建的架构基于 Typescript 类型。我了解如何验证简单的数据类型,如数组、对象、数字、字符串等。
但是有没有办法指定这样的类型:
type Conf = {
idle_session_timeout?: number | "none";
item:
| {
kind: "attribute";
name: string;
}
| {
kind: "relation";
name: string;
}
| {
kind: "group";
name: string;
label?: string | undefined;
entries: PresentationItem[];
};
order_by:
| string
| {
attribute: string;
direction?: "asc" | "desc" | undefined;
};
};
Run Code Online (Sandbox Code Playgroud)
我从http://json-schema.org/draft-07/schema注意到它支持 if then else 语句来根据值切换验证模式,但我不知道如何实现它们。
我正在使用Spring Boot和REST Assured来测试REST API.我正在尝试使用JSON模式验证的示例,但它会抛出此错误:
java.lang.IllegalArgumentException: Schema to use cannot be null
Run Code Online (Sandbox Code Playgroud)
根据文档,架构应位于类路径中.我的示例架构位于那里.这是我的项目结构和示例模式位置:
这是我的代码.没有架构验证它工作正常.
given().
contentType("application/json").
when().
get("http://myExample/users").
then().
assertThat().body(matchesJsonSchemaInClasspath("example_schema.json"));
Run Code Online (Sandbox Code Playgroud) 我的数据中有一个字段可以多次输入:
它可以是 type=string,其架构如下:
{"mixed_field" : {"type":"string"} }
Run Code Online (Sandbox Code Playgroud)
其他时候可能是 type=object,架构如下所示:
{"mixed_field" : {
"properties": {
"access_token": {
"type": "string"
},
"created_at": {
"type": "integer"
}
},
"type": "object"
}
}
Run Code Online (Sandbox Code Playgroud)
如何表达“mixed_field”可以是字符串类型或对象类型?我应该按如下方式使用“oneOf”关键字吗?
{
"mixed_field": {
"oneOf": [
{
"type": "string"
},
{
"properties": {
"access_token": {
"type": "string"
},
"created_at": {
"type": "integer"
}
},
"type": "object"
}
]
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的 AJV 架构:
// schema.js
module.exports = {
title: 'task',
description: 'A Task Schema',
type: 'object',
properties: {
object_city: {
title: 'City',
type:'string'
},
object_zip: {
title: 'Zip Code',
type: 'string',
maxLength: 5,
minLength: 5
}
},
required: ['object_zip', 'object_city'],
additionalProperties: false
};
Run Code Online (Sandbox Code Playgroud)
当我针对此架构运行验证测试时,缺少 object_city 的结果是:
{ keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'object_city' },
message: 'should have required property \'object_city\'' }
Run Code Online (Sandbox Code Playgroud)
但邮政编码短于 minLength 的结果是:
{ keyword: 'minLength',
dataPath: '.object_zip',
schemaPath: '#/properties/object_zip/minLength',
params: { limit: 5 }, …Run Code Online (Sandbox Code Playgroud) 您可以使用jsonSchemaLint进行测试.
我有这个JsonSchema,它将格式设置为"完整日期".所有Draft-6验证器(Json.Net)都接受模式为有效.
{
"title": "MyTestSchema",
"type": "object",
"properties": {
"MyDateValue": {
"type": "string",
"format": "full-date",
"description": "We expect yyyy-MM-dd"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它无法识别这个Json对象是错误的:
{
"MyDateValue": "2017-10-1"
}
Run Code Online (Sandbox Code Playgroud)
当我将架构从"完整日期"切换到"日期"时,它可以工作:
{
"title": "MyTestSchema",
"type": "object",
"properties": {
"MyDateValue": {
"type": "string",
"format": "date",
"description": "We expect yyyy-MM-dd"
}
}
}
Run Code Online (Sandbox Code Playgroud)
是Json规则的最佳("完整日期")正确术语吗?请参考一些文档.
我有两个参数,比如说number_1和number_2。的值number_2应大于 的值number_1。这如何在 jsonschema 中完成?
我有兴趣验证一些 JSON 逻辑来检查数组中第一个元素的某个值是否到位。如果可能的话,我想通过 JSON 模式来实现这一点。例如,我想检查第一个元素是否是“manager”:
"employees": [
{
"manager": "Band35",
"name": "Tom"
},
{
"developer": "Band25",
"name": "Kelly"
},
{
"analyst": "Band25",
"name": "Jack"
}
]
}
Run Code Online (Sandbox Code Playgroud) 我知道有一个简短的编辑器列表可以支持基于 json 模式的自动完成:VSCode、Atom(带插件),但是配置这些编辑器并将其嵌入带有插件的网页中并不那么容易(或不可能) 。另一方面,有大量基于网络的编辑器。然而,找不到真正提供基于(自定义)JSON 模式的自动完成的功能。
任何想法?
有没有办法根据给定的条件提供自定义错误消息?我正在使用https://github.com/networknt/json-schema-validator,版本 1.0.43
这是我的 JSON 架构:
{
"$id": "https://configurations/provider.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Configuration",
"type": "object",
"properties": {
"provider": {
"description": "Name of the provider.",
"enum": [
"Provider #1",
"Provider #2"
]
},
"configuration": {
"$schema": "json-schema/configurations/configuration.json"
}
},
"if": {
"properties": {
"parcsProvider": {
"const": "Provider #1"
}
}
},
"then": {
"required": [
"configuration"
]
},
"else": {
"not": {
"required": [
"configuration"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果提供程序的值为“Provider #1”,则需要配置对象,如果是“Provider #2”并且传递配置,则会发生错误。我想自定义该错误,以便响应与现在相同,但带有诸如“提供商 2 无法进行配置”之类的自定义消息。
当前错误消息/响应:
{
"timestamp": "2020-11-23T12:50:56.20658+01:00",
"message": "invalid.json.input", …Run Code Online (Sandbox Code Playgroud) jsonschema ×8
ajv ×2
java ×2
json ×2
autocomplete ×1
javascript ×1
json.net ×1
maven ×1
node.js ×1
rest-assured ×1
rfc ×1
spring-boot ×1