### Install TerraSchema using Go Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Install the TerraSchema CLI tool using the Go installation command. This is the recommended method for obtaining the latest version. ```bash $ go install github.com/HewlettPackard/terraschema@latest ``` -------------------------------- ### Terraform Configuration Example (Nullable) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Example Terraform configuration file demonstrating variable definitions. Note that 'name' explicitly sets nullable to false, while 'age' does not specify it. ```hcl variable "name" { type = string nullable = false } variable "age" { type = number default = 10 } ``` -------------------------------- ### Install JSON Schema Validator (jv) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Install the 'jv' command-line tool for validating JSON files against a schema using Go. This tool is useful for testing generated schemas. ```bash $ go install github.com/santhosh-tekuri/jsonschema/cmd/jv@latest ``` -------------------------------- ### Terraform Variable Block Example Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md A typical Terraform variable block demonstrating various optional fields including type, default, description, nullable, sensitive, and validation rules. All fields are optional, and multiple validation blocks can be specified. ```hcl variable "age" { type = number default = 10 description = "Your age" nullable = false sensitive = false validation { condition = var.age >= 0 error_message = "Age must not be negative" } } ``` -------------------------------- ### Generated JSON Schema for a Variable Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md An example of a JSON schema generated by Terraschema for a module containing a single variable. This schema defines the variable's properties, including its type, default value, and validation rules. The `additionalProperties` field can be overridden. ```json { "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": true, "properties": { "age": { "description": "Your age", "default": 10, "minimum": 0, "type": "number" } }, "required": [] } ``` -------------------------------- ### Supported Optional Type Declaration Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Shows the supported format for optional type declarations in go-cty. ```hcl optional() ``` -------------------------------- ### Use tfvars.json with Terraform Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Apply a validated .tfvars.json file to Terraform commands like 'plan', 'apply', or 'destroy'. This shows how to integrate the generated schema validation into the Terraform workflow. ```bash $ terraform plan -var-file="input.tfvars.json" ``` ```bash $ terraform apply -var-file="input.tfvars.json" ``` ```bash $ terraform destroy -var-file="input.tfvars.json" ``` -------------------------------- ### String Length Validation (Inclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minLength' and 'maxLength' for string variables with inclusive length constraints. ```json {"minLength": 0, "maxLength": 10} ``` -------------------------------- ### String Length Validation (Exact) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minLength' and 'maxLength' set to the same value for strings of an exact length. ```json {"minLength": 5, "maxLength": 5} ``` -------------------------------- ### Array Length Validation (Inclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minItems' and 'maxItems' for list, tuple, or set types with inclusive item count constraints. ```json {"minItems": 0, "maxItems": 10} ``` -------------------------------- ### Validate JSON Schema with jv Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Use the 'jv' tool to validate an input JSON file against a generated schema file. This demonstrates how to check if a .tfvars.json file conforms to the TerraSchema output. ```bash $ jv schema.json input.tfvars.json ``` -------------------------------- ### Array Length Validation (Exact) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minItems' and 'maxItems' set to the same value for list, tuple, or set types with an exact item count. ```json {"minItems": 5, "maxItems": 5} ``` -------------------------------- ### Optional Type Error in go-cty Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Illustrates an error encountered in go-cty when using the optional type with more than one argument. ```hcl type = optional(,) ``` -------------------------------- ### JSON Schema for Set Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform set type in JSON Schema format. Similar to lists, but enforces unique items. ```json { "type": "array", "items": { "type": "" }, "uniqueItems": true } ``` -------------------------------- ### Object Length Validation (Inclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minProperties' and 'maxProperties' for map or object types with inclusive property count constraints. ```json {"minProperties": 0, "maxProperties": 10} ``` -------------------------------- ### Generated JSON Schema (With --nullable-all) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md The resulting JSON Schema when the Terraform configuration above is processed with the `--nullable-all` flag. 'age' is now treated as nullable, showing the 'anyOf' structure. ```json { "additionalProperties": true, "properties": { "age": { "anyOf": [ { "title": "null", "type": "null" }, { "title": "number", "type": "number" } ], "default": 10, "title": "age: Select a type" }, "name": { "type": "string" } }, "required": [ "age", "name" ] } ``` -------------------------------- ### String Length Validation (Exclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minLength' and 'maxLength' for string variables with exclusive length constraints. ```json {"minLength": 1, "maxLength": 9} ``` -------------------------------- ### Object Length Validation (Exact) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minProperties' and 'maxProperties' set to the same value for map or object types with an exact property count. ```json {"minProperties": 5, "maxProperties": 5} ``` -------------------------------- ### Number Value Range Validation (Inclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minimum' and 'maximum' for number variables within an inclusive range. ```json {"minimum": 0, "maximum": 10} ``` -------------------------------- ### Regex Validation Rule Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with a 'pattern' property for string variables that must match a given regular expression. ```json {"pattern": ""} ``` -------------------------------- ### Generated JSON Schema (No --nullable-all) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md The resulting JSON Schema when the Terraform configuration above is processed without the `--nullable-all` flag. 'age' is treated as non-nullable by default. ```json { "additionalProperties": true, "properties": { "age": { "default": 10, "type": "number" }, "name": { "type": "string" } }, "required": [ "age", "name" ] } ``` -------------------------------- ### JSON Schema for String Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform string type in JSON Schema format. This is a basic type definition used in schema generation. ```json { "type": "string" } ``` -------------------------------- ### JSON Schema for List Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform list type in JSON Schema format. The `items` property specifies the schema for elements within the list. ```json { "type": "array", "items": { "type": "" } } ``` -------------------------------- ### Array Length Validation (Exclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minItems' and 'maxItems' for list, tuple, or set types with exclusive item count constraints. ```json {"minItems": 1, "maxItems": 9} ``` -------------------------------- ### Go Struct for Terraform Variable Block Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md The Go struct representation of a Terraform variable block used internally by Terraschema. It maps HCL expressions and strings to Go types, with optional fields handled by pointers or slices. Empty expressions are filtered out after unmarshalling. ```go type VariableBlock struct { Type hcl.Expression // or nil Default hcl.Expression // or nil Description *string Nullable *bool Sensitive *bool Validation []struct{ Condition hcl.Expression ErrorMessage string } } ``` -------------------------------- ### Tuple Type Schema Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Defines the JSON schema for a tuple type, specifying the types of its elements and the minimum and maximum number of items. ```json { "type": "array", "items": [ { "type": "" }, ... { "type": "" } ], "minItems": N, "maxItems": N } ``` -------------------------------- ### JSON Schema for Object Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform object type in JSON Schema format. Defines properties and their types, and specifies required fields. `additionalProperties` can be overridden. ```json { "additionalProperties": true, "type": "object", "properties": { "": { "type": "" }, ... }, "required": [ "", ... ] } ``` -------------------------------- ### JSON Schema for Map Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform map type in JSON Schema format. The `additionalProperties` property defines the schema for values in the map. ```json { "type": "object", "additionalProperties": { "type": "" } } ``` -------------------------------- ### JSON Schema for Number Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform number type in JSON Schema format. This is a basic type definition used in schema generation. ```json { "type": "number" } ``` -------------------------------- ### Enum Validation Rule Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with an 'enum' property for variables that must match one of a specified list of values. ```json {"enum": [1, 2, ...]} ``` -------------------------------- ### Nullable JSON Schema Structure Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md This JSON structure is generated when a Terraform variable is marked as nullable. It uses 'anyOf' to allow either null or the specified type, primarily for react-jsonschema-form compatibility. ```JSON { "": { "anyOf": [ { "title": "null", "type": "null" }, { "title": "", "type": "" } ], "description": "", "default": "", "title": ": Select a type" } } ``` -------------------------------- ### Object Length Validation (Exclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'minProperties' and 'maxProperties' for map or object types with exclusive property count constraints. ```json {"minProperties": 1, "maxProperties": 9} ``` -------------------------------- ### JSON Schema for Boolean Type Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Represents a Terraform boolean type in JSON Schema format. This is a basic type definition used in schema generation. ```json { "type": "boolean" } ``` -------------------------------- ### Exported Variables JSON Format Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md The JSON format for exported variables when the `--export-variables` flag is used. This format includes all variable attributes, such as condition and error message for validation, directly in the JSON output. ```json { "age": { "description": "Your age", "default": 10, "sensitive": false, "nullable": false, "validation": [ { "condition": "var.age >= 0", "error_message": "Age must not be negative" } ], "type": "number" } } ``` -------------------------------- ### Number Value Range Validation (Exclusive) Source: https://github.com/hewlettpackard/terraschema/blob/main/README.md Generates a JSON schema with 'exclusiveMinimum' and 'exclusiveMaximum' for number variables within an exclusive range. ```json {"exclusiveMinimum": 0, "exclusiveMaximum": 10} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.