Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

topdown: Make *.is_valid function behavior more consistent #4844

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions test/cases/testdata/jsonbuiltins/test-is-valid.yaml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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