Object Built-ins
| Function | Description | OPA | Wasm | Swift | Java |
|---|---|---|---|---|---|
|
Filters the object. For example: Arguments: Returns:object (object[any: any])object to filter paths (any<array[any<string, array[any]>], set[any<string, array[any]>]>)JSON string paths filtered (object[any: any])remaining data from | v0.17.0 | ✓ | 0.0.10 | 0.4.0 |
|
Checks that the document matches the JSON schema. The Arguments: Returns:document (any<string, object[any: any]>)document to verify by schema schema (any<string, object[any: any]>)schema to verify document by output (array<boolean, array[object<desc: string, error: string, field: string, type: string>]>)
| v0.50.0 | SDK | ✗ | 0.4.0 |
|
Patches an object according to RFC6902. For example: Arguments: Returns:target (any)the object, array or set to patch patches (array[object<op: string, path: any>[any: any]])the JSON patches to apply output (any)result obtained after consecutively applying all patch operations in | v0.25.0 | SDK | 0.0.10 | 0.4.0 |
|
Removes paths from an object. For example: Arguments: Returns:object (object[any: any])object to remove paths from paths (any<array[any<string, array[any]>], set[any<string, array[any]>]>)JSON string paths output (object[any: any])result of removing all keys specified in | v0.18.0 | ✓ | 0.0.10 | 0.4.0 |
|
Checks that the input is a valid JSON schema object. The schema can be either a JSON string or an JSON object. The Arguments: Returns:schema (any<string, object[any: any]>)the schema to verify output (array<boolean, any<null, string>>)
| v0.50.0 | SDK | ✗ | 0.4.0 |
|
Filters the object by keeping only specified keys. For example: Arguments: Returns:object (object[any: any])object to filter keys keys (any<array[any], object[any: any], set[any]>)keys to keep in filtered (object[any: any])remaining data from | v0.17.2 | ✓ | 0.0.9 | 0.1.0 |
|
Returns value of an object's key if present, otherwise a default. If the supplied Arguments: Returns:object (object[any: any])object to get key (any)key to lookup in default (any)default to use when lookup fails value (any)
| v0.17.0 | ✓ | 0.0.1 | 0.1.0 |
|
Returns a set of an object's keys. For example: Arguments: Returns:object (object[any: any])object to get keys from value (set[any])set of | v0.47.0 | ✓ | 0.0.1 | 0.1.0 |
|
Removes specified keys from an object. Arguments: Returns:object (object[any: any])object to remove keys from keys (any<array[any], object[any: any], set[any]>)keys to remove from x output (object[any: any])result of removing the specified | v0.17.2 | ✓ | 0.0.9 | 0.1.0 |
|
Determines if an object Arguments: Returns:super (any<array[any], object[any: any], set[any]>)object to test if sub is a subset of sub (any<array[any], object[any: any], set[any]>)object to test if super is a superset of result (boolean)
| v0.42.0 | SDK | 0.0.9 | 0.1.0 |
|
Creates a new object of the asymmetric union of two objects. For example: Arguments: Returns:a (object[any: any])left-hand object b (object[any: any])right-hand object output (object[any: any])a new object which is the result of an asymmetric recursive union of two objects where conflicts are resolved by choosing the key from the right-hand object | v0.17.2 | ✓ | 0.0.1 | 0.1.0 |
|
Creates a new object that is the asymmetric union of all objects merged from left to right. For example: Arguments: Returns:objects (array[object[any: any]])list of objects to merge output (object[any: any])asymmetric recursive union of all objects in | v0.37.0 | ✓ | 0.0.1 | 0.1.0 |
- When
keysare provided as an object only the top level keys on the object will be used, values are ignored. For example:object.remove({"a": {"b": {"c": 2}}, "x": 123}, {"a": 1}) == {"x": 123}regardless of the value for keyain the keys object, the followingkeysobject gives the same resultobject.remove({"a": {"b": {"c": 2}}, "x": 123}, {"a": {"b": {"foo": "bar"}}}) == {"x": 123}. - The
jsonstringpathsmay reference into array values by using index numbers. For example with the object{"a": ["x", "y", "z"]}the patha/1referencesy. Nested structures are supported as well, for example:{"a": ["x", {"y": {"y1": {"y2": ["foo", "bar"]}}}, "z"]}the patha/1/y1/y2/0references"foo". - The
jsonstringpathssupport~0, or~1characters for~and/characters in key names. It does not support-for last index of an array. For example the path/foo~1bar~0will referencebazin{ "foo/bar~": "baz" }. - The
jsonstringpathsmay be an array of string path segments rather than a/separated string. For example the patha/b/ccan be passed in as["a", "b", "c"].
Examples
object.get
object.get reads a key from an object and returns a default when the key
is missing. The second argument can also be a path array to walk nested
objects. That is useful for optional labels, annotations, or config keys
that callers are allowed to omit.
Reading optional labels with a default
Kubernetes objects and API payloads often omit optional fields. object.get
reads a key (or a nested path) and returns a default when it is missing, so
the rest of the policy does not need extra existence checks. Using a path
array also covers the case where an intermediate field like labels is
undefined.
Here, workloads without an env label are treated as dev.
package play
# object.get(object, key, default) — key may also be a path array, which
# still returns the default when an intermediate field (like labels) is missing.
env_of(workload) := object.get(workload, ["labels", "env"], "dev")
# Only production workloads need a team label.
deny contains msg if {
some w in input.workloads
env_of(w) == "prod"
not w.labels.team
msg := sprintf("production workload %q is missing labels.team", [w.name])
}
envs := {w.name: env_of(w) | some w in input.workloads}
{
"deny": [
"production workload \"frontend\" is missing labels.team"
],
"envs": {
"frontend": "prod",
"scratch": "dev"
}
}{
"workloads": [
{
"name": "frontend",
"labels": {
"app": "web",
"env": "prod"
}
},
{
"name": "scratch",
"labels": {
"app": "jobs"
}
}
]
}
{}