gpt4 book ai didi

json - JSON 模式中的重用模式

转载 作者:行者123 更新时间:2023-12-03 14:26:57 24 4
gpt4 key购买 nike

是否可以定义一次正则表达式并重新使用它?我有一些非常复杂的正则表达式,我想将它们用作模式中各种不同对象的大量属性值的模式。复制粘贴这个看起来像是在自找麻烦,但我似乎无法在任何地方找到合适的重用示例。

减少说明我想要做什么的模式。

{
"$schema": "http://json-schema.org/draft-04/schema#",
"patterns": {
"fqdn_or_ipaddress": "(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\\.)+[a-zA-Z]{2,63}$)||(((?:^[0-9])(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))(?![0-9])$)|(^\\*$))",
},
"properties": {
"server_hostname" : {
"type":"string",
"pattern": {"#ref", "#/patterns/address"},
},
"proxy_hostname" : {
"type":"string",
"pattern": {"#ref", "#/patterns/address"},
}
}
}

此处不验证 http://www.jsonschemavalidator.net/因为“模式”不是字符串。这是一个漏洞再利用。我看过patternProperties,但这似乎解决了完全不同的用例。

最佳答案

您只能$ref一个架构。你需要做这样的事情。

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"server_hostname" : {
"$ref": "#/definitions/fqdn_or_ipaddress",
"description": "The server hostname"
},
"proxy_hostname" : {
"allOf": [{ "$ref": "#/definitions/fqdn_or_ipaddress" }],
"description": "The proxy hostname"
}
},
"definitions": {
"fqdn_or_ipaddress": {
"type": "string",
"pattern": "(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\\.)+[a-zA-Z]{2,63}$)||(((?:^[0-9])(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))(?![0-9])$)|(^\\*$))"
}
}
}

编辑

我添加了两个关于如何从 $ref 扩展的示例。 .首先,您可以添加 description .它将被忽略,但这不是错误。自 description只是一个元数据关键字,这应该不是问题。

在第二个示例中,您可以使用 allOf包裹 $ref并且您可以添加您需要的任何关键字(甚至是非元数据关键字)。

关于json - JSON 模式中的重用模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44757591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com