Skip to content

Commit 5560763

Browse files
authored
Add tests for the other kinds of schema (#22)
This tests to make sure that the `additionalProperties: false` and `--nullable-all` schemas validate different sets of values to to the ones that are validated by the default `schema.json`. In particular, `schema-nullable-all.json` should (unless disallowed by certain fields such as `enum`) all a file with all variables defined as `null`. `schema-disallow-additional.json` should disallow additional properties, including within nested objects.
1 parent 3995cff commit 5560763

File tree

6 files changed

+139
-5
lines changed

6 files changed

+139
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ running Terraform.
7474
- `--stdout`: Print schema to stdout and prevent all other logging unless an error occurs. Does not create a file.
7575
Overrides `--debug` and `--output`.
7676

77-
- `--export-variables`: Export the variables in JSON format directly and do not create a JSON Schema. This provides similar functionality to applications such as terraform-docs, where the input variables can be output to a machine-readable format such as JSON. The `type` field is converted to a type constraint based on the type definition, and the `default` field is translated to its literal value. `condition` inside the `validation` block is left as a string, because it is difficult to represent arbitrary (ie unevaluated) hcl Expressions in JSON.
77+
- `--export-variables`: Export the variables in JSON format directly and do not create a JSON Schema. This provides similar functionality to applications such as terraform-docs, where the input variables can be output to a machine-readable format such as JSON. The `type` field is converted to a type constraint based on the type definition, and the `default` field is translated to its literal value. `condition` inside the `validation` block is left as a string, because it is difficult to represent arbitrary (ie unevaluated) HCL Expressions in JSON.
7878

7979
- `--escape-json`: Escape special characters in the JSON (`<`,`>` and `&`) so that the schema can be used in a web context. By default, this behaviour is disabled so the JSON file can be read more easily, though it does not effect external programs such as `jq`.
8080

pkg/jsonschema/json-schema_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func getErrorLocationsFromValidationErr(t *testing.T, valErr *jsonschema.Validat
8686
return keywordLocations
8787
}
8888

89+
//nolint:funlen,maintidx
8990
func TestSampleInput(t *testing.T) {
9091
t.Parallel()
9192

@@ -95,6 +96,7 @@ func TestSampleInput(t *testing.T) {
9596
filePath string
9697
keywordLocations []errorLocation
9798
}{
99+
// Minimum input
98100
{
99101
name: "empty minimum input",
100102
filePath: "../../test/expected/empty/sample-input/test-input-min.json",
@@ -125,6 +127,7 @@ func TestSampleInput(t *testing.T) {
125127
schemaPath: "../../test/expected/custom-validation/schema.json",
126128
keywordLocations: nil,
127129
},
130+
// Maximum input plus an unknown variable, additionalProperties is true
128131
{
129132
name: "simple full input",
130133
filePath: "../../test/expected/simple/sample-input/test-input-all.json",
@@ -149,6 +152,41 @@ func TestSampleInput(t *testing.T) {
149152
schemaPath: "../../test/expected/custom-validation/schema.json",
150153
keywordLocations: nil,
151154
},
155+
// Maximum input plus an unknown variable, additionalProperties is false
156+
{
157+
name: "simple full input additionalProperties false",
158+
filePath: "../../test/expected/simple/sample-input/test-input-all.json",
159+
schemaPath: "../../test/expected/simple/schema-disallow-additional.json",
160+
keywordLocations: []errorLocation{
161+
{name: "/additionalProperties"},
162+
},
163+
},
164+
{
165+
name: "simple-types full input additionalProperties false",
166+
filePath: "../../test/expected/simple-types/sample-input/test-input-all.json",
167+
schemaPath: "../../test/expected/simple-types/schema-disallow-additional.json",
168+
keywordLocations: []errorLocation{
169+
{name: "/additionalProperties"},
170+
},
171+
},
172+
{
173+
name: "complex-types full input additionalProperties false",
174+
filePath: "../../test/expected/complex-types/sample-input/test-input-all.json",
175+
schemaPath: "../../test/expected/complex-types/schema-disallow-additional.json",
176+
keywordLocations: []errorLocation{
177+
{name: "/additionalProperties"},
178+
},
179+
},
180+
{
181+
name: "custom-validation full input additionalProperties false",
182+
filePath: "../../test/expected/custom-validation/sample-input/test-input-all.json",
183+
schemaPath: "../../test/expected/custom-validation/schema-disallow-additional.json",
184+
keywordLocations: []errorLocation{
185+
{name: "/additionalProperties"},
186+
{name: "/properties/an_object_maximum_minimum_items/additionalProperties"},
187+
},
188+
},
189+
// bad input on all fields
152190
{
153191
name: "simple bad input",
154192
filePath: "../../test/expected/simple/sample-input/test-input-bad.json",
@@ -271,6 +309,98 @@ func TestSampleInput(t *testing.T) {
271309
},
272310
},
273311
},
312+
// null input on all fields, nullableAll is false
313+
{
314+
name: "simple null input nullableAll false",
315+
filePath: "../../test/expected/simple/sample-input/test-input-null.json",
316+
schemaPath: "../../test/expected/simple/schema.json",
317+
keywordLocations: []errorLocation{
318+
{name: "/properties/age/type"},
319+
{name: "/properties/name/type"},
320+
},
321+
},
322+
{
323+
name: "simple-types null input nullableAll false",
324+
filePath: "../../test/expected/simple-types/sample-input/test-input-null.json",
325+
schemaPath: "../../test/expected/simple-types/schema.json",
326+
keywordLocations: []errorLocation{
327+
{name: "/properties/a_bool/type"},
328+
{name: "/properties/a_list/type"},
329+
{name: "/properties/a_map_of_strings/type"},
330+
{name: "/properties/a_number/type"},
331+
{name: "/properties/a_set/type"},
332+
{name: "/properties/a_string/type"},
333+
{name: "/properties/a_tuple/type"},
334+
{name: "/properties/a_variable_in_another_file/type"},
335+
{name: "/properties/an_object/type"},
336+
},
337+
},
338+
{
339+
name: "complex-types null input nullableAll false",
340+
filePath: "../../test/expected/complex-types/sample-input/test-input-null.json",
341+
schemaPath: "../../test/expected/complex-types/schema.json",
342+
keywordLocations: []errorLocation{
343+
{name: "/properties/a_very_complicated_object/type"},
344+
{name: "/properties/an_object_with_optional/type"},
345+
},
346+
},
347+
{
348+
name: "custom-validation null input nullableAll false",
349+
filePath: "../../test/expected/custom-validation/sample-input/test-input-null.json",
350+
schemaPath: "../../test/expected/custom-validation/schema.json",
351+
keywordLocations: []errorLocation{
352+
{name: "/properties/a_list_maximum_minimum_length/type"},
353+
{name: "/properties/a_map_maximum_minimum_entries/type"},
354+
{name: "/properties/a_number_enum_kind_1/type"},
355+
{name: "/properties/a_number_enum_kind_2/type"},
356+
{name: "/properties/a_number_exclusive_maximum_minimum/type"},
357+
{name: "/properties/a_number_maximum_minimum/type"},
358+
{name: "/properties/a_set_maximum_minimum_items/type"},
359+
{name: "/properties/a_string_enum_escaped_characters_kind_1/type"},
360+
{name: "/properties/a_string_enum_escaped_characters_kind_2/type"},
361+
{name: "/properties/a_string_enum_kind_1/type"},
362+
{name: "/properties/a_string_enum_kind_2/type"},
363+
{name: "/properties/a_string_length_over_defined/type"},
364+
{name: "/properties/a_string_maximum_minimum_length/type"},
365+
{name: "/properties/a_string_pattern_1/type"},
366+
{name: "/properties/a_string_pattern_2/type"},
367+
{name: "/properties/a_string_set_length/type"},
368+
{name: "/properties/an_object_maximum_minimum_items/type"},
369+
},
370+
},
371+
// null input on all fields, nullableAll is true
372+
{
373+
name: "simple null input nullableAll true",
374+
filePath: "../../test/expected/simple/sample-input/test-input-null.json",
375+
schemaPath: "../../test/expected/simple/schema-nullable-all.json",
376+
keywordLocations: nil,
377+
},
378+
{
379+
name: "simple-types null input nullableAll true",
380+
filePath: "../../test/expected/simple-types/sample-input/test-input-null.json",
381+
schemaPath: "../../test/expected/simple-types/schema-nullable-all.json",
382+
keywordLocations: nil,
383+
},
384+
{
385+
name: "complex-types null input nullableAll true",
386+
filePath: "../../test/expected/complex-types/sample-input/test-input-null.json",
387+
schemaPath: "../../test/expected/complex-types/schema-nullable-all.json",
388+
keywordLocations: nil,
389+
},
390+
{
391+
// of note: custom validation still applies to nullable fields, and sometimes 'null' doesn't satisfy the
392+
// condition, meaning these fields effectively can't be null.
393+
// This seems to primarily be the case for "enum" fields. Other fields tend to ignore this error, in JSON Schema.
394+
name: "custom-validation null input nullableAll true",
395+
filePath: "../../test/expected/custom-validation/sample-input/test-input-null.json",
396+
schemaPath: "../../test/expected/custom-validation/schema-nullable-all.json",
397+
keywordLocations: []errorLocation{
398+
{name: "/properties/a_number_enum_kind_1/enum"},
399+
{name: "/properties/a_number_enum_kind_2/enum"},
400+
{name: "/properties/a_string_enum_kind_1/enum"},
401+
{name: "/properties/a_string_enum_kind_2/enum"},
402+
},
403+
},
274404
}
275405
for i := range testCases {
276406
tc := testCases[i]

test/expected/complex-types/sample-input/test-input-all.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353
"b": 2,
5454
"c": false,
5555
"d": "Optional string"
56-
}
56+
},
57+
"something_else": "string"
5758
}

test/expected/custom-validation/sample-input/test-input-all.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"an_object_maximum_minimum_items": {
2323
"name": "a",
2424
"other_name": "this is an additional property, terraform will ignore it"
25-
}
25+
},
26+
"something_else": "string"
2627
}

test/expected/simple-types/sample-input/test-input-all.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
"a": "d",
3030
"b": 2,
3131
"c": false
32-
}
32+
},
33+
"something_else": "string"
3334
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "../schema.json",
33
"name": "Aisling",
4-
"age": 24
4+
"age": 24,
5+
"something_else": "string"
56
}

0 commit comments

Comments
 (0)