Skip to content

Add yaml.is_valid builtin - #150

Merged
sspaink merged 2 commits into
open-policy-agent:mainfrom
Aayush10016:issue-144-yaml-is-valid
Jul 28, 2026
Merged

Add yaml.is_valid builtin#150
sspaink merged 2 commits into
open-policy-agent:mainfrom
Aayush10016:issue-144-yaml-is-valid

Conversation

@Aayush10016

Copy link
Copy Markdown
Contributor

Summary

  • adds the yaml.is_valid builtin to opa-builtins-json
  • validates YAML strings with the existing Jackson YAML mapper
  • adds focused tests for registration, valid YAML, and invalid YAML

Closes #144.

Verification

  • JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 .\gradlew.bat --no-daemon --console=plain :opa-builtins:opa-builtins-json:test
  • JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 .\gradlew.bat --no-daemon --console=plain :opa-builtins:opa-builtins-json:check

Note: On this Windows environment, the first Gradle run without explicit UTF-8 failed while compiling existing Unicode box-drawing characters in MetricsPrinter.java using Windows-1252. Rerunning with UTF-8 completed successfully.

Signed-off-by: Aayush Tiwari <aayushtiwari1001@gmail.com>
@Aayush10016
Aayush10016 force-pushed the issue-144-yaml-is-valid branch from 970e376 to 3d243f6 Compare July 12, 2026 12:28
@sspaink

sspaink commented Jul 16, 2026

Copy link
Copy Markdown
Member

@Aayush10016 did you mean to open this as a draft?

@Aayush10016
Aayush10016 marked this pull request as ready for review July 17, 2026 15:04
@Aayush10016
Aayush10016 requested a review from a team as a code owner July 17, 2026 15:04
@Aayush10016

Copy link
Copy Markdown
Contributor Author

It was initially opened as a draft while validation and DCO were being finalized. It is now marked ready for review, and the current checks are passing. Thanks for checking.

@sspaink sspaink left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for working on this!

name = "result",
description = "`true` if `x` is valid YAML, `false` otherwise"))
public RegoBoolean yamlIsValid(EvaluationContext ctx, RegoValue[] args) {
String yamlInput = getArg(args, 0, RegoString.class).getValue();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OPA returns false for non-string operands rather that raising a type error. Can you catch that here and return false instead? Adding a test to cover this would also be nice.

if (!(args[0] instanceof RegoString yamlInput)) {
  return RegoBoolean.FALSE;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I pushed an update that:

  • returns false from yaml.is_valid when the operand is not a RegoString
  • adds a non-string input test
  • updates the JUnit/JUnit Platform versions in opa-builtins-json to 6.1.2

Validation passed locally:

  • :opa-builtins:opa-builtins-json:test --tests io.github.open_policy_agent.opa.ast.builtin.impls.JsonBuiltinsTest
  • :opa-builtins:opa-builtins-json:test

DCO is passing now as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great thank you!

Comment on lines +22 to +23
testImplementation("org.junit.jupiter:junit-jupiter:6.1.1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.1.1")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
testImplementation("org.junit.jupiter:junit-jupiter:6.1.1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.1.1")
testImplementation("org.junit.jupiter:junit-jupiter:6.1.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.1.2")

the rest of the repo is on 6.1.2

Signed-off-by: Aayush Tiwari <aayushtiwari1001@gmail.com>
@Aayush10016
Aayush10016 force-pushed the issue-144-yaml-is-valid branch from be0abde to 0fbaa79 Compare July 28, 2026 18:07
@sspaink
sspaink merged commit 6647d68 into open-policy-agent:main Jul 28, 2026
18 checks passed
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.

Implement yaml.is_valid builtin

2 participants