次の方法で共有


ARM テンプレートの型定義

この記事では、Azure Resource Manager テンプレート (ARM テンプレート) で定義を作成して使用する方法について説明します。 独自の型を定義することで、これらの型を再利用できます。 型定義は 、languageVersion 2.0 でのみ使用できます。

Visual Studio Code 用の Azure Resource Manager Tools 拡張機能の現在のリリースでは、 languageVersion 2.0 で行われた機能強化は認識されません。

ヒント

Bicep には ARM テンプレートと同じ機能が用意されており、構文の方が使いやすいため、 Bicep をお勧めします。 詳細については、 Bicep のユーザー定義データ型に関するページを参照してください。

最小限の宣言

少なくとも、すべての型定義には名前と type または $refが必要です。

"definitions": {
  "demoStringType": {
    "type": "string"
  },
  "demoIntType": {
    "type": "int"
  },
  "demoBoolType": {
    "type": "bool"
  },
  "demoObjectType": {
    "type": "object"
  },
  "demoArrayType": {
    "type": "array"
  }
}

使用できる値

型定義に使用できる値を定義できます。 使用できる値は配列で指定します。 許可されている値の 1 つではない型定義の値が渡された場合、検証中にデプロイが失敗します。

"definitions": {
  "demoEnumType": {
    "type": "string",
    "allowedValues": [
      "one",
      "two"
    ]
  }
}

長さの制限

文字列型定義と配列型定義の最小長と最大長を指定できます。 一方または両方の制約を設定できます。 文字列の場合、長さは文字数を示します。 配列の場合、長さは配列内の項目数を示します。

次の例では、2 つの型定義を宣言します。 1 つの型定義は、3 ~ 24 文字のストレージ アカウント名用です。 もう 1 つの型定義は、1 ~ 5 個の項目を含む必要がある配列です。

"definitions": {
  "storageAccountNameType": {
    "type": "string",
    "minLength": 3,
    "maxLength": 24
  },
  "appNameType": {
    "type": "array",
    "minLength": 1,
    "maxLength": 5
  }
}

整数の制約

整数型定義の最小値と最大値を設定できます。 一方または両方の制約を設定できます。

"definitions": {
  "monthType": {
    "type": "int",
    "minValue": 1,
    "maxValue": 12
  }
}

オブジェクト制約

特性

propertiesの値は、プロパティ名 => 型定義のマップです。

次の例では、{"foo": "string", "bar": 1} は受け入れられますが、{"foo": "string", "bar": -1}{"foo": "", "bar": 1}、および foobar プロパティがないオブジェクトは拒否されます。

"definitions": {
  "objectDefinition": {
    "type": "object",
    "properties": {
      "foo": {
        "type": "string",
        "minLength": 3
      },
      "bar": {
        "type": "int",
        "minValue": 0
      }
    }
  }
},
"parameters": {
  "objectParameter": {
    "$ref": "#/definitions/objectDefinition",
  }
}

プロパティの型定義に "nullable": true 制約がない限り、すべてのプロパティが必要です。 前の例での両方のプロパティを省略できるようにするには、次のようにします。

"definitions": {
  "objectDefinition": {
    "type": "object",
    "properties": {
      "foo": {
        "type": "string",
        "minLength": 3,
        "nullable": true
      },
      "bar": {
        "type": "int",
        "minValue": 0,
        "nullable": true
      }
    }
  }
}

additionalプロパティ

additionalPropertiesの値は、型定義またはブール値です。 additionalProperties 制約が定義されていない場合、既定値は true です。

値が型定義の場合、この値では、properties 制約で示されていないすべてのプロパティに適用されるスキーマを記述します。 次の例では、{"fizz": "buzz", "foo": "bar"} は受け入れられますが、{"property": 1} は拒否されます。

"definitions": {
  "dictionaryDefinition": {
    "type": "object",
    "properties": {
      "foo": {
        "type": "string",
        "minLength": 3,
        "nullable": true
      },
      "bar": {
        "type": "int",
        "minValue": 0,
        "nullable": true
      }
    },
    "additionalProperties": {
      "type": "string"
    }
  }
}

値が false の場合、properties 制約で定義されているプロパティ以外のプロパティは指定できません。 次の例では、{"foo": "string", "bar": 1} は受け入れられますが、{"foo": "string", "bar": 1, "fizz": "buzz"} は拒否されます。

"definitions": {
  "dictionaryDefinition": {
    "type": "object",
    "properties": {
      "foo": {
        "type": "string",
        "minLength": 3
      },
      "bar": {
        "type": "int",
        "minValue": 0
      }
    },
    "additionalProperties": false
  }
}

値が true の場合、properties 制約で定義されていないプロパティでは、どの値でも受け入れられます。 次の例では、{"foo": "string", "bar": 1, "fizz": "buzz"} は受け入れられます。

