Ansible获得了对jsonschema的支持,我有各种各样的用例,但大多数都需要条件。我一直在阅读jsonschema和draft-7的文档,如果然后是else,并尝试了几种排列,似乎只处理了一半的目标。希望有人能为我提供一个可以解构的实际例子。
两种基于";send_notifications":
{
"capture_state": "value here",
"send_notifications": true,
"user_email_list": ["email@something.com", "email2@something.com"],
"host_info": [
{
"ansible_host": "192.20.xxx.xxx",
"ECRTicket": "1103035"
},
.... continued
]
}
或
{
"capture_state": "value here",
"send_notifications": false
}
本质上,只允许和要求示例中所示的基于";send_notifications";,如果这两种模式都不是,则无条件失败。我可能也会用模式来改进这一点,但只有这些键和类型是一个很好的开始。
我最初生成了以下基本模式,用作操作和添加条件的引用,也许这个模式中的某些内容与使用条件和additionalProperties相冲突:false,但作为jsonschema的新手,这对我来说并不明显,所以我可以寻求一些帮助,以便更频繁地使用它。如果有任何帮助,我们将不胜感激,感谢您的时间,新发布的堆栈,如果我能改进我的问题,请告诉我。
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"capture_state",
"send_notifications",
"user_email_list",
"host_info"
],
"properties": {
"capture_state": {
"type": "string"
},
"send_notifications": {
"type": "boolean"
},
"user_email_list": {
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
}
]
}
},
"host_info": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"required": [
"ansible_host",
"ECRTicket"
],
"properties": {
"ansible_host": {
"type": "string"
},
"ECRTicket": {
"type": "string"
}
},
"additionalProperties": false
}
]
}
}
},
"additionalProperties": false
}
通常,无论何时选择限制未知属性,都会遇到困难。我认为你有一个很好的理由,包括它是值得的。
有几种方法可以做到这一点。我之所以这样介绍它,是因为当JSON实例未通过模式验证时,它会生成最好的消息。
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"capture_state": { "type": "string" },
"send_notifications": { "type": "boolean" },
},
"required": ["capture_state", "send_notifications"],
"if": {
"properties": {
"send_notifications": { "const": true },
},
"required": ["send_notifications"]
},
"then": {
"properties": {
"capture_state": true,
"send_notifications": true,
"user_email_list": {
"type": "array",
"items": { "type": "string" }
},
"host_info": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ansible_host": { "type": "string" },
"ECRTicket": { "type": "string" }
},
"required": ["ansible_host", "ECRTicket"],
"additionalProperties": false
}
}
},
"required": ["user_email_list", "host_info"],
"additionalProperties": false
},
"else": {
"properties": {
"capture_state": true,
"send_notifications": true
},
"additionalProperties": false
}
}
additionalProperties
只识别在它出现的同一子架构中声明的属性。这意味着你需要包括一些伪properties
条目,这样它就知道在架构中其他地方声明的属性了。
我认为ansible不支持比draft-07更新版本的JSON Schema?如果是这样,您可以使用unevaluatedProperties
而不是additionalProperties
,以避免使用additionalProperties
时跳过一些困难。
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"capture_state": { "type": "string" },
"send_notifications": { "type": "boolean" },
},
"required": ["capture_state", "send_notifications"],
"unevaluatedProperties": false,
"if": {
"properties": {
"send_notifications": { "const": true },
},
"required": ["send_notifications"]
},
"then": {
"properties": {
"user_email_list": {
"type": "array",
"items": { "type": "string" }
},
"host_info": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ansible_host": { "type": "string" },
"ECRTicket": { "type": "string" }
},
"required": ["ansible_host", "ECRTicket"],
"additionalProperties": false
}
}
},
"required": ["user_email_list", "host_info"]
}
}
如果Ansible的任何人碰巧看到了这一点,2020-12草案包括unevaluatedProperties
,并将获得大量实现支持,因为它也包含在OpenAPI 3.1中。