Skip to content

Commit

Permalink
do not include named validation in openapi if when= is present
Browse files Browse the repository at this point in the history
when= introduces more validation dynamicism which is not possible to represent via openapi
  • Loading branch information
Dmitriy Kalinin committed Apr 13, 2024
1 parent 7dd754a commit 8467f36
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/cmd/template/schema_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions pkg/validations/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (a *validationRun) VisitWithParent(value yamlmeta.Node, parent yamlmeta.Nod
}

func (v NodeValidation) HasSimpleMinLength() (int64, bool) { // TODO check when

Check warning on line 111 in pkg/validations/validate.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method NodeValidation.HasSimpleMinLength should have comment or be unexported (revive)
if v.kwargs.when != nil {
return 0, false
}
if v.kwargs.minLength != nil {
value, ok := v.kwargs.minLength.Int64()
if ok {
Expand All @@ -119,6 +122,9 @@ func (v NodeValidation) HasSimpleMinLength() (int64, bool) { // TODO check when
}

func (v NodeValidation) HasSimpleMaxLength() (int64, bool) {

Check warning on line 124 in pkg/validations/validate.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method NodeValidation.HasSimpleMaxLength should have comment or be unexported (revive)
if v.kwargs.when != nil {
return 0, false
}
if v.kwargs.maxLength != nil {
value, ok := v.kwargs.maxLength.Int64()
if ok {
Expand All @@ -129,6 +135,9 @@ func (v NodeValidation) HasSimpleMaxLength() (int64, bool) {
}

func (v NodeValidation) HasSimpleMin() (interface{}, bool) {

Check warning on line 137 in pkg/validations/validate.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method NodeValidation.HasSimpleMin should have comment or be unexported (revive)
if v.kwargs.when != nil {
return nil, false
}
if v.kwargs.min != nil {
value, err := core.NewStarlarkValue(v.kwargs.min).AsGoValue()
if err == nil {
Expand All @@ -139,6 +148,9 @@ func (v NodeValidation) HasSimpleMin() (interface{}, bool) {
}

func (v NodeValidation) HasSimpleMax() (interface{}, bool) {

Check warning on line 150 in pkg/validations/validate.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method NodeValidation.HasSimpleMax should have comment or be unexported (revive)
if v.kwargs.when != nil {
return nil, false
}
if v.kwargs.max != nil {
value, err := core.NewStarlarkValue(v.kwargs.max).AsGoValue()
if err == nil {
Expand All @@ -149,6 +161,9 @@ func (v NodeValidation) HasSimpleMax() (interface{}, bool) {
}

func (v NodeValidation) HasSimpleOneOf() ([]interface{}, bool) {

Check warning on line 163 in pkg/validations/validate.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method NodeValidation.HasSimpleOneOf should have comment or be unexported (revive)
if v.kwargs.when != nil {
return nil, false
}
if v.kwargs.oneOf != nil {
enum := []interface{}{}
iter := starlark.Iterate(v.kwargs.oneOf)
Expand Down

0 comments on commit 8467f36

Please sign in to comment.