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/validations/validate.go b/pkg/validations/validate.go index 37503207..6ae36e84 100644 --- a/pkg/validations/validate.go +++ b/pkg/validations/validate.go @@ -109,6 +109,9 @@ func (a *validationRun) VisitWithParent(value yamlmeta.Node, parent yamlmeta.Nod } func (v NodeValidation) HasSimpleMinLength() (int64, bool) { // TODO check when + if v.kwargs.when != nil { + return 0, false + } if v.kwargs.minLength != nil { value, ok := v.kwargs.minLength.Int64() if ok { @@ -119,6 +122,9 @@ func (v NodeValidation) HasSimpleMinLength() (int64, bool) { // TODO check 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 { @@ -129,6 +135,9 @@ func (v NodeValidation) HasSimpleMaxLength() (int64, bool) { } 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 { @@ -139,6 +148,9 @@ func (v NodeValidation) HasSimpleMin() (interface{}, bool) { } 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 { @@ -149,6 +161,9 @@ func (v NodeValidation) HasSimpleMax() (interface{}, bool) { } 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)