From eacb7bceae6de023f02996b11a9d47457b94ac79 Mon Sep 17 00:00:00 2001 From: Dmitriy Kalinin Date: Sat, 13 Apr 2024 08:40:38 -0400 Subject: [PATCH] do not include named validation in openapi if when= is present when= introduces more validation dynamicism which is not possible to represent via openapi Signed-off-by: Dmitriy Kalinin --- pkg/cmd/template/schema_inspect_test.go | 63 +++++++++++++++++++++++++ pkg/schema/type.go | 1 - pkg/validations/validate.go | 27 ++++++++++- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/template/schema_inspect_test.go b/pkg/cmd/template/schema_inspect_test.go index 6a6d5cad..900cc7fd 100644 --- a/pkg/cmd/template/schema_inspect_test.go +++ b/pkg/cmd/template/schema_inspect_test.go @@ -647,6 +647,69 @@ components: 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{"openapi-v3"} + + 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 := `openapi: 3.0.0 +info: + version: 0.1.0 + title: Schema for data values, generated by ytt +paths: {} +components: + schemas: + dataValues: + 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 TestSchemaInspect_annotation_adds_key(t *testing.T) { t.Run("in the correct relative order", func(t *testing.T) { diff --git a/pkg/schema/type.go b/pkg/schema/type.go index 31585715..4ef273cf 100644 --- a/pkg/schema/type.go +++ b/pkg/schema/type.go @@ -83,7 +83,6 @@ type ScalarType struct { Position *filepos.Position defaultValue interface{} documentation documentation - validations map[string]interface{} } type AnyType struct { diff --git a/pkg/validations/validate.go b/pkg/validations/validate.go index 37503207..45d2c759 100644 --- a/pkg/validations/validate.go +++ b/pkg/validations/validate.go @@ -108,7 +108,12 @@ func (a *validationRun) VisitWithParent(value yamlmeta.Node, parent yamlmeta.Nod return nil } -func (v NodeValidation) HasSimpleMinLength() (int64, bool) { // TODO check when +// HasSimpleMinLength indicates presence of min length validation and its associated value. +// Returns false if validation is conditional (via when=). +func (v NodeValidation) HasSimpleMinLength() (int64, bool) { + if v.kwargs.when != nil { + return 0, false + } if v.kwargs.minLength != nil { value, ok := v.kwargs.minLength.Int64() if ok { @@ -118,7 +123,12 @@ func (v NodeValidation) HasSimpleMinLength() (int64, bool) { // TODO check when return 0, false } +// HasSimpleMaxLength indicates presence of max length validation and its associated value. +// Returns false if validation is conditional (via when=). func (v NodeValidation) HasSimpleMaxLength() (int64, bool) { + if v.kwargs.when != nil { + return 0, false + } if v.kwargs.maxLength != nil { value, ok := v.kwargs.maxLength.Int64() if ok { @@ -128,7 +138,12 @@ func (v NodeValidation) HasSimpleMaxLength() (int64, bool) { return 0, false } +// HasSimpleMin indicates presence of min validation and its associated value. +// Returns false if validation is conditional (via when=). func (v NodeValidation) HasSimpleMin() (interface{}, bool) { + if v.kwargs.when != nil { + return nil, false + } if v.kwargs.min != nil { value, err := core.NewStarlarkValue(v.kwargs.min).AsGoValue() if err == nil { @@ -138,7 +153,12 @@ func (v NodeValidation) HasSimpleMin() (interface{}, bool) { return nil, false } +// HasSimpleMax indicates presence of max validation and its associated value. +// Returns false if validation is conditional (via when=). func (v NodeValidation) HasSimpleMax() (interface{}, bool) { + if v.kwargs.when != nil { + return nil, false + } if v.kwargs.max != nil { value, err := core.NewStarlarkValue(v.kwargs.max).AsGoValue() if err == nil { @@ -148,7 +168,12 @@ func (v NodeValidation) HasSimpleMax() (interface{}, bool) { return nil, false } +// HasSimpleOneOf indicates presence of one-of validation and its allowed values. +// Returns false if validation is conditional (via when=). func (v NodeValidation) HasSimpleOneOf() ([]interface{}, bool) { + if v.kwargs.when != nil { + return nil, false + } if v.kwargs.oneOf != nil { enum := []interface{}{} iter := starlark.Iterate(v.kwargs.oneOf)