我正在开发一个主题,并希望所有十六进制颜色在它们旁边显示 VSCode 颜色选择器。
以前有人问过类似的问题,但即使在查看文档后我也不知道如何实现它。
所以我尝试在这个例子之后创建一个 JSON 模式(Github)。
我得到了以下代码,但它仅在 JSONkey字符串具有以下名称时才有效hex-color:
...
"type": "object",
"properties": {
"hex-color": {"type": "string","format": "color"}
}
...
Run Code Online (Sandbox Code Playgroud)
所以
{
"test": "#000", // Does not work
"hex-color": "#000" // Works
}
Run Code Online (Sandbox Code Playgroud)
创建 VSCode 主题时,为架构的主题文件中可能的每个属性键命名是非常困难且耗时的。有没有办法创建通配符键,或者有更好的方法来检测值是否为十六进制颜色?
我有两个单独的JSON模式(用于验证REST API的HTTP请求端点),它们都接受相同的确切对象,但具有不同的必需字段(这是创建与更新请求).有没有办法可以重用此对象的单个定义并仅更改必填字段?我知道如何使用$ref重用对象作为另一个对象的属性,但我无法弄清楚如何将整个对象重用为模式中的顶级对象.到目前为止我失败的尝试:
event.json
{
"id": "event",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"start_date": {
"type": "integer"
},
"end_date": {
"type": "integer"
},
"description": {
"type": "string"
}
},
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)
事件create.json
{
"id": "event-create",
"type": "object",
"$ref": "event",
"additionalProperties": false,
"required": [ "name", "description" ]
}
Run Code Online (Sandbox Code Playgroud)
显然这不起作用.似乎它试图将"事件"的全部内容插入到"event-create"的定义中,包括ID等.我试过推荐event#/properties无济于事.我似乎无法$ref在属性属性中作为唯一值.有任何想法吗?
如果在输入文件中指定在架构中配置为要求的元素,则验证是否正常.如果你附加"maxItems":1,它不关心你是否在输入文件中添加另一个元素,验证器仍然将其视为有效的输入文件.
即:架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Books": {
"type": "object",
"minItems": 1,
"properties": {
"Book": {
"type": "object",
"minItems": 1,
"maxItems": 1,
"properties": {
"Author": {
"type": "string",
"minItems": 1,
"maxItems": 1
}
},
"required": ["Author"]
}
},
"required": ["Book"]
}
},
"required": ["Books"]
}
Run Code Online (Sandbox Code Playgroud)
输入文件:
{
"Books": {
"Book": {
"Author": "Andreas",
"Author": "Geir"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不应该是无效的输入文件吗?
验证人:
如果我希望数组的元素(都是所有对象)都遵循相同的模式,我将在JSON模式中使用什么关键字?
例:
"data":
[
{ //validated
"id": 1,
"name": "Bob",
"ready": "Not Ready"
},
{ //validated
"id": 2,
"name": "Steve",
"ready": "Ready"
},
{ //not validated, missing "ready"
"id": 3,
"name": "Ted"
}
]
Run Code Online (Sandbox Code Playgroud) 我尝试使用 json 模式,这里有一个简单的例子。我使用该网站:http://www.jsonschemavalidator.net/
架构:
{
'Foods':
{
'type': 'array',
'items':
{
'GoodFoods': { 'type':'string' },
'NastyFoods': { 'type':'string' },
'BlendFoods': { 'type': 'string' }
},
'required': ['BlendFoods'],
}
}
Run Code Online (Sandbox Code Playgroud)
输入 JSON:
{
"Foods":
[
{
"GoodFoods": "Pasta",
"NastyFoods": true,
}
]
}
Run Code Online (Sandbox Code Playgroud)
这里的想法是,它应该抱怨缺少“BlendFoods”属性,并且 NastyFoods 是布尔值而不是字符串。但它却显示“未发现错误。JSON 根据架构进行验证”。那不是我想要的。
我对此尝试了很多,但无法弄清楚我在模式中做错了什么,有什么想法吗?
最好的问候罗布
供应商向我发送一个 json 模式。请看这个:
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"definitions" : {
...
},
"oneOf" : [{
"$ref" : "#/definitions/commons/strings/text"
}, {
"$ref" : "#/definitions/dto/scriptStep"
}, {
"$ref" : "#/definitions/dto/callResult"
}
]
}
Run Code Online (Sandbox Code Playgroud)
没有“属性”关键字(但顺便说一下,有非常大的“定义”部分)。这是否意味着架构实际上描述了空的 json 对象 {}?或者这是否意味着 json 可以包含“oneOf”数组中的元素之一?
我试图在 JSON 模式中定义以下内容:
"codenumber": {
"12345": [
{
"prop1": "yes",
"prop2": "no"
}
]
}
Run Code Online (Sandbox Code Playgroud)
codenumber 对象包含一个属性“12345”,它始终是一个包含数组的字符串编号。然而,数值可以改变,所以我不能像这样简单地定义它:
"codenumber": {
"type": "object",
"properties": {
"12345": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何方式我都可以将第一个属性名称定义为任何类型的字符串?
我正在尝试为字典列表(也称为对象数组)编写一个 jsonschema,在其中验证字典中的键。此示例中的标签是我感兴趣的。我想允许任意数量的标签,并希望验证name和value字段始终存在于标签字典中。以下是表示为 yaml 的示例输入。
some_field1: "value_a"
some_field2: "value_b"
labels:
- name: "bar"
value: "foo"
- name: "baz"
value: "blah"
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止拼凑的内容,但它并没有验证字典中的键。我不确定 extraProperites 在这种情况下是如何工作的,但我在网上找到了一个例子。
properties:
some_field1:
type: string
default: 'value_a'
some_field2:
type: string
default: 'value_b'
labels:
type: array
items:
type: object
additionalProperties:
type: string
Run Code Online (Sandbox Code Playgroud)
我的用例是,我尝试为 Kubernetes 创建自定义资源定义 (CRD),在其中验证输入,我的理解是 CRD 使用 openapi3/jsonschema 验证来定义其字段。
我无法找到有关如何使用特定键验证字典列表的信息。如果您提供任何帮助,我将不胜感激。
我有一个描述我的 API 的 JSON 架构文件。它包含一些定义以及我想忽略的代码生成的一些残留部分(properties和required字段):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"CreateBook": {
"properties": {
"title": {"type": "string"},
"author": {"type": "string"},
"numPages": {"type": "number"}
},
"required": ["title", "author"]
},
"CreateShelf": {
"properties": {
"books": {"type": "array", "items": {"type": "string"}}
},
"required": ["books"]
}
},
"properties": {
"/api/create-book": {
"properties": {"type": {"post": {"$ref": "#/definitions/CreateBook"}}},
"required": ["post"],
"type": "object"
},
"/api/create-shelf": {
"properties": {"type": {"post": {"$ref": "#/definitions/CreateShelf"}}},
"required": ["post"],
"type": "object"
}
},
"required": ["/api/create-book", "/api/create-shelf"],
"type": "object"
} …Run Code Online (Sandbox Code Playgroud) jsonschema ×10
json ×8
validation ×2
ajv ×1
java ×1
json.net ×1
kubernetes ×1
openapi ×1
schema ×1