"definitions": {
  "dictionaryDefinition": {
    "type": "object",
    "properties": {
      "foo": {
        "type": "string",
        "minLength": 3
      },
      "bar": {
        "type": "int",
        "minValue": 0
      }
    },
    "additionalProperties": true
  }
}

識別子

discriminator では、識別子プロパティに基づいて適用されるスキーマを定義します。 次の例では、{"type": "ints", "foo": 1, "bar": 2}{"type": "strings", "fizz": "buzz", "pop": "goes", "the": "weasel"} は受け入れられますが、{"type": "ints", "fizz": "buzz"} は拒否されます。

"definitions": {
  "taggedUnionDefinition": {
    "type": "object",
    "discriminator": {
      "propertyName": "type",
      "mapping": {
        "ints": {
          "type": "object",
          "additionalProperties": {"type": "int"}
        },
        "strings": {
          "type": "object",
          "additionalProperties": {"type": "string"}
          }
      }
    }
  }
}

配列制約

prefixItems(プレフィックスアイテムズ)

prefixItemsの値は、型定義の配列です。 この値での各型定義は、配列の要素を同じインデックスで検証するために使用されるスキーマです。 次の例では、[1, true] は受け入れられますが、[1, "string"][1] は拒否されます。

"definitions": {
  "tupleDefinition": {
    "type": "array",
    "prefixItems": [
      { "type": "int" },
      { "type": "bool" }
    ]
  }
},
"parameters": {
  "tupleParameter": {
    "$ref": "#/definitions/tupleDefinition"
  }
}

アイテム

itemsの値は、型定義またはブール値です。 items 制約が定義されていない場合、既定値は true です。

値が型定義の場合、この値では、インデックスが prefixItems 制約の最大インデックスより大きいすべての配列要素に適用されるスキーマを記述します。 次の例では、[1, true, 1][1, true, 1, 1] は受け入れられますが、[1, true, "foo"] は拒否されます。

"definitions": {
  "tupleDefinition": {
    "type": "array",
    "prefixItems": [
      { "type": "int" },
      { "type": "bool" }
    ],
    "items": { "type": "int" }
  }
},
"parameters": {
  "tupleParameter": {
    "$ref": "#/definitions/tupleDefinition"
  }
}

items を使用せずに prefixItems を使用できます。 次の例では、[1, 2][1] は受け入れられますが、["foo"] は拒否されます。

"definitions": {
  "intArrayDefinition": {
    "type": "array",
    "items": { "type": "int" }
  }
},
"parameters": {
  "intArrayParameter": {
    "$ref": "#/definitions/intArrayDefinition"
  }
}

値が false の場合、検証される配列は、prefixItems 制約と同じ長さである必要があります。 次の例では、[1, true] は受け入れられますが、[1, true, 1][1, true, false, "foo", "bar"] は拒否されます。

"definitions": {
  "tupleDefinition": {
    "type": "array",
    "prefixItems": [
      {"type": "int"},
      {"type": "bool"}
    ]
  },
  "items": false
}

値が true の場合、インデックスが prefixItems 制約の最大インデックスより大きい配列要素では、どの値でも受け入れられます。 次の例では、[1, true][1, true, 1][1, true, false, "foo", "bar"] は受け入れられます。

"definitions": {
  "tupleDefinition": {
    "type": "array",
    "prefixItems": [
      {"type": "int"},
      {"type": "bool"}
    ]
  }
}
"definitions": {
  "tupleDefinition": {
    "type": "array",
    "prefixItems": [
      {"type": "int"},
      {"type": "bool"}
    ]
  },
  "items": true
}

Null 許容制約

null 許容制約は、値が null または省略される可能性があることを示します。 例については、「 プロパティ」 を参照してください。

説明

テンプレートのユーザーが提供する値を理解できるように、型定義に説明を追加できます。

"definitions": {
  "virtualMachineSize": {
    "type": "string",
    "metadata": {
      "description": "Must be at least Standard_A3 to support 2 NICs."
    },
    "defaultValue": "Standard_DS1_v2"
  }
}

定義を使用する

型定義を参照するには、次の構文を使用します。

"$ref": "#/definitions/<definition-name>"

次の例は、パラメーターと出力から型定義を参照する方法を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "languageVersion": "2.0",

  "definitions": {
    "naturalNumber": {
      "type": "int",
      "minValue": 1
    }
  },
  "parameters": {
    "numberParam": {
      "$ref": "#/definitions/naturalNumber",
      "defaultValue": 0
    }
  },
  "resources": {},
  "outputs": {
    "output1": {
      "$ref": "#/definitions/naturalNumber",
      "value": "[parameters('numberParam')]"
    }
  }
}

次のステップ