Skip to content

Commit

Permalink
Add make test-data
Browse files Browse the repository at this point in the history
This will be used for creating more unit tests, and allows for automatically regenerating schema files when code changes.
  • Loading branch information
AislingHPE committed Aug 30, 2024
1 parent 8eb198f commit e3cbc6c
Show file tree
Hide file tree
Showing 26 changed files with 2,269 additions and 502 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/expected/*/*.json linguist-generated=true
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ terraschema:

.PHONY: clean
clean:
@rm -f terraschema
@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
7 changes: 7 additions & 0 deletions pkg/jsonschema/json-schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/jsonschema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/jsonschema/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
170 changes: 170 additions & 0 deletions test/expected/complex-types/schema-disallow-additional.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3cbc6c

Please sign in to comment.