Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add make test-data #20

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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