Skip to content

docs: add array.concat and array.slice examples to array.mdx#8669

Open
omribz156 wants to merge 1 commit into
open-policy-agent:mainfrom
omribz156:docs/array-concat-slice-examples
Open

docs: add array.concat and array.slice examples to array.mdx#8669
omribz156 wants to merge 1 commit into
open-policy-agent:mainfrom
omribz156:docs/array-concat-slice-examples

Conversation

@omribz156
Copy link
Copy Markdown

Why the changes in this PR are needed?

The Arrays built-ins page has no examples at all — only the <BuiltinTable>. Issue #3786 tracks adding more examples across built-ins to help users get started faster. array.concat and array.slice are practical functions used in real admission policies and there is nothing to show how to call them.

What are the changes in this PR?

Adds two PlaygroundExample entries to array.mdx:

  • array.concat — merging a base label-requirement list with an environment-specific list in a Kubernetes admission policy deny rule.
  • array.slice — extracting a fixed-length route prefix from a variable-length request path after splitting on /.

Each example follows the existing pattern from aggregates/ and strings/: policy.rego, input.json, data.json, output.json, intro.md, title.txt, config.json.

Notes to assist PR review:

  • No Go or test changes; docs-only.
  • DCO sign-off is on the commit.
  • This was written with Claude assistance. I reviewed the policy logic and output values manually before submitting.

Further comments:

Closes part of #3786. Happy to add more array built-in examples (array.reverse, array.set_diff) in a follow-up if this shape looks right.

Adds two PlaygroundExample entries to the Arrays built-ins page, which
previously had no examples at all.

- array.concat: merging base and environment-specific label requirements
  in a Kubernetes admission policy
- array.slice: extracting a fixed-length route prefix from a
  variable-length request path

Closes part of open-policy-agent#3786.

Signed-off-by: Omri SirComp <omribz156@gmail.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented May 18, 2026

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 4154680
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/6a0b2234b370d400081bedb7
😎 Deploy Preview https://deploy-preview-8669--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing! 🙂

Maybe we could reduce the complexity of these examples a bit? So the target functions don't get lost in the overall complexity.

@@ -0,0 +1,5 @@
<!-- markdownlint-disable MD041 -->

`array.concat` joins two arrays in order. A common use is building a complete
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this'll be injected into the content in array.mdx, the reader will experience a bit of a hiccup in the text. E.g.:

### array.concat

array.concat joins two arrays in order, producing a new array that contains all elements of the first array followed by all elements of the second.

#### Merging base and environment label requirements

array.concat joins two arrays in order. A common use is building a complete validation list by merging a shared base set of rules with environment-specific additions, then iterating the result in a single deny rule.

Consider dropping the redundant text from here to make it read a bit better. Same for array.slice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider reducing the complexity of this rule, as the important part (array.concat) might get lost in the weeds for the reader.

package play

# Split a request path into segments.
# e.g. "/api/v1/users/123" → ["api", "v1", "users", "123"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This arrow won't transition well when the user open's the example in the playground. Consider not using any "special" characters.

api_version := path_segments[1]

# The first two segments identify the route, regardless of how long the path is.
route_prefix := array.slice(path_segments, 0, 2)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other example policy, I wonder if this policy isn't a bit too complicated for its intended purpose. route_prefix will contain the result of the function we want to demo, but it's not used anywhere in the example.

Comment on lines +14 to +17
deny contains msg if {
api_version != "v1"
msg := sprintf("unsupported API version %q; only v1 is allowed", [api_version])
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the final example policies end up using generated strings, consider using string-templates instead of sprintf, as it might reduce the apparent complexity for the reader, making it easier to focus on what's important.

Suggested change
deny contains msg if {
api_version != "v1"
msg := sprintf("unsupported API version %q; only v1 is allowed", [api_version])
}
deny contains $"unsupported API version {api_version}; only v1 is allowed" if {
api_version != "v1"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants