From b34518f54c6e2956c759138f413e017bd41ef984 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Fri, 20 Oct 2023 17:52:24 +0200 Subject: [PATCH] Allow override of achivement probes in credential subject --- .../vc/probe/CredentialSubjectProbe.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/CredentialSubjectProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/CredentialSubjectProbe.java index a97f00b..2c4a103 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/CredentialSubjectProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/CredentialSubjectProbe.java @@ -1,6 +1,7 @@ package org.oneedtech.inspect.vc.probe; import java.util.List; +import java.util.Optional; import org.oneedtech.inspect.core.probe.Probe; import org.oneedtech.inspect.core.probe.RunContext; @@ -83,21 +84,11 @@ public class CredentialSubjectProbe extends Probe { * Check achievement result description */ if (subject.hasNonNull("achievement")) { - JsonNode achievement = subject.get("achievement"); - if (achievement.hasNonNull("resultDescription")) { - List resultDescriptions = JsonNodeUtil.asNodeList(achievement.get("resultDescription")); - for (JsonNode resultDescription : resultDescriptions) { - // check that type contains "ResultDescription" - if (!JsonNodeUtil.asStringList(resultDescription.get("type")).contains("ResultDescription")) { - return error("resultDescription in achievement of credentialSubject is not of type \"ResultDescription\"", ctx); - } - } - } - // criteria must have id or narrative - JsonNode criteria = achievement.get("criteria"); - if (!criteria.hasNonNull("id") && !criteria.hasNonNull("narrative")) { - return error("criteria in achievement of credentialSubject must have id or narrative", ctx); + Optional achievementResult = checkAchievement(subject.get("achievement"), ctx); + if (achievementResult.isPresent()) { + return achievementResult.get(); } + } else if (achivementRequired) { return error("missing required achievement in credentialSubject", ctx); } @@ -115,6 +106,23 @@ public class CredentialSubjectProbe extends Probe { return success(ctx); } + protected Optional checkAchievement(JsonNode achievement, RunContext ctx) { + if (achievement.hasNonNull("resultDescription")) { + List resultDescriptions = JsonNodeUtil.asNodeList(achievement.get("resultDescription")); + for (JsonNode resultDescription : resultDescriptions) { + // check that type contains "ResultDescription" + if (!JsonNodeUtil.asStringList(resultDescription.get("type")).contains("ResultDescription")) { + return Optional.of(error("resultDescription in achievement of credentialSubject is not of type \"ResultDescription\"", ctx)); + } + } + } + // criteria must have id or narrative + JsonNode criteria = achievement.get("criteria"); + if (!criteria.hasNonNull("id") && !criteria.hasNonNull("narrative")) { + return Optional.of(error("criteria in achievement of credentialSubject must have id or narrative", ctx)); + } + return Optional.empty(); + } private boolean idAndIdentifierEmpty(JsonNode root) { JsonNode id = root.get("id"); if (id != null && id.textValue().strip().length() > 0) return false;