forked from enterprise-contract/ec-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request enterprise-contract#1204 from zregvart/issue/EC-895
- Loading branch information
Showing
52 changed files
with
838 additions
and
471 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package lib.json | ||
|
||
import rego.v1 | ||
|
||
# Validates schema reporting the error message as well as the severity | ||
validate_schema(doc, schema) := issues if { | ||
count(_arg_issues(doc, schema)) == 0 | ||
issues := _validation_issues(doc, schema) | ||
} else := _arg_issues(doc, schema) | ||
|
||
_validation_issues(doc, schema) := issues if { | ||
not is_null(doc) | ||
not is_null(schema) | ||
d := _prepare_document(doc) | ||
ok_error := json.match_schema(d, schema) | ||
ok := ok_error[0] | ||
errors := ok_error[1] | ||
not ok | ||
issues := [i | | ||
some e in errors | ||
i := { | ||
"message": e.error, # e.desc is ignored, seems to repeat what is in e.error | ||
"severity": _severity(e), | ||
} | ||
] | ||
} | ||
|
||
_arg_issues(doc, schema) := [i | | ||
some check in [ | ||
{is_null(doc) == false: "Provided empty document for schema validation"}, | ||
{is_null(schema) == false: "Provided empty schema for schema validation"}, | ||
_check_schema(schema), | ||
] | ||
some ok, msg in check | ||
not ok | ||
i := { | ||
"message": msg, | ||
"severity": "failure", | ||
} | ||
] | ||
|
||
_check_schema(schema) := ok_msg if { | ||
not is_null(schema) | ||
ok_error := json.verify_schema(schema) | ||
ok := ok_error[0] | ||
error := ok_error[1] | ||
not ok | ||
ok_msg := {false: sprintf("Provided schema is not a valid JSON Schema: %s", [error])} | ||
} else := {true, ""} | ||
|
||
_prepare_document(doc) := d if { | ||
is_array(doc) | ||
|
||
# match_schema expects either a marshaled JSON resource (String) or an | ||
# Object. It doesn't handle an Array directly. | ||
d := json.marshal(doc) | ||
} else := doc | ||
|
||
_severity(e) := "warning" if { | ||
startswith(e.desc, "Additional property") | ||
} else := "failure" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package lib.json_test | ||
|
||
import data.lib | ||
import data.lib.json as j | ||
import rego.v1 | ||
|
||
test_validate_args if { | ||
lib.assert_equal( | ||
[ | ||
{ | ||
"message": "Provided empty document for schema validation", | ||
"severity": "failure", | ||
}, | ||
{ | ||
"message": "Provided empty schema for schema validation", | ||
"severity": "failure", | ||
}, | ||
], | ||
j.validate_schema(null, null), | ||
) | ||
lib.assert_equal( | ||
[{ | ||
"message": "Provided empty schema for schema validation", | ||
"severity": "failure", | ||
}], | ||
j.validate_schema({}, null), | ||
) | ||
lib.assert_equal( | ||
[{ | ||
"message": "Provided empty document for schema validation", | ||
"severity": "failure", | ||
}], | ||
j.validate_schema(null, {}), | ||
) | ||
lib.assert_equal( | ||
[{ | ||
"message": "Provided schema is not a valid JSON Schema: jsonschema: wrong type, expected string or object", | ||
"severity": "failure", | ||
}], | ||
j.validate_schema({}, ["something"]), | ||
) | ||
} | ||
|
||
test_validate_schema_ok if { | ||
lib.assert_equal( | ||
[], | ||
j.validate_schema({"a": 3}, { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"properties": {"a": {"type": "number"}}, | ||
}), | ||
) | ||
lib.assert_equal( | ||
[], | ||
j.validate_schema([{"a": 3}], { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "array", | ||
"items": {"properties": {"a": {"type": "number"}}}, | ||
}), | ||
) | ||
} | ||
|
||
test_validate_schema_not_ok if { | ||
lib.assert_equal( | ||
[{ | ||
"message": "a: Invalid type. Expected: number, given: string", | ||
"severity": "failure", | ||
}], | ||
j.validate_schema({"a": "b"}, { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"properties": {"a": {"type": "number"}}, | ||
}), | ||
) | ||
lib.assert_equal( | ||
[{ | ||
"message": "0.a: Invalid type. Expected: number, given: string", | ||
"severity": "failure", | ||
}], | ||
j.validate_schema([{"a": "b"}], { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "array", | ||
"items": {"properties": {"a": {"type": "number"}}}, | ||
}), | ||
) | ||
} | ||
|
||
test_validate_schema_unknown_property_warning if { | ||
lib.assert_equal( | ||
[{ | ||
"message": "(Root): Additional property b is not allowed", | ||
"severity": "warning", | ||
}], | ||
j.validate_schema({"a": 3, "b": "here"}, { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"properties": {"a": {"type": "number"}}, | ||
"additionalProperties": false, | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.