From a29ae1f69f54d44354418fbfcd8cf8920d2c0bbb Mon Sep 17 00:00:00 2001 From: Mathias Petermann Date: Sat, 20 Apr 2024 11:34:08 +0200 Subject: [PATCH] Add test excluding named validations when when= is present for json schema Signed-off-by: Mathias Petermann --- pkg/cmd/template/schema_inspect_test.go | 106 ++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/pkg/cmd/template/schema_inspect_test.go b/pkg/cmd/template/schema_inspect_test.go index 7f08e5ba..d5d3374e 100644 --- a/pkg/cmd/template/schema_inspect_test.go +++ b/pkg/cmd/template/schema_inspect_test.go @@ -1703,6 +1703,9 @@ foo: #@schema/validation min=0, max=100 range_key: 0 + #@schema/validation min=-1.1, max=100.1 + range_float_key: 2.2 + #@schema/default 10 #@schema/validation min=0 min_key: 0 @@ -1714,11 +1717,22 @@ foo: #@schema/validation min_len=1, max_len=10 string_key: "" + #@schema/validation min_len=3, max_len=4 + array_key: + - "" + + #@schema/validation min_len=2, max_len=5 + map_key: {} + #@schema/validation one_of=[1,2,3] one_of_integers: 1 #@schema/validation one_of=["one", "two", "three"] one_of_strings: "one" + + #@schema/type any=True + #@schema/validation one_of=["one", 2, 3.3, {}] + one_of_mixed: "one" ` expected := `$schema: https://json-schema.org/draft/2020-12/schema $id: https://example.biz/schema/ytt/data-values.json @@ -1735,6 +1749,12 @@ properties: default: 10 minimum: 0 maximum: 100 + range_float_key: + type: number + format: float + default: 2.2 + minimum: -1.1 + maximum: 100.1 min_key: type: integer default: 10 @@ -1748,6 +1768,20 @@ properties: default: "" minLength: 1 maxLength: 10 + array_key: + type: array + items: + type: string + default: "" + default: [] + minItems: 3 + maxItems: 4 + map_key: + type: object + additionalProperties: false + properties: {} + minProperties: 2 + maxProperties: 5 one_of_integers: type: integer default: 1 @@ -1762,6 +1796,20 @@ properties: - one - two - three + one_of_mixed: + type: + - "null" + - string + - number + - object + - array + - boolean + default: one + enum: + - one + - 2 + - 3.3 + - {} ` filesToProcess := files.NewSortedFiles([]*files.File{ @@ -1771,6 +1819,64 @@ properties: assertSucceedsDocSet(t, filesToProcess, expected, opts) }) + t.Run("not including named validations when when= is present", func(t *testing.T) { + opts := cmdtpl.NewOptions() + opts.DataValuesFlags.InspectSchema = true + opts.RegularFilesSourceOpts.OutputType.Types = []string{"json-schema"} + + schemaYAML := `#@data/values-schema +--- +foo: + #@schema/validation min=0, max=100, when=lambda: False + range_key: 0 + + #@schema/default 10 + #@schema/validation min=0, when=lambda: False + min_key: 0 + + #@schema/default 10 + #@schema/validation max=100, when=lambda: False + max_key: 0 + + #@schema/validation min_len=1, max_len=10, when=lambda: False + string_key: "" + + #@schema/validation one_of=[1,2,3], when=lambda: False + one_of_integers: 1 +` + expected := `$schema: https://json-schema.org/draft/2020-12/schema +$id: https://example.biz/schema/ytt/data-values.json +description: Schema for data values, generated by ytt +type: object +additionalProperties: false +properties: + foo: + type: object + additionalProperties: false + properties: + range_key: + type: integer + default: 0 + min_key: + type: integer + default: 10 + max_key: + type: integer + default: 10 + string_key: + type: string + default: "" + one_of_integers: + type: integer + default: 1 +` + + filesToProcess := files.NewSortedFiles([]*files.File{ + files.MustNewFileFromSource(files.NewBytesSource("schema.yml", []byte(schemaYAML))), + }) + + assertSucceedsDocSet(t, filesToProcess, expected, opts) + }) } func assertSucceedsDocSet(t *testing.T, filesToProcess []*files.File, expectedOut string, opts *cmdtpl.Options) {