Skip to content

Commit

Permalink
Remove unnecessary when_null_skip= keyword argument
Browse files Browse the repository at this point in the history
Resolves #732
  • Loading branch information
jtigger committed Sep 7, 2022
1 parent a787f84 commit 3a85263
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 65 deletions.
6 changes: 1 addition & 5 deletions pkg/cmd/template/schema_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ foo:
assertFailsWithSchemaAndDataValues(t, schemaYAML, valuesYAML, expectedErrMsg)
})

t.Run("when @schema/nullable, skips if value is null (unless either not_null=True or when_null_skip=False)", func(t *testing.T) {
t.Run("when @schema/nullable, skips if value is null (unless not_null=True)", func(t *testing.T) {
schemaYAML := `#@data/values-schema
---
#@schema/nullable
Expand All @@ -1555,15 +1555,11 @@ foo: 0
#@schema/nullable
#@schema/validation ("bar > 2", lambda v: True if v == None else v > 2), not_null=True
bar: 0
#@schema/nullable
#@schema/validation ("qux > 2", lambda v: v > 2), when_null_skip=False
qux: 0
`
valuesYAML := ``

expectedErrMsg := `One or more data values were invalid:
- "bar" (schema.yaml:8) requires "not null"; fail: value is null (by schema.yaml:7)
- "qux" (schema.yaml:11) requires "qux > 2"; NoneType > int not implemented (by schema.yaml:10)
`
assertFailsWithSchemaAndDataValues(t, schemaYAML, valuesYAML, expectedErrMsg)
})
Expand Down
11 changes: 4 additions & 7 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewDocumentType(doc *yamlmeta.Document) (*DocumentType, error) {
return nil, err
}

v, err := getValidation(doc, typeOfValue)
v, err := getValidation(doc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func NewMapItemType(item *yamlmeta.MapItem) (*MapItemType, error) {
return nil, err
}

v, err := getValidation(item, typeOfValue)
v, err := getValidation(item)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func NewArrayItemType(item *yamlmeta.ArrayItem) (*ArrayItemType, error) {
return nil, err
}

v, err := getValidation(item, typeOfValue)
v, err := getValidation(item)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func getValue(node yamlmeta.Node, t Type) (interface{}, error) {
return t.GetDefaultValue(), nil
}

func getValidation(node yamlmeta.Node, nodeType Type) (*validations.NodeValidation, error) {
func getValidation(node yamlmeta.Node) (*validations.NodeValidation, error) {
if !experiments.IsValidationsEnabled() {
return nil, nil
}
Expand All @@ -174,9 +174,6 @@ func getValidation(node yamlmeta.Node, nodeType Type) (*validations.NodeValidati
}

if validationAnn != nil {
if _, ok := nodeType.(*NullType); ok {
validationAnn.GetValidation().DefaultNullSkipTrue()
}
return validationAnn.GetValidation(), nil
}
return nil, nil
Expand Down
24 changes: 8 additions & 16 deletions pkg/validations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import (
const (
AnnotationAssertValidate template.AnnotationName = "assert/validate"

KwargWhen string = "when"
KwargWhenNullSkip string = "when_null_skip"
KwargMinLength string = "min_len"
KwargMaxLength string = "max_len"
KwargMin string = "min"
KwargMax string = "max"
KwargNotNull string = "not_null"
KwargOneNotNull string = "one_not_null"
KwargOneOf string = "one_of"
KwargWhen string = "when"
KwargMinLength string = "min_len"
KwargMaxLength string = "max_len"
KwargMin string = "min"
KwargMax string = "max"
KwargNotNull string = "not_null"
KwargOneNotNull string = "one_not_null"
KwargOneOf string = "one_of"
)

// ProcessAssertValidateAnns checks Assert annotations on data values and stores them on a Node as Validations.
Expand Down Expand Up @@ -138,13 +137,6 @@ func newValidationKwargs(kwargs []starlark.Tuple, annPos *filepos.Position) (val
return validationKwargs{}, fmt.Errorf("expected keyword argument %q to be a function, but was %s (at %s)", KwargWhen, value[1].Type(), annPos.AsCompactString())
}
processedKwargs.when = v
case KwargWhenNullSkip:
v, ok := value[1].(starlark.Bool)
if !ok {
return validationKwargs{}, fmt.Errorf("expected keyword argument %q to be a boolean, but was %s (at %s)", KwargWhenNullSkip, value[1].Type(), annPos.AsCompactString())
}
b := bool(v)
processedKwargs.whenNullSkip = &b
case KwargMinLength:
v, err := starlark.NumberToInt(value[1])
if err != nil {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#@assert/validate when_null_skip=True, not_null=True
#@assert/validate not_null=True
foo: null

+++
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#@assert/validate ("", lambda v: fail("fails")), when_null_skip=True
#@assert/validate ("", lambda v: fail("fails"))
foo: null

+++
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#@assert/validate ("", lambda v: fail("rules were run")), when_null_skip=True
#@assert/validate ("", lambda v: fail("rules were run"))
foo: ""

+++
Expand Down
30 changes: 9 additions & 21 deletions pkg/validations/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ type rule struct {

// validationKwargs represent the optional keyword arguments and their values in a validationRun annotation.
type validationKwargs struct {
when starlark.Callable
whenNullSkip *bool // default: nil if kwarg is not set, True if value is Nullable
minLength *starlark.Int // 0 len("") == 0, this always passes
maxLength *starlark.Int
min starlark.Value
max starlark.Value
notNull bool
oneNotNull starlark.Value // valid values are either starlark.Sequence or starlark.Bool
oneOf starlark.Sequence
when starlark.Callable
minLength *starlark.Int // 0 len("") == 0, this always passes
maxLength *starlark.Int
min starlark.Value
max starlark.Value
notNull bool
oneNotNull starlark.Value // valid values are either starlark.Sequence or starlark.Bool
oneOf starlark.Sequence
}

// Run takes a root Node, and threadName, and validates each Node in the tree.
Expand Down Expand Up @@ -120,22 +119,11 @@ func (v NodeValidation) Validate(node yamlmeta.Node, thread *starlark.Thread) []
return failures
}

// DefaultNullSkipTrue sets the kwarg when_null_skip to true if not set explicitly.
func (v *NodeValidation) DefaultNullSkipTrue() {
if v.kwargs.whenNullSkip == nil {
t := true
v.kwargs.whenNullSkip = &t
}
}

// shouldValidate uses validationKwargs and the node's value to run checks on the value. If the value satisfies the checks,
// then the NodeValidation's rules should execute, otherwise the rules will be skipped.
func (v validationKwargs) shouldValidate(value starlark.Value, thread *starlark.Thread) (bool, error) {
// avoid nil pointer errors: handle `when_null_skip=`, first
_, valueIsNull := value.(starlark.NoneType)
nullIsAllowed := !v.notNull
whenNullSkip := v.whenNullSkip != nil && *v.whenNullSkip
if valueIsNull && nullIsAllowed && whenNullSkip {
if valueIsNull && !v.notNull {
return false, nil
}

Expand Down

0 comments on commit 3a85263

Please sign in to comment.