Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions opa-builtins/opa-builtins-json/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ dependencies {
// RegoValueModule (auto-registered via Jackson SPI) provides (de)serializers for the AST
// types so they don't need to carry annotations.
runtimeOnly(project(":opa-jackson"))

testImplementation("org.junit.jupiter:junit-jupiter:6.1.1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.1.1")
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public Map<String, BiFunction<EvaluationContext, RegoValue[], RegoValue>> builti
result.put("json.remove", instance::remove);
result.put("json.unmarshal", instance::unmarshal);
result.put("json.verify_schema", instance::verify_schema);
result.put("yaml.is_valid", instance::yamlIsValid);
result.put("yaml.marshal", instance::yamlMarshal);
result.put("yaml.unmarshal", instance::yamlUnmarshal);
return result;
Expand Down Expand Up @@ -706,6 +707,26 @@ public RegoString yamlMarshal(EvaluationContext ctx, RegoValue[] args) {
}
}

@OpaBuiltin(
name = "yaml.is_valid",
description = "Verifies the input string is a valid YAML document.",
categories = {"encoding"},
args = {@OpaType(type = "string", name = "x", description = "a YAML string")},
result =
@OpaType(
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();

try {
YAML_MAPPER.readTree(yamlInput);
return RegoBoolean.TRUE;
} catch (JsonProcessingException e) {
return RegoBoolean.FALSE;
}
}

@OpaBuiltin(
name = "yaml.unmarshal",
description = "Deserializes the input YAML string.",
Expand Down Expand Up @@ -1071,4 +1092,4 @@ private JsonNode removeNodes(JsonNode node, Set<String> paths) {

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.open_policy_agent.opa.ast.builtin.impls;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.github.open_policy_agent.opa.ast.types.RegoBoolean;
import io.github.open_policy_agent.opa.ast.types.RegoString;
import io.github.open_policy_agent.opa.ast.types.RegoValue;
import org.junit.jupiter.api.Test;

class JsonBuiltinsTest {

private final JsonBuiltins builtins = new JsonBuiltins();

@Test
void registersYamlIsValidBuiltin() {
assertTrue(builtins.builtins().containsKey("yaml.is_valid"));
}

@Test
void yamlIsValidReturnsTrueForValidYaml() {
RegoValue[] args = {new RegoString("name: opa\nvalues:\n - sdk\n - java\n")};

assertEquals(RegoBoolean.TRUE, builtins.yamlIsValid(null, args));
}

@Test
void yamlIsValidReturnsFalseForInvalidYaml() {
RegoValue[] args = {new RegoString("name: [unterminated")};

assertEquals(RegoBoolean.FALSE, builtins.yamlIsValid(null, args));
}
}