对象
Object 是一种数据类型,用于以 JSON 保存对象定义。
💡 常用对象 Usage
对象属性类型可被用来存储任何基于键/值的数据,例如
- 配置;
- 标签
- HTTP 响应;
- 字典/哈希映射;
- 等等。
在live demo 这个示例中,我们可以看到 Tags
对象属性。
应用程序接口定义
- Basic
- Array
{
"myObjectProp": {
"title": "My object",
"icon": "My icon",
"description": "My object property",
"type": "object",
"default": {
"myKey": "myValue"
}
}
}
{
"myObjectArray": {
"title": "My object array",
"icon": "My icon",
"description": "My object array",
"type": "array",
"items": {
"type": "object"
}
}
}
Check out Port's API reference to learn more.
Terraform 定义
- Basic
- Array
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
object_props = {
"myObjectProp" = {
title = "My object"
icon = "My icon"
description = "My object property"
default = jsonencode({"myKey" = "myValue"})
}
}
}
}
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties {
identifier = "myObjectArray"
title = "My object array"
required = false
type = "array"
items = {
type = "object"
}
}
}
Pulumi 的定义
- Basic
- Array - coming soon
- Python
- TypeScript
- JavaScript
- GO
"""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={},
)
import * as pulumi from "@pulumi/pulumi";
import * as port from "@port-labs/port";
export const blueprint = new port.Blueprint("myBlueprint", {
identifier: "myBlueprint",
title: "My Blueprint",
properties: {
objectProps: {
myObjectProp: {
title: "My object",
required: true,
},
},
},
});
"use strict";
const pulumi = require("@pulumi/pulumi");
const port = require("@port-labs/port");
const entity = new port.Blueprint("myBlueprint", {
title: "My Blueprint",
identifier: "myBlueprint",
properties: {
objectProps: {
myObjectProp: {
title: "My object",
required: true,
},
},
},
relations: {},
});
exports.title = entity.title;
package main
import (
"github.com/port-labs/pulumi-port/sdk/go/port"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
blueprint, err := port.NewBlueprint(ctx, "myBlueprint", &port.BlueprintArgs{
Identifier: pulumi.String("myBlueprint"),
Title: pulumi.String("My Blueprint"),
Properties: port.BlueprintPropertiesArgs{
ObjectProps: port.BlueprintPropertiesObjectPropsMap{
"myObjectProp": port.BlueprintPropertiesObjectPropsArgs{
Title: pulumi.String("My object"),
Required: pulumi.Bool(false),
},
},
},
})
ctx.Export("blueprint", blueprint.Title)
if err != nil {
return err
}
return nil
})
}
验证对象
对象验证支持以下操作符:
properties
- 必须出现的键及其类型;additionalProperties
- 是否允许使用properties
中未定义的键,以及它们的类型;patternProperties
- 属性应遵循哪种 regex 模式
对象验证遵循 JSON 模式模型,请参阅JSON schema docs 了解所有可用验证
- Basic
- Array
- Terraform - coming soon
{
"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
}
}
{
"myObjectArray": {
"title": "My object array",
"icon": "My icon",
"description": "My object array",
"type": "array",
"items": {
"type": "object",
"properties": {
"myRequiredProp": { "type": "number" }
},
"patternProperties": {
"^S_": { "type": "string" },
"^I_": { "type": "number" }
},
"additionalProperties": true
}
}
}