diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..099dc47 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +test/expected/*/*.json linguist-generated=true \ No newline at end of file diff --git a/Makefile b/Makefile index 27d56f1..f21f9b7 100644 --- a/Makefile +++ b/Makefile @@ -18,4 +18,13 @@ terraschema: .PHONY: clean clean: - @rm -f terraschema \ No newline at end of file + @rm -f terraschema + +.PHONY: test-data +test-data: + @for name in `ls test/modules`; do \ + go run . -i test/modules/$$name -o test/expected/$$name/schema.json --overwrite --allow-empty; \ + go run . -i test/modules/$$name -o test/expected/$$name/variables.json --overwrite --allow-empty --export-variables; \ + go run . -i test/modules/$$name -o test/expected/$$name/schema-disallow-additional.json --overwrite --allow-empty --disallow-additional-properties; \ + go run . -i test/modules/$$name -o test/expected/$$name/schema-nullable-all.json --overwrite --allow-empty --nullable-all; \ + done diff --git a/pkg/jsonschema/json-schema.go b/pkg/jsonschema/json-schema.go index 6eb27bd..0bda054 100644 --- a/pkg/jsonschema/json-schema.go +++ b/pkg/jsonschema/json-schema.go @@ -64,6 +64,7 @@ func CreateSchema(path string, options CreateSchemaOptions) (map[string]any, err return schemaOut, nil } +//nolint:cyclop func createNode(name string, v model.TranslatedVariable, options CreateSchemaOptions) (map[string]any, error) { tc, err := reader.GetTypeConstraint(v.Variable.Type) if err != nil { @@ -115,6 +116,12 @@ func createNode(name string, v model.TranslatedVariable, options CreateSchemaOpt node["description"] = *v.Variable.Description } + // if nullable is true, then we need to unset the definition for "type" here, since it was only added to + // satisfy the validation rules and is not actually a part of the schema. + if nullableTranslatedValue { + delete(node, "type") + } + return node, nil } diff --git a/pkg/jsonschema/type.go b/pkg/jsonschema/type.go index fc02636..93c91d8 100644 --- a/pkg/jsonschema/type.go +++ b/pkg/jsonschema/type.go @@ -55,6 +55,10 @@ func getNullableNode(name string, typeInterface any, options CreateSchemaOptions } node["title"] = fmt.Sprintf("%s: Select a type", name) + // this is here until the validation rules are applied, because otherwise they error since "type" is undefined. + // This sets validation to apply to the top level, which implies that validation must pass even if the value is null. + node["type"] = internalNode["type"] + return node, nil } diff --git a/pkg/jsonschema/validation.go b/pkg/jsonschema/validation.go index 8d8ac92..c17e202 100644 --- a/pkg/jsonschema/validation.go +++ b/pkg/jsonschema/validation.go @@ -24,7 +24,7 @@ func parseConditionToNode(ex hcl.Expression, _ string, name string, m *map[strin } t, ok := (*m)["type"].(string) if !ok { - return fmt.Errorf("cannot apply validation, type is not defined for %#v", *m) + return fmt.Errorf("cannot apply validation, type is not defined for %v", *m) } functions := map[string]conditionMutator{ "contains([...],var.input_parameter)": contains, diff --git a/test/expected/complex-types/schema-disallow-additional.json b/test/expected/complex-types/schema-disallow-additional.json new file mode 100644 index 0000000..4e0e1a0 --- /dev/null +++ b/test/expected/complex-types/schema-disallow-additional.json @@ -0,0 +1,170 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": false, + "properties": { + "a_very_complicated_object": { + "additionalProperties": false, + "default": { + "b": [ + [ + "a", + "b", + "c" + ], + true + ], + "c": { + "a": [ + "a" + ], + "b": [ + "b" + ] + }, + "d": { + "a": [ + [ + "a", + "b" + ], + [ + "c", + "d" + ] + ], + "b": 1 + }, + "e": [ + "a", + 1 + ], + "f": [ + [ + "a" + ], + [ + "b" + ], + [ + "a", + "b" + ] + ] + }, + "description": "This is a very complicated object", + "properties": { + "a": { + "type": "string" + }, + "b": { + "items": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "boolean" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + }, + "c": { + "additionalProperties": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "object" + }, + "d": { + "additionalProperties": false, + "properties": { + "a": { + "items": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "array" + }, + "b": { + "type": "number" + } + }, + "required": [ + "a", + "b" + ], + "type": "object" + }, + "e": { + "items": [ + { + "type": "string" + }, + { + "type": "number" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + }, + "f": { + "items": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "array", + "uniqueItems": true + } + }, + "required": [ + "b", + "c", + "d", + "e", + "f" + ], + "type": "object" + }, + "an_object_with_optional": { + "additionalProperties": false, + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object variable with an optional field", + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + }, + "c": { + "type": "boolean" + }, + "d": { + "type": "string" + } + }, + "required": [ + "a", + "b", + "c" + ], + "type": "object" + } + }, + "required": [] +} diff --git a/test/expected/complex-types/schema-nullable-all.json b/test/expected/complex-types/schema-nullable-all.json new file mode 100644 index 0000000..81e07ab --- /dev/null +++ b/test/expected/complex-types/schema-nullable-all.json @@ -0,0 +1,190 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "a_very_complicated_object": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "additionalProperties": true, + "properties": { + "a": { + "type": "string" + }, + "b": { + "items": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "boolean" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + }, + "c": { + "additionalProperties": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "object" + }, + "d": { + "additionalProperties": true, + "properties": { + "a": { + "items": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "array" + }, + "b": { + "type": "number" + } + }, + "required": [ + "a", + "b" + ], + "type": "object" + }, + "e": { + "items": [ + { + "type": "string" + }, + { + "type": "number" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + }, + "f": { + "items": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "array", + "uniqueItems": true + } + }, + "required": [ + "b", + "c", + "d", + "e", + "f" + ], + "title": "object", + "type": "object" + } + ], + "default": { + "b": [ + [ + "a", + "b", + "c" + ], + true + ], + "c": { + "a": [ + "a" + ], + "b": [ + "b" + ] + }, + "d": { + "a": [ + [ + "a", + "b" + ], + [ + "c", + "d" + ] + ], + "b": 1 + }, + "e": [ + "a", + 1 + ], + "f": [ + [ + "a" + ], + [ + "b" + ], + [ + "a", + "b" + ] + ] + }, + "description": "This is a very complicated object", + "title": "a_very_complicated_object: Select a type" + }, + "an_object_with_optional": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "additionalProperties": true, + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + }, + "c": { + "type": "boolean" + }, + "d": { + "type": "string" + } + }, + "required": [ + "a", + "b", + "c" + ], + "title": "object", + "type": "object" + } + ], + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object variable with an optional field", + "title": "an_object_with_optional: Select a type" + } + }, + "required": [] +} diff --git a/test/expected/complex-types/schema.json b/test/expected/complex-types/schema.json index 388a07b..6ea6578 100644 --- a/test/expected/complex-types/schema.json +++ b/test/expected/complex-types/schema.json @@ -1,170 +1,170 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "additionalProperties": true, - "properties": { - "a_very_complicated_object": { - "additionalProperties": true, - "default": { - "b": [ - [ - "a", - "b", - "c" - ], - true - ], - "c": { - "a": [ - "a" - ], - "b": [ - "b" - ] - }, - "d": { - "a": [ - [ - "a", - "b" - ], - [ - "c", - "d" - ] - ], - "b": 1 - }, - "e": [ - "a", - 1 - ], - "f": [ - [ - "a" - ], - [ - "b" - ], - [ - "a", - "b" - ] - ] - }, - "description": "This is a very complicated object", - "properties": { - "a": { - "type": "string" - }, - "b": { - "items": [ - { - "items": { - "type": "string" - }, - "type": "array" - }, - { - "type": "boolean" - } - ], - "maxItems": 2, - "minItems": 2, - "type": "array" - }, - "c": { - "additionalProperties": { - "items": { - "type": "string" - }, - "type": "array" - }, - "type": "object" - }, - "d": { - "additionalProperties": true, - "properties": { - "a": { - "items": { - "items": { - "type": "string" - }, - "type": "array" - }, - "type": "array" - }, - "b": { - "type": "number" - } - }, - "required": [ - "a", - "b" - ], - "type": "object" - }, - "e": { - "items": [ - { - "type": "string" - }, - { - "type": "number" - } - ], - "maxItems": 2, - "minItems": 2, - "type": "array" - }, - "f": { - "items": { - "items": { - "type": "string" - }, - "type": "array" - }, - "type": "array", - "uniqueItems": true - } - }, - "required": [ - "b", - "c", - "d", - "e", - "f" - ], - "type": "object" - }, - "an_object_with_optional": { - "additionalProperties": true, - "default": { - "a": "a", - "b": 1, - "c": true - }, - "description": "This is an object variable with an optional field", - "properties": { - "a": { - "type": "string" - }, - "b": { - "type": "number" - }, - "c": { - "type": "boolean" - }, - "d": { - "type": "string" - } - }, - "required": [ - "a", - "b", - "c" - ], - "type": "object" - } - }, - "required": [] + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "a_very_complicated_object": { + "additionalProperties": true, + "default": { + "b": [ + [ + "a", + "b", + "c" + ], + true + ], + "c": { + "a": [ + "a" + ], + "b": [ + "b" + ] + }, + "d": { + "a": [ + [ + "a", + "b" + ], + [ + "c", + "d" + ] + ], + "b": 1 + }, + "e": [ + "a", + 1 + ], + "f": [ + [ + "a" + ], + [ + "b" + ], + [ + "a", + "b" + ] + ] + }, + "description": "This is a very complicated object", + "properties": { + "a": { + "type": "string" + }, + "b": { + "items": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "type": "boolean" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + }, + "c": { + "additionalProperties": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "object" + }, + "d": { + "additionalProperties": true, + "properties": { + "a": { + "items": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "array" + }, + "b": { + "type": "number" + } + }, + "required": [ + "a", + "b" + ], + "type": "object" + }, + "e": { + "items": [ + { + "type": "string" + }, + { + "type": "number" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + }, + "f": { + "items": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": "array", + "uniqueItems": true + } + }, + "required": [ + "b", + "c", + "d", + "e", + "f" + ], + "type": "object" + }, + "an_object_with_optional": { + "additionalProperties": true, + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object variable with an optional field", + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + }, + "c": { + "type": "boolean" + }, + "d": { + "type": "string" + } + }, + "required": [ + "a", + "b", + "c" + ], + "type": "object" + } + }, + "required": [] } diff --git a/test/expected/complex-types/variables.json b/test/expected/complex-types/variables.json new file mode 100644 index 0000000..d8ad984 --- /dev/null +++ b/test/expected/complex-types/variables.json @@ -0,0 +1,125 @@ +{ + "a_very_complicated_object": { + "default": { + "b": [ + [ + "a", + "b", + "c" + ], + true + ], + "c": { + "a": [ + "a" + ], + "b": [ + "b" + ] + }, + "d": { + "a": [ + [ + "a", + "b" + ], + [ + "c", + "d" + ] + ], + "b": 1 + }, + "e": [ + "a", + 1 + ], + "f": [ + [ + "a" + ], + [ + "b" + ], + [ + "a", + "b" + ] + ] + }, + "description": "This is a very complicated object", + "type": [ + "object", + { + "a": "string", + "b": [ + "tuple", + [ + [ + "list", + "string" + ], + "bool" + ] + ], + "c": [ + "map", + [ + "list", + "string" + ] + ], + "d": [ + "object", + { + "a": [ + "list", + [ + "list", + "string" + ] + ], + "b": "number" + } + ], + "e": [ + "tuple", + [ + "string", + "number" + ] + ], + "f": [ + "set", + [ + "list", + "string" + ] + ] + }, + [ + "a" + ] + ] + }, + "an_object_with_optional": { + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object variable with an optional field", + "type": [ + "object", + { + "a": "string", + "b": "number", + "c": "bool", + "d": "string" + }, + [ + "d" + ] + ] + } +} diff --git a/test/expected/custom-validation/schema-disallow-additional.json b/test/expected/custom-validation/schema-disallow-additional.json new file mode 100644 index 0000000..cf53f83 --- /dev/null +++ b/test/expected/custom-validation/schema-disallow-additional.json @@ -0,0 +1,196 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": false, + "properties": { + "a_list_maximum_minimum_length": { + "default": [ + "a" + ], + "description": "A list variable that must have a length greater than 0 and less than 10", + "items": { + "type": "string" + }, + "maxItems": 9, + "minItems": 1, + "type": "array" + }, + "a_map_maximum_minimum_entries": { + "additionalProperties": { + "type": "string" + }, + "default": { + "a": "a" + }, + "description": "A map variable that must have greater than 0 and less than 10 entries", + "maxProperties": 9, + "minProperties": 1, + "type": "object" + }, + "a_number_enum_kind_1": { + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "enum": [ + 1, + 2, + 3 + ], + "type": "number" + }, + "a_number_enum_kind_2": { + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "enum": [ + 1, + 2, + 3 + ], + "type": "number" + }, + "a_number_exclusive_maximum_minimum": { + "default": 1, + "description": "A number variable that must be greater than 0 and less than 10", + "exclusiveMaximum": 10, + "exclusiveMinimum": 0, + "type": "number" + }, + "a_number_maximum_minimum": { + "default": 0, + "description": "A number variable that must be between 0 and 10 (inclusive)", + "maximum": 10, + "minimum": 0, + "type": "number" + }, + "a_set_maximum_minimum_items": { + "default": [ + "a" + ], + "description": "A set variable that must have a length greater than 0 and less than 10", + "items": { + "type": "string" + }, + "maxItems": 9, + "minItems": 1, + "type": "array", + "uniqueItems": true + }, + "a_string_enum_escaped_characters_kind_1": { + "default": "\\", + "description": "A string variable that must some complicated escaped characters", + "enum": [ + "\\", + "\"", + "\\\"", + "${abc}", + "\n", + "\t", + "10%", + "10%%", + "$a", + "$$a", + "\r", + "\\r", + null, + "<", + ">", + "&" + ], + "type": "string" + }, + "a_string_enum_escaped_characters_kind_2": { + "default": "\"", + "description": "A string variable that must some complicated escaped characters", + "enum": [ + "\\", + "\"", + "\\\"", + "${abc}", + "\n", + "\t", + "10%", + "10%%", + "$a", + "$$a", + "\r", + "\\r", + null, + "<", + ">", + "&" + ], + "type": "string" + }, + "a_string_enum_kind_1": { + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "enum": [ + "a", + "b", + "c" + ], + "type": "string" + }, + "a_string_enum_kind_2": { + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "enum": [ + "a", + "b", + "c" + ], + "type": "string" + }, + "a_string_length_over_defined": { + "default": "a", + "description": "A string variable that must have length 4", + "maxLength": 4, + "minLength": 4, + "type": "string" + }, + "a_string_maximum_minimum_length": { + "default": "a", + "description": "A string variable that must have a length less than 10 and greater than 0", + "maxLength": 9, + "minLength": 1, + "type": "string" + }, + "a_string_pattern_1": { + "default": "1.1.1.1", + "description": "A string variable that must be a valid IPv4 address", + "pattern": "^[0-9]{1,3}(\\.[0-9]{1,3}){3}$", + "type": "string" + }, + "a_string_pattern_2": { + "default": "#000000", + "description": "string that must be a valid colour hex code in the form #RRGGBB", + "pattern": "^#[0-9a-fA-F]{6}$", + "type": "string" + }, + "a_string_set_length": { + "default": "abcd", + "description": "A string variable that must have length 4", + "maxLength": 4, + "minLength": 4, + "type": "string" + }, + "an_object_maximum_minimum_items": { + "additionalProperties": false, + "default": { + "name": "a", + "other_field": "b" + }, + "description": "An object variable that must have fewer than 3 properties", + "maxProperties": 2, + "minProperties": 1, + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + } + }, + "required": [] +} diff --git a/test/expected/custom-validation/schema-nullable-all.json b/test/expected/custom-validation/schema-nullable-all.json new file mode 100644 index 0000000..728bde3 --- /dev/null +++ b/test/expected/custom-validation/schema-nullable-all.json @@ -0,0 +1,366 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "a_list_maximum_minimum_length": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "items": { + "type": "string" + }, + "title": "array", + "type": "array" + } + ], + "default": [ + "a" + ], + "description": "A list variable that must have a length greater than 0 and less than 10", + "maxItems": 9, + "minItems": 1, + "title": "a_list_maximum_minimum_length: Select a type" + }, + "a_map_maximum_minimum_entries": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "additionalProperties": { + "type": "string" + }, + "title": "object", + "type": "object" + } + ], + "default": { + "a": "a" + }, + "description": "A map variable that must have greater than 0 and less than 10 entries", + "maxProperties": 9, + "minProperties": 1, + "title": "a_map_maximum_minimum_entries: Select a type" + }, + "a_number_enum_kind_1": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "number", + "type": "number" + } + ], + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "enum": [ + 1, + 2, + 3 + ], + "title": "a_number_enum_kind_1: Select a type" + }, + "a_number_enum_kind_2": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "number", + "type": "number" + } + ], + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "enum": [ + 1, + 2, + 3 + ], + "title": "a_number_enum_kind_2: Select a type" + }, + "a_number_exclusive_maximum_minimum": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "number", + "type": "number" + } + ], + "default": 1, + "description": "A number variable that must be greater than 0 and less than 10", + "exclusiveMaximum": 10, + "exclusiveMinimum": 0, + "title": "a_number_exclusive_maximum_minimum: Select a type" + }, + "a_number_maximum_minimum": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "number", + "type": "number" + } + ], + "default": 0, + "description": "A number variable that must be between 0 and 10 (inclusive)", + "maximum": 10, + "minimum": 0, + "title": "a_number_maximum_minimum: Select a type" + }, + "a_set_maximum_minimum_items": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "items": { + "type": "string" + }, + "title": "array", + "type": "array", + "uniqueItems": true + } + ], + "default": [ + "a" + ], + "description": "A set variable that must have a length greater than 0 and less than 10", + "maxItems": 9, + "minItems": 1, + "title": "a_set_maximum_minimum_items: Select a type" + }, + "a_string_enum_escaped_characters_kind_1": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "\\", + "description": "A string variable that must some complicated escaped characters", + "enum": [ + "\\", + "\"", + "\\\"", + "${abc}", + "\n", + "\t", + "10%", + "10%%", + "$a", + "$$a", + "\r", + "\\r", + null, + "<", + ">", + "&" + ], + "title": "a_string_enum_escaped_characters_kind_1: Select a type" + }, + "a_string_enum_escaped_characters_kind_2": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "\"", + "description": "A string variable that must some complicated escaped characters", + "enum": [ + "\\", + "\"", + "\\\"", + "${abc}", + "\n", + "\t", + "10%", + "10%%", + "$a", + "$$a", + "\r", + "\\r", + null, + "<", + ">", + "&" + ], + "title": "a_string_enum_escaped_characters_kind_2: Select a type" + }, + "a_string_enum_kind_1": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "enum": [ + "a", + "b", + "c" + ], + "title": "a_string_enum_kind_1: Select a type" + }, + "a_string_enum_kind_2": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "enum": [ + "a", + "b", + "c" + ], + "title": "a_string_enum_kind_2: Select a type" + }, + "a_string_length_over_defined": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "a", + "description": "A string variable that must have length 4", + "maxLength": 4, + "minLength": 4, + "title": "a_string_length_over_defined: Select a type" + }, + "a_string_maximum_minimum_length": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "a", + "description": "A string variable that must have a length less than 10 and greater than 0", + "maxLength": 9, + "minLength": 1, + "title": "a_string_maximum_minimum_length: Select a type" + }, + "a_string_pattern_1": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "1.1.1.1", + "description": "A string variable that must be a valid IPv4 address", + "pattern": "^[0-9]{1,3}(\\.[0-9]{1,3}){3}$", + "title": "a_string_pattern_1: Select a type" + }, + "a_string_pattern_2": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "#000000", + "description": "string that must be a valid colour hex code in the form #RRGGBB", + "pattern": "^#[0-9a-fA-F]{6}$", + "title": "a_string_pattern_2: Select a type" + }, + "a_string_set_length": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "abcd", + "description": "A string variable that must have length 4", + "maxLength": 4, + "minLength": 4, + "title": "a_string_set_length: Select a type" + }, + "an_object_maximum_minimum_items": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "additionalProperties": true, + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ], + "title": "object", + "type": "object" + } + ], + "default": { + "name": "a", + "other_field": "b" + }, + "description": "An object variable that must have fewer than 3 properties", + "maxProperties": 2, + "minProperties": 1, + "title": "an_object_maximum_minimum_items: Select a type" + } + }, + "required": [] +} diff --git a/test/expected/custom-validation/schema.json b/test/expected/custom-validation/schema.json index dd3d1fd..7ba116f 100644 --- a/test/expected/custom-validation/schema.json +++ b/test/expected/custom-validation/schema.json @@ -1,188 +1,196 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "additionalProperties": true, - "properties": { - "a_list_maximum_minimum_length": { - "default": [ - "a" - ], - "description": "A list variable that must have a length greater than 0 and less than 10", - "items": { - "type": "string" - }, - "maxItems": 9, - "minItems": 1, - "type": "array" - }, - "a_map_maximum_minimum_entries": { - "additionalProperties": { - "type": "string" - }, - "default": { - "a": "a" - }, - "description": "A map variable that must have greater than 0 and less than 10 entries", - "maxProperties": 9, - "minProperties": 1, - "type": "object" - }, - "a_number_enum_kind_1": { - "default": 1, - "description": "A number variable that must be one of the values 1, 2, or 3", - "enum": [ - 1, - 2, - 3 - ], - "type": "number" - }, - "a_number_enum_kind_2": { - "default": 1, - "description": "A number variable that must be one of the values 1, 2, or 3", - "enum": [ - 1, - 2, - 3 - ], - "type": "number" - }, - "a_number_exclusive_maximum_minimum": { - "default": 1, - "description": "A number variable that must be greater than 0 and less than 10", - "exclusiveMaximum": 10, - "exclusiveMinimum": 0, - "type": "number" - }, - "a_number_maximum_minimum": { - "default": 0, - "description": "A number variable that must be between 0 and 10 (inclusive)", - "maximum": 10, - "minimum": 0, - "type": "number" - }, - "a_set_maximum_minimum_items": { - "default": [ - "a" - ], - "description": "A set variable that must have a length greater than 0 and less than 10", - "items": { - "type": "string" - }, - "maxItems": 9, - "minItems": 1, - "type": "array", - "uniqueItems": true - }, - "a_string_enum_escaped_characters_kind_1": { - "default": "\\", - "description": "A string variable that must some complicated escaped characters", - "enum": [ - "\\", - "\"", - "\\\"", - "${abc}", - "\n", - "\t", - "10%", - "10%%", - "$a", - "$$a", - "\r", - "\\r" - ], - "type": "string" - }, - "a_string_enum_escaped_characters_kind_2": { - "default": "\"", - "description": "A string variable that must some complicated escaped characters", - "enum": [ - "\\", - "\"", - "\\\"", - "${abc}", - "\n", - "\t", - "10%", - "10%%", - "$a", - "$$a", - "\r", - "\\r" - ], - "type": "string" - }, - "a_string_enum_kind_1": { - "default": "a", - "description": "A string variable that must be one of the values 'a', 'b', or 'c'", - "enum": [ - "a", - "b", - "c" - ], - "type": "string" - }, - "a_string_enum_kind_2": { - "default": "a", - "description": "A string variable that must be one of the values 'a', 'b', or 'c'", - "enum": [ - "a", - "b", - "c" - ], - "type": "string" - }, - "a_string_length_over_defined": { - "default": "a", - "description": "A string variable that must have length 4", - "maxLength": 4, - "minLength": 4, - "type": "string" - }, - "a_string_maximum_minimum_length": { - "default": "a", - "description": "A string variable that must have a length less than 10 and greater than 0", - "maxLength": 9, - "minLength": 1, - "type": "string" - }, - "a_string_pattern_1": { - "default": "1.1.1.1", - "description": "A string variable that must be a valid IPv4 address", - "pattern": "^[0-9]{1,3}(\\.[0-9]{1,3}){3}$", - "type": "string" - }, - "a_string_pattern_2": { - "default": "#000000", - "description": "string that must be a valid colour hex code in the form #RRGGBB", - "pattern": "^#[0-9a-fA-F]{6}$", - "type": "string" - }, - "a_string_set_length": { - "default": "abcd", - "description": "A string variable that must have length 4", - "maxLength": 4, - "minLength": 4, - "type": "string" - }, - "an_object_maximum_minimum_items": { - "additionalProperties": true, - "default": { - "name": "a", - "other_field": "b" - }, - "description": "An object variable that must have fewer than 3 properties", - "maxProperties": 2, - "minProperties": 1, - "properties": { - "name": { - "type": "string" - } - }, - "required": [ - "name" - ], - "type": "object" - } - }, - "required": [] -} \ No newline at end of file + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "a_list_maximum_minimum_length": { + "default": [ + "a" + ], + "description": "A list variable that must have a length greater than 0 and less than 10", + "items": { + "type": "string" + }, + "maxItems": 9, + "minItems": 1, + "type": "array" + }, + "a_map_maximum_minimum_entries": { + "additionalProperties": { + "type": "string" + }, + "default": { + "a": "a" + }, + "description": "A map variable that must have greater than 0 and less than 10 entries", + "maxProperties": 9, + "minProperties": 1, + "type": "object" + }, + "a_number_enum_kind_1": { + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "enum": [ + 1, + 2, + 3 + ], + "type": "number" + }, + "a_number_enum_kind_2": { + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "enum": [ + 1, + 2, + 3 + ], + "type": "number" + }, + "a_number_exclusive_maximum_minimum": { + "default": 1, + "description": "A number variable that must be greater than 0 and less than 10", + "exclusiveMaximum": 10, + "exclusiveMinimum": 0, + "type": "number" + }, + "a_number_maximum_minimum": { + "default": 0, + "description": "A number variable that must be between 0 and 10 (inclusive)", + "maximum": 10, + "minimum": 0, + "type": "number" + }, + "a_set_maximum_minimum_items": { + "default": [ + "a" + ], + "description": "A set variable that must have a length greater than 0 and less than 10", + "items": { + "type": "string" + }, + "maxItems": 9, + "minItems": 1, + "type": "array", + "uniqueItems": true + }, + "a_string_enum_escaped_characters_kind_1": { + "default": "\\", + "description": "A string variable that must some complicated escaped characters", + "enum": [ + "\\", + "\"", + "\\\"", + "${abc}", + "\n", + "\t", + "10%", + "10%%", + "$a", + "$$a", + "\r", + "\\r", + null, + "<", + ">", + "&" + ], + "type": "string" + }, + "a_string_enum_escaped_characters_kind_2": { + "default": "\"", + "description": "A string variable that must some complicated escaped characters", + "enum": [ + "\\", + "\"", + "\\\"", + "${abc}", + "\n", + "\t", + "10%", + "10%%", + "$a", + "$$a", + "\r", + "\\r", + null, + "<", + ">", + "&" + ], + "type": "string" + }, + "a_string_enum_kind_1": { + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "enum": [ + "a", + "b", + "c" + ], + "type": "string" + }, + "a_string_enum_kind_2": { + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "enum": [ + "a", + "b", + "c" + ], + "type": "string" + }, + "a_string_length_over_defined": { + "default": "a", + "description": "A string variable that must have length 4", + "maxLength": 4, + "minLength": 4, + "type": "string" + }, + "a_string_maximum_minimum_length": { + "default": "a", + "description": "A string variable that must have a length less than 10 and greater than 0", + "maxLength": 9, + "minLength": 1, + "type": "string" + }, + "a_string_pattern_1": { + "default": "1.1.1.1", + "description": "A string variable that must be a valid IPv4 address", + "pattern": "^[0-9]{1,3}(\\.[0-9]{1,3}){3}$", + "type": "string" + }, + "a_string_pattern_2": { + "default": "#000000", + "description": "string that must be a valid colour hex code in the form #RRGGBB", + "pattern": "^#[0-9a-fA-F]{6}$", + "type": "string" + }, + "a_string_set_length": { + "default": "abcd", + "description": "A string variable that must have length 4", + "maxLength": 4, + "minLength": 4, + "type": "string" + }, + "an_object_maximum_minimum_items": { + "additionalProperties": true, + "default": { + "name": "a", + "other_field": "b" + }, + "description": "An object variable that must have fewer than 3 properties", + "maxProperties": 2, + "minProperties": 1, + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + } + }, + "required": [] +} diff --git a/test/expected/custom-validation/variables.json b/test/expected/custom-validation/variables.json new file mode 100644 index 0000000..b086b94 --- /dev/null +++ b/test/expected/custom-validation/variables.json @@ -0,0 +1,178 @@ +{ + "a_list_maximum_minimum_length": { + "default": [ + "a" + ], + "description": "A list variable that must have a length greater than 0 and less than 10", + "validation": { + "condition": "length(var.a_list_maximum_minimum_length) > 0 && length(var.a_list_maximum_minimum_length) < 10", + "error_message": "a_list_maximum_minimum_length must have a length greater than 0 and less than 10" + }, + "type": [ + "list", + "string" + ] + }, + "a_map_maximum_minimum_entries": { + "default": { + "a": "a" + }, + "description": "A map variable that must have greater than 0 and less than 10 entries", + "validation": { + "condition": "length(var.a_map_maximum_minimum_entries) > 0 && length(var.a_map_maximum_minimum_entries)< 10", + "error_message": "a_map_maximum_minimum_entries must greater than 0 and less than 10 entries" + }, + "type": [ + "map", + "string" + ] + }, + "a_number_enum_kind_1": { + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "validation": { + "condition": "contains([1, 2, 3], var.a_number_enum_kind_1)", + "error_message": "Invalid value for a_number_enum_kind_1" + }, + "type": "number" + }, + "a_number_enum_kind_2": { + "default": 1, + "description": "A number variable that must be one of the values 1, 2, or 3", + "validation": { + "condition": "var.a_number_enum_kind_2 == 1 || var.a_number_enum_kind_2 == 2 || var.a_number_enum_kind_2 == 3", + "error_message": "Invalid value for a_number_enum_kind_2" + }, + "type": "number" + }, + "a_number_exclusive_maximum_minimum": { + "default": 1, + "description": "A number variable that must be greater than 0 and less than 10", + "validation": { + "condition": "var.a_number_exclusive_maximum_minimum > 0 && var.a_number_exclusive_maximum_minimum < 10", + "error_message": "a_number_exclusive_maximum_minimum must be less than 10 and greater than 0" + }, + "type": "number" + }, + "a_number_maximum_minimum": { + "default": 0, + "description": "A number variable that must be between 0 and 10 (inclusive)", + "validation": { + "condition": "var.a_number_maximum_minimum >= 0 && var.a_number_maximum_minimum <= 10", + "error_message": "a_number_maximum_minimum must be less than or equal to 10 and greater than or equal to 0" + }, + "type": "number" + }, + "a_set_maximum_minimum_items": { + "default": [ + "a" + ], + "description": "A set variable that must have a length greater than 0 and less than 10", + "validation": { + "condition": "0 < length(var.a_set_maximum_minimum_items) && 10 > length(var.a_set_maximum_minimum_items)", + "error_message": "a_set_maximum_minimum_items must have a length greater than 0 and less than 10" + }, + "type": [ + "set", + "string" + ] + }, + "a_string_enum_escaped_characters_kind_1": { + "default": "\\", + "description": "A string variable that must some complicated escaped characters", + "validation": { + "condition": "contains([\"\\\\\", \"\\\"\", \"\\\\\\\"\", \"$${abc}\",\"\\n\",\"\\t\",\"10%\",\"10%%\",\"$a\",\"$$a\",\"\\r\",\"\\\\r\", null, \"<\", \">\", \"&\"], var.a_string_enum_escaped_characters_kind_1)", + "error_message": "Invalid value for a_string_enum_escaped_characters" + }, + "type": "string" + }, + "a_string_enum_escaped_characters_kind_2": { + "default": "\"", + "description": "A string variable that must some complicated escaped characters", + "validation": { + "condition": "var.a_string_enum_escaped_characters_kind_2 == \"\\\\\" || var.a_string_enum_escaped_characters_kind_2 == \"\\\"\" || var.a_string_enum_escaped_characters_kind_2 == \"\\\\\\\"\" || var.a_string_enum_escaped_characters_kind_2 == \"$${abc}\" || var.a_string_enum_escaped_characters_kind_2 == \"\\n\" || var.a_string_enum_escaped_characters_kind_2 == \"\\t\" || var.a_string_enum_escaped_characters_kind_2 == \"10%\" || var.a_string_enum_escaped_characters_kind_2 == \"10%%\" || var.a_string_enum_escaped_characters_kind_2 == \"$a\" || var.a_string_enum_escaped_characters_kind_2 == \"$$a\" || var.a_string_enum_escaped_characters_kind_2 == \"\\r\" || var.a_string_enum_escaped_characters_kind_2 == \"\\\\r\" || var.a_string_enum_escaped_characters_kind_2 == null || var.a_string_enum_escaped_characters_kind_2 == \"<\" || var.a_string_enum_escaped_characters_kind_2 == \">\" || var.a_string_enum_escaped_characters_kind_2 == \"&\"", + "error_message": "Invalid value for a_string_enum_escaped_characters" + }, + "type": "string" + }, + "a_string_enum_kind_1": { + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "validation": { + "condition": "contains([\"a\", \"b\", \"c\"], var.a_string_enum_kind_1)", + "error_message": "Invalid value for a_string_enum_kind_1" + }, + "type": "string" + }, + "a_string_enum_kind_2": { + "default": "a", + "description": "A string variable that must be one of the values 'a', 'b', or 'c'", + "validation": { + "condition": "var.a_string_enum_kind_2 == \"a\" || var.a_string_enum_kind_2 == \"b\" || var.a_string_enum_kind_2 == \"c\"", + "error_message": "Invalid value for a_string_enum_kind_2" + }, + "type": "string" + }, + "a_string_length_over_defined": { + "default": "a", + "description": "A string variable that must have length 4", + "validation": { + "condition": "2length(var.a_string_length_over_defined)", + "error_message": "a_string_set_length must have length 4" + }, + "type": "string" + }, + "a_string_maximum_minimum_length": { + "default": "a", + "description": "A string variable that must have a length less than 10 and greater than 0", + "validation": { + "condition": "0 0 && length(var.an_object_maximum_minimum_items) < 3", + "error_message": "an_object_maximum_minimum_items must have fewer than 3 properties" + }, + "type": [ + "object", + { + "name": "string" + } + ] + } +} diff --git a/test/expected/empty/schema-disallow-additional.json b/test/expected/empty/schema-disallow-additional.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/expected/empty/schema-disallow-additional.json @@ -0,0 +1 @@ +{} diff --git a/test/expected/empty/schema-nullable-all.json b/test/expected/empty/schema-nullable-all.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/expected/empty/schema-nullable-all.json @@ -0,0 +1 @@ +{} diff --git a/test/expected/empty/schema.json b/test/expected/empty/schema.json index 9e26dfe..0967ef4 100644 --- a/test/expected/empty/schema.json +++ b/test/expected/empty/schema.json @@ -1 +1 @@ -{} \ No newline at end of file +{} diff --git a/test/expected/empty/variables.json b/test/expected/empty/variables.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/expected/empty/variables.json @@ -0,0 +1 @@ +{} diff --git a/test/expected/simple-types/schema-disallow-additional.json b/test/expected/simple-types/schema-disallow-additional.json new file mode 100644 index 0000000..43943ee --- /dev/null +++ b/test/expected/simple-types/schema-disallow-additional.json @@ -0,0 +1,128 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": false, + "properties": { + "a_bool": { + "default": false, + "description": "This is a boolean", + "type": "boolean" + }, + "a_list": { + "default": [ + "a", + "b", + "c" + ], + "description": "This is a list of strings", + "items": { + "type": "string" + }, + "type": "array" + }, + "a_map_of_strings": { + "additionalProperties": { + "type": "string" + }, + "default": { + "a": "a", + "b": "b", + "c": "c" + }, + "description": "This is a map of strings", + "type": "object" + }, + "a_nullable_string": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "description": "This is a nullable string", + "title": "a_nullable_string: Select a type" + }, + "a_number": { + "description": "This is a number", + "type": "number" + }, + "a_set": { + "default": [ + "a", + "b", + "c" + ], + "description": "This is a set of strings", + "items": { + "type": "string" + }, + "type": "array", + "uniqueItems": true + }, + "a_string": { + "default": "a string", + "description": "This is a string", + "type": "string" + }, + "a_tuple": { + "default": [ + "a", + 1, + true + ], + "description": "This is a tuple", + "items": [ + { + "type": "string" + }, + { + "type": "number" + }, + { + "type": "boolean" + } + ], + "maxItems": 3, + "minItems": 3, + "type": "array" + }, + "a_variable_in_another_file": { + "default": "", + "description": "a string", + "type": "string" + }, + "an_object": { + "additionalProperties": false, + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object", + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + }, + "c": { + "type": "boolean" + } + }, + "required": [ + "a", + "b", + "c" + ], + "type": "object" + } + }, + "required": [ + "a_nullable_string", + "a_number" + ] +} diff --git a/test/expected/simple-types/schema-nullable-all.json b/test/expected/simple-types/schema-nullable-all.json new file mode 100644 index 0000000..9e5947d --- /dev/null +++ b/test/expected/simple-types/schema-nullable-all.json @@ -0,0 +1,218 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "a_bool": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "boolean", + "type": "boolean" + } + ], + "default": false, + "description": "This is a boolean", + "title": "a_bool: Select a type" + }, + "a_list": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "items": { + "type": "string" + }, + "title": "array", + "type": "array" + } + ], + "default": [ + "a", + "b", + "c" + ], + "description": "This is a list of strings", + "title": "a_list: Select a type" + }, + "a_map_of_strings": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "additionalProperties": { + "type": "string" + }, + "title": "object", + "type": "object" + } + ], + "default": { + "a": "a", + "b": "b", + "c": "c" + }, + "description": "This is a map of strings", + "title": "a_map_of_strings: Select a type" + }, + "a_nullable_string": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "description": "This is a nullable string", + "title": "a_nullable_string: Select a type" + }, + "a_number": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "number", + "type": "number" + } + ], + "description": "This is a number", + "title": "a_number: Select a type" + }, + "a_set": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "items": { + "type": "string" + }, + "title": "array", + "type": "array", + "uniqueItems": true + } + ], + "default": [ + "a", + "b", + "c" + ], + "description": "This is a set of strings", + "title": "a_set: Select a type" + }, + "a_string": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "a string", + "description": "This is a string", + "title": "a_string: Select a type" + }, + "a_tuple": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "items": [ + { + "type": "string" + }, + { + "type": "number" + }, + { + "type": "boolean" + } + ], + "maxItems": 3, + "minItems": 3, + "title": "array", + "type": "array" + } + ], + "default": [ + "a", + 1, + true + ], + "description": "This is a tuple", + "title": "a_tuple: Select a type" + }, + "a_variable_in_another_file": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "", + "description": "a string", + "title": "a_variable_in_another_file: Select a type" + }, + "an_object": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "additionalProperties": true, + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + }, + "c": { + "type": "boolean" + } + }, + "required": [ + "a", + "b", + "c" + ], + "title": "object", + "type": "object" + } + ], + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object", + "title": "an_object: Select a type" + } + }, + "required": [ + "a_nullable_string", + "a_number" + ] +} diff --git a/test/expected/simple-types/schema.json b/test/expected/simple-types/schema.json index b35d72d..78d5d68 100644 --- a/test/expected/simple-types/schema.json +++ b/test/expected/simple-types/schema.json @@ -1,128 +1,128 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "additionalProperties": true, - "properties": { - "a_bool": { - "default": false, - "description": "This is a boolean", - "type": "boolean" - }, - "a_list": { - "default": [ - "a", - "b", - "c" - ], - "description": "This is a list of strings", - "items": { - "type": "string" - }, - "type": "array" - }, - "a_map_of_strings": { - "additionalProperties": { - "type": "string" - }, - "default": { - "a": "a", - "b": "b", - "c": "c" - }, - "description": "This is a map of strings", - "type": "object" - }, - "a_nullable_string": { - "anyOf": [ - { - "title": "null", - "type": "null" - }, - { - "title": "string", - "type": "string" - } - ], - "description": "This is a nullable string", - "title": "a_nullable_string: Select a type" - }, - "a_number": { - "description": "This is a number", - "type": "number" - }, - "a_set": { - "default": [ - "a", - "b", - "c" - ], - "description": "This is a set of strings", - "items": { - "type": "string" - }, - "type": "array", - "uniqueItems": true - }, - "a_string": { - "default": "a string", - "description": "This is a string", - "type": "string" - }, - "a_tuple": { - "default": [ - "a", - 1, - true - ], - "description": "This is a tuple", - "items": [ - { - "type": "string" - }, - { - "type": "number" - }, - { - "type": "boolean" - } - ], - "maxItems": 3, - "minItems": 3, - "type": "array" - }, - "a_variable_in_another_file": { - "default": "", - "description": "a string", - "type": "string" - }, - "an_object": { - "additionalProperties": true, - "default": { - "a": "a", - "b": 1, - "c": true - }, - "description": "This is an object", - "properties": { - "a": { - "type": "string" - }, - "b": { - "type": "number" - }, - "c": { - "type": "boolean" - } - }, - "required": [ - "a", - "b", - "c" - ], - "type": "object" - } - }, - "required": [ - "a_nullable_string", - "a_number" - ] + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "a_bool": { + "default": false, + "description": "This is a boolean", + "type": "boolean" + }, + "a_list": { + "default": [ + "a", + "b", + "c" + ], + "description": "This is a list of strings", + "items": { + "type": "string" + }, + "type": "array" + }, + "a_map_of_strings": { + "additionalProperties": { + "type": "string" + }, + "default": { + "a": "a", + "b": "b", + "c": "c" + }, + "description": "This is a map of strings", + "type": "object" + }, + "a_nullable_string": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "description": "This is a nullable string", + "title": "a_nullable_string: Select a type" + }, + "a_number": { + "description": "This is a number", + "type": "number" + }, + "a_set": { + "default": [ + "a", + "b", + "c" + ], + "description": "This is a set of strings", + "items": { + "type": "string" + }, + "type": "array", + "uniqueItems": true + }, + "a_string": { + "default": "a string", + "description": "This is a string", + "type": "string" + }, + "a_tuple": { + "default": [ + "a", + 1, + true + ], + "description": "This is a tuple", + "items": [ + { + "type": "string" + }, + { + "type": "number" + }, + { + "type": "boolean" + } + ], + "maxItems": 3, + "minItems": 3, + "type": "array" + }, + "a_variable_in_another_file": { + "default": "", + "description": "a string", + "type": "string" + }, + "an_object": { + "additionalProperties": true, + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object", + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + }, + "c": { + "type": "boolean" + } + }, + "required": [ + "a", + "b", + "c" + ], + "type": "object" + } + }, + "required": [ + "a_nullable_string", + "a_number" + ] } diff --git a/test/expected/simple-types/variables.json b/test/expected/simple-types/variables.json new file mode 100644 index 0000000..138ad41 --- /dev/null +++ b/test/expected/simple-types/variables.json @@ -0,0 +1,96 @@ +{ + "a_bool": { + "default": false, + "description": "This is a boolean", + "type": "bool" + }, + "a_list": { + "default": [ + "a", + "b", + "c" + ], + "description": "This is a list of strings", + "type": [ + "list", + "string" + ] + }, + "a_map_of_strings": { + "default": { + "a": "a", + "b": "b", + "c": "c" + }, + "description": "This is a map of strings", + "type": [ + "map", + "string" + ] + }, + "a_nullable_string": { + "default": null, + "description": "This is a nullable string", + "nullable": true, + "type": "string" + }, + "a_number": { + "default": null, + "description": "This is a number", + "type": "number" + }, + "a_set": { + "default": [ + "a", + "b", + "c" + ], + "description": "This is a set of strings", + "type": [ + "set", + "string" + ] + }, + "a_string": { + "default": "a string", + "description": "This is a string", + "type": "string" + }, + "a_tuple": { + "default": [ + "a", + 1, + true + ], + "description": "This is a tuple", + "type": [ + "tuple", + [ + "string", + "number", + "bool" + ] + ] + }, + "a_variable_in_another_file": { + "default": "", + "description": "a string", + "type": "string" + }, + "an_object": { + "default": { + "a": "a", + "b": 1, + "c": true + }, + "description": "This is an object", + "type": [ + "object", + { + "a": "string", + "b": "number", + "c": "bool" + } + ] + } +} diff --git a/test/expected/simple/schema-disallow-additional.json b/test/expected/simple/schema-disallow-additional.json new file mode 100644 index 0000000..6a77a6b --- /dev/null +++ b/test/expected/simple/schema-disallow-additional.json @@ -0,0 +1,18 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": false, + "properties": { + "age": { + "description": "Your age. Required.", + "type": "number" + }, + "name": { + "default": "world", + "description": "Your name.", + "type": "string" + } + }, + "required": [ + "age" + ] +} diff --git a/test/expected/simple/schema-nullable-all.json b/test/expected/simple/schema-nullable-all.json new file mode 100644 index 0000000..ac27a8a --- /dev/null +++ b/test/expected/simple/schema-nullable-all.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "age": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "number", + "type": "number" + } + ], + "description": "Your age. Required.", + "title": "age: Select a type" + }, + "name": { + "anyOf": [ + { + "title": "null", + "type": "null" + }, + { + "title": "string", + "type": "string" + } + ], + "default": "world", + "description": "Your name.", + "title": "name: Select a type" + } + }, + "required": [ + "age" + ] +} diff --git a/test/expected/simple/schema.json b/test/expected/simple/schema.json index ba3ff62..74ab04a 100644 --- a/test/expected/simple/schema.json +++ b/test/expected/simple/schema.json @@ -1,18 +1,18 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "additionalProperties": true, - "properties": { - "age": { - "description": "Your age. Required.", - "type": "number" - }, - "name": { - "default": "world", - "description": "Your name.", - "type": "string" - } - }, - "required": [ - "age" - ] + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": true, + "properties": { + "age": { + "description": "Your age. Required.", + "type": "number" + }, + "name": { + "default": "world", + "description": "Your name.", + "type": "string" + } + }, + "required": [ + "age" + ] } diff --git a/test/expected/simple/variables.json b/test/expected/simple/variables.json new file mode 100644 index 0000000..9d757c9 --- /dev/null +++ b/test/expected/simple/variables.json @@ -0,0 +1,12 @@ +{ + "age": { + "default": null, + "description": "Your age. Required.", + "type": "number" + }, + "name": { + "default": "world", + "description": "Your name.", + "type": "string" + } +} diff --git a/test/modules/custom-validation/variables.tf b/test/modules/custom-validation/variables.tf index 52ddf7a..4e1b828 100644 --- a/test/modules/custom-validation/variables.tf +++ b/test/modules/custom-validation/variables.tf @@ -163,7 +163,7 @@ variable "a_string_enum_escaped_characters_kind_1" { type = string description = "A string variable that must some complicated escaped characters" validation { - condition = contains(["\\", "\"", "\\\"", "$${abc}","\n","\t","10%","10%%","$a","$$a","\r","\\r"], var.a_string_enum_escaped_characters_kind_1) + condition = contains(["\\", "\"", "\\\"", "$${abc}","\n","\t","10%","10%%","$a","$$a","\r","\\r", null, "<", ">", "&"], var.a_string_enum_escaped_characters_kind_1) error_message = "Invalid value for a_string_enum_escaped_characters" } default = "\\" @@ -173,7 +173,7 @@ variable "a_string_enum_escaped_characters_kind_2" { type = string description = "A string variable that must some complicated escaped characters" validation { - condition = var.a_string_enum_escaped_characters_kind_2 == "\\" || var.a_string_enum_escaped_characters_kind_2 == "\"" || var.a_string_enum_escaped_characters_kind_2 == "\\\"" || var.a_string_enum_escaped_characters_kind_2 == "$${abc}" || var.a_string_enum_escaped_characters_kind_2 == "\n" || var.a_string_enum_escaped_characters_kind_2 == "\t" || var.a_string_enum_escaped_characters_kind_2 == "10%" || var.a_string_enum_escaped_characters_kind_2 == "10%%" || var.a_string_enum_escaped_characters_kind_2 == "$a" || var.a_string_enum_escaped_characters_kind_2 == "$$a" || var.a_string_enum_escaped_characters_kind_2 == "\r" || var.a_string_enum_escaped_characters_kind_2 == "\\r" + condition = var.a_string_enum_escaped_characters_kind_2 == "\\" || var.a_string_enum_escaped_characters_kind_2 == "\"" || var.a_string_enum_escaped_characters_kind_2 == "\\\"" || var.a_string_enum_escaped_characters_kind_2 == "$${abc}" || var.a_string_enum_escaped_characters_kind_2 == "\n" || var.a_string_enum_escaped_characters_kind_2 == "\t" || var.a_string_enum_escaped_characters_kind_2 == "10%" || var.a_string_enum_escaped_characters_kind_2 == "10%%" || var.a_string_enum_escaped_characters_kind_2 == "$a" || var.a_string_enum_escaped_characters_kind_2 == "$$a" || var.a_string_enum_escaped_characters_kind_2 == "\r" || var.a_string_enum_escaped_characters_kind_2 == "\\r" || var.a_string_enum_escaped_characters_kind_2 == null || var.a_string_enum_escaped_characters_kind_2 == "<" || var.a_string_enum_escaped_characters_kind_2 == ">" || var.a_string_enum_escaped_characters_kind_2 == "&" error_message = "Invalid value for a_string_enum_escaped_characters" } default = "\""