Skip to content

Commit

Permalink
topdown: is_valid functions no longer error. (#4844)
Browse files Browse the repository at this point in the history
This commit updates the `*.is_valid` functions to no longer produce
errors when providing wrong-typed arguments. Instead, they will now
return true/false for all inputs. Tests and WASM versions of these
builtins have been updated to match the new behavior.

Fixes #4760.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
  • Loading branch information
philipaconrad committed Jul 6, 2022
1 parent b2bf19f commit 41dc9f7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
10 changes: 6 additions & 4 deletions test/cases/testdata/jsonbuiltins/test-is-valid.yaml
Expand Up @@ -12,6 +12,7 @@ cases:
]
p = [x | doc = documents[_]; json.is_valid(doc, x)]
strict_error: true
want_result:
- x:
- false
Expand All @@ -28,9 +29,9 @@ cases:
}
query: data.generated.p = x
input: {"foo": 1}
want_error: operand 1 must be string but got number
want_error_code: eval_type_error
strict_error: true
want_result:
- x: false

- note: jsonbuiltins/yaml is_valid
query: data.generated.p = x
Expand All @@ -49,6 +50,7 @@ cases:
]
p = [x | doc = documents[_]; yaml.is_valid(doc, x)]
strict_error: true
want_result:
- x:
- true
Expand All @@ -65,6 +67,6 @@ cases:
}
query: data.generated.p = x
input: {"foo": 1}
want_error: operand 1 must be string but got number
want_error_code: eval_type_error
strict_error: true
want_result:
- x: false
6 changes: 3 additions & 3 deletions topdown/encoding.go
Expand Up @@ -55,7 +55,7 @@ func builtinJSONIsValid(a ast.Value) (ast.Value, error) {

str, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
return ast.Boolean(false), nil
}

return ast.Boolean(json.Valid([]byte(str))), nil
Expand Down Expand Up @@ -83,7 +83,7 @@ func builtinBase64Decode(a ast.Value) (ast.Value, error) {
func builtinBase64IsValid(a ast.Value) (ast.Value, error) {
str, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
return ast.Boolean(false), nil
}

_, err = base64.StdEncoding.DecodeString(string(str))
Expand Down Expand Up @@ -257,7 +257,7 @@ func builtinYAMLUnmarshal(a ast.Value) (ast.Value, error) {
func builtinYAMLIsValid(a ast.Value) (ast.Value, error) {
str, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
return ast.Boolean(false), nil
}

var x interface{}
Expand Down
4 changes: 2 additions & 2 deletions topdown/graphql.go
Expand Up @@ -343,11 +343,11 @@ func builtinGraphQLIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*as
// feed them to the GraphQL parser functions.
rawQuery, err := builtins.StringOperand(operands[0].Value, 1)
if err != nil {
return err
return iter(ast.BooleanTerm(false))
}
rawSchema, err := builtins.StringOperand(operands[1].Value, 1)
if err != nil {
return err
return iter(ast.BooleanTerm(false))
}

// Generate ASTs/errors for the GraphQL schema and query.
Expand Down
3 changes: 1 addition & 2 deletions topdown/semver.go
Expand Up @@ -40,8 +40,7 @@ func builtinSemVerCompare(bctx BuiltinContext, args []*ast.Term, iter func(*ast.
func builtinSemVerIsValid(bctx BuiltinContext, args []*ast.Term, iter func(*ast.Term) error) error {
versionString, err := builtins.StringOperand(args[0].Value, 1)
if err != nil {
result := ast.BooleanTerm(false)
return iter(result)
return iter(ast.BooleanTerm(false))
}

result := true
Expand Down
4 changes: 2 additions & 2 deletions wasm/src/encoding.c
Expand Up @@ -223,7 +223,7 @@ opa_value *opa_base64_is_valid(opa_value *a)
{
if (opa_value_type(a) != OPA_STRING)
{
return NULL;
return opa_boolean(false);
}

opa_string_t *s = opa_cast_string(a);
Expand Down Expand Up @@ -323,7 +323,7 @@ opa_value *opa_json_is_valid(opa_value *a)
{
if (opa_value_type(a) != OPA_STRING)
{
return NULL;
return opa_boolean(false);
}


Expand Down

0 comments on commit 41dc9f7

Please sign in to comment.