Skip to main content

对象

Object 是一种数据类型,用于以 JSON 保存对象定义。

💡 常用对象 Usage

对象属性类型可被用来存储任何基于键/值的数据,例如

  • 配置;
  • 标签
  • HTTP 响应;
  • 字典/哈希映射;
  • 等等。

live demo 这个示例中,我们可以看到 Tags 对象属性。

应用程序接口定义

{
"myObjectProp": {
"title": "My object",
"icon": "My icon",
"description": "My object property",
"type": "object",
"default": {
"myKey": "myValue"
}
}
}

Check out Port's API reference to learn more.

Terraform 定义

resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
object_props = {
"myObjectProp" = {
title = "My object"
icon = "My icon"
description = "My object property"
default = jsonencode({"myKey" = "myValue"})
}
}
}
}

Pulumi 的定义

"""A Python Pulumi program"""

import pulumi
from port_pulumi import Blueprint,BlueprintPropertiesArgs,BlueprintPropertiesObjectPropsArgs

blueprint = Blueprint(
"myBlueprint",
identifier="myBlueprint",
title="My Blueprint",
properties=BlueprintPropertiesArgs(
object_props=BlueprintPropertiesObjectPropsArgs(
title="My object", required=False
)
),
relations={},
)

验证对象

对象验证支持以下操作符:

  • properties - 必须出现的键及其类型;
  • additionalProperties - 是否允许使用 properties 中未定义的键,以及它们的类型;
  • patternProperties - 属性应遵循哪种 regex 模式
对象验证遵循 JSON 模式模型,请参阅JSON schema docs 了解所有可用验证
{
"myObjectProp": {
"title": "My object",
"icon": "My icon",
"description": "My object property",
"type": "object",
"properties": {
"myRequiredProp": { "type": "number" }
},
"patternProperties": {
"^S_": { "type": "string" },
"^I_": { "type": "number" }
},
"additionalProperties": true
}
}