From 3e3d26c987c1c6240c8f3be57f34508449eb2384 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Fri, 23 Feb 2024 11:36:10 +0100 Subject: [PATCH] Add JsonSchemasProbe class for validating JSON schema and its aliases --- .../inspect/vc/probe/JsonSchemasProbe.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonSchemasProbe.java diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonSchemasProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonSchemasProbe.java new file mode 100644 index 0000000..fd70297 --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonSchemasProbe.java @@ -0,0 +1,62 @@ +package org.oneedtech.inspect.vc.probe; + +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.oneedtech.inspect.core.probe.Outcome; +import org.oneedtech.inspect.core.probe.Probe; +import org.oneedtech.inspect.core.probe.RunContext; +import org.oneedtech.inspect.core.probe.json.JsonSchemaProbe; +import org.oneedtech.inspect.core.report.ReportItem; +import org.oneedtech.inspect.core.report.ReportItems; +import org.oneedtech.inspect.schema.SchemaKey; +import org.oneedtech.inspect.vc.VerifiableCredential; + +public class JsonSchemasProbe extends Probe { + private SchemaKey key; + + public JsonSchemasProbe(SchemaKey key) { + this.key = key; + } + + @Override + public ReportItems run(VerifiableCredential cred, RunContext ctx) throws Exception { + + // main run + JsonSchemaProbe jsonSchemaProbe = new JsonSchemaProbe(key); + ReportItems report = jsonSchemaProbe.run(cred.getJson(), ctx); + + if (report.asBoolean()) { + return report; + } + + // aliases run + if (key.getAliases().isPresent()) { + for (SchemaKey alias : key.getAliases().get()) { + jsonSchemaProbe = new JsonSchemaProbe(alias); + ReportItems newReport = jsonSchemaProbe.run(cred.getJson(), ctx); + + if (newReport.asBoolean()) { + // return old errors as warnings + return new ReportItems( + StreamSupport.stream(report.spliterator(), false) + .map( + item -> + new ReportItem.Builder(this) + .msg( + "JSON-LD schema validation succeed. However, Plain JSON schema" + + " validation failed, so it could have an impact when working with" + + " plain JSON platforms. Error:" + + item.getMessage()) + .loc(item.getLocation()) + .outcome(Outcome.WARNING) + .build()) + .collect(Collectors.toUnmodifiableList())); + } + } + + } + + return report; + } +}