Added related validation test, fixing fullValidate

This commit is contained in:
Xavi Aracil 2022-12-12 16:14:18 +01:00
parent 8260d7e7aa
commit caea0b50f4
9 changed files with 60 additions and 11 deletions

View File

@ -201,7 +201,7 @@ public class Assertion extends Credential {
new Validation.Builder().name("image").type(ValueType.IMAGE).build(), new Validation.Builder().name("image").type(ValueType.IMAGE).build(),
new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(), new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(),
new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(), new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(),
new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Assertion).many(true).build(), new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Assertion).many(true).fullValidate(false).build(),
new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build() new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build()
)) ))
.put(Type.BadgeClass, List.of( .put(Type.BadgeClass, List.of(
@ -216,7 +216,7 @@ public class Assertion extends Credential {
new Validation.Builder().name("tags").type(ValueType.TEXT).many(true).build(), new Validation.Builder().name("tags").type(ValueType.TEXT).many(true).build(),
new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(), new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(),
new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(), new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(),
new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.BadgeClass).many(true).build(), new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.BadgeClass).many(true).fullValidate(false).build(),
new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build() new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build()
)) ))
.put(Type.AlignmentObject, List.of( .put(Type.AlignmentObject, List.of(
@ -241,13 +241,13 @@ public class Assertion extends Credential {
.put(Type.Endorsement, List.of( .put(Type.Endorsement, List.of(
new Validation.Builder().name("id").type(ValueType.IRI).required(true).build(), new Validation.Builder().name("id").type(ValueType.IRI).required(true).build(),
new Validation.Builder().name("type").type(ValueType.RDF_TYPE).required(true).many(true).mustContainOneType(List.of(Type.Endorsement)).build(), new Validation.Builder().name("type").type(ValueType.RDF_TYPE).required(true).many(true).mustContainOneType(List.of(Type.Endorsement)).build(),
new Validation.Builder().name("claim").type(ValueType.ID).required(true).expectedTypes(List.of(Type.EndorsementClaim, Type.Endorsement)).build(), new Validation.Builder().name("claim").type(ValueType.ID).required(true).expectedTypes(List.of(Type.EndorsementClaim, Type.Endorsement)).fullValidate(false).build(),
new Validation.Builder().name("issuedOn").type(ValueType.DATETIME).required(true).build(), new Validation.Builder().name("issuedOn").type(ValueType.DATETIME).required(true).build(),
new Validation.Builder().name("issuer").type(ValueType.ID).expectedType(Type.Profile).fetch(true).required(true).build(), new Validation.Builder().name("issuer").type(ValueType.ID).expectedType(Type.Profile).fetch(true).required(true).build(),
new Validation.Builder().name("verification").build(), new Validation.Builder().name("verification").build(),
new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(), new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(),
new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(), new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(),
new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Endorsement).many(true).build(), new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Endorsement).many(true).fullValidate(false).build(),
new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build() new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build()
)) ))
.put(Type.EndorsementClaim, List.of( .put(Type.EndorsementClaim, List.of(
@ -303,7 +303,7 @@ public class Assertion extends Credential {
new Validation.Builder().name("id").type(ValueType.ISSUER).messageLevel(MessageLevel.Warning).build(), new Validation.Builder().name("id").type(ValueType.ISSUER).messageLevel(MessageLevel.Warning).build(),
new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(), new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(),
new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(), new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(),
new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Issuer).many(true).build(), new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Issuer).many(true).fullValidate(false).build(),
new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build() new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build()
)) ))
.put(Type.Profile, List.of( .put(Type.Profile, List.of(
@ -320,7 +320,7 @@ public class Assertion extends Credential {
new Validation.Builder().name("id").type(ValueType.ISSUER).messageLevel(MessageLevel.Warning).build(), new Validation.Builder().name("id").type(ValueType.ISSUER).messageLevel(MessageLevel.Warning).build(),
new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(), new Validation.Builder().name("@language").type(ValueType.LANGUAGE).build(),
new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(), new Validation.Builder().name("version").type(ValueType.TEXT_OR_NUMBER).build(),
new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Profile).many(true).build(), new Validation.Builder().name("related").type(ValueType.ID).allowRemoteUrl(true).expectedType(Type.Profile).many(true).fullValidate(false).build(),
new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build() new Validation.Builder().name("endorsement").type(ValueType.ID).allowRemoteUrl(true).fetch(true).expectedType(Type.Endorsement).many(true).build()
)) ))
.put(Type.RevocationList, List.of( .put(Type.RevocationList, List.of(

View File

@ -122,6 +122,7 @@ public class Validation {
this.prerequisites = new ArrayList<>(); this.prerequisites = new ArrayList<>();
this.expectedTypes = new ArrayList<>(); this.expectedTypes = new ArrayList<>();
this.messageLevel = MessageLevel.Error; this.messageLevel = MessageLevel.Error;
this.fullValidate = true; // by default, full validation
} }
public Builder name(String name) { public Builder name(String name) {

View File

@ -40,11 +40,11 @@ public class ValidationPropertyProbe extends PropertyProbe {
protected final boolean fullValidate; // TODO: fullValidate protected final boolean fullValidate; // TODO: fullValidate
public ValidationPropertyProbe(Validation validation) { public ValidationPropertyProbe(Validation validation) {
this(ID, validation, false); this(ID, validation, true);
} }
public ValidationPropertyProbe(String id, Validation validation) { public ValidationPropertyProbe(String id, Validation validation) {
this(ID, validation, false); this(ID, validation, true);
} }
public ValidationPropertyProbe(Validation validation, boolean fullValidate) { public ValidationPropertyProbe(Validation validation, boolean fullValidate) {
@ -60,10 +60,10 @@ public class ValidationPropertyProbe extends PropertyProbe {
@Override @Override
protected ReportItems reportForNonExistentProperty(JsonNode node, RunContext ctx) { protected ReportItems reportForNonExistentProperty(JsonNode node, RunContext ctx) {
if (validation.isRequired()) { if (fullValidate && validation.isRequired()) {
return error("Required property " + validation.getName() + " not present in " + node.toPrettyString(), ctx); return error("Required property " + validation.getName() + " not present in " + node.toPrettyString(), ctx);
} else { } else {
// optional property // optional property or not doing full validation
return success(ctx); return success(ctx);
} }
} }

View File

@ -11,7 +11,7 @@ import org.oneedtech.inspect.vc.Validation;
*/ */
public class ValidationPropertyProbeFactory { public class ValidationPropertyProbeFactory {
public static ValidationPropertyProbe of(Validation validation) { public static ValidationPropertyProbe of(Validation validation) {
return of(validation, false); return of(validation, true);
} }
public static ValidationPropertyProbe of(Validation validation, boolean fullValidate) { public static ValidationPropertyProbe of(Validation validation, boolean fullValidate) {

View File

@ -39,6 +39,10 @@ public class ValidationRdfTypePropertyProbe extends ValidationPropertyProbe {
} }
} }
// if we're not doing a full validation and it's not and id field, pass
if (!fullValidate && !validation.getName().equals("id")) {
return success(ctx);
}
return error("Required property " + validation.getName() + " not present in " + node.toPrettyString(), ctx); return error("Required property " + validation.getName() + " not present in " + node.toPrettyString(), ctx);
} }

View File

@ -216,6 +216,15 @@ public class OB20Tests {
}); });
} }
@Test
void testAssertionWithLanguage() {
assertDoesNotThrow(()->{
Report report = validator.run(Samples.OB20.JSON.BASIC_WITH_LANGUAGE_JSON.asFileResource());
if(verbose) PrintHelper.print(report, true);
assertValid(report);
});
}
@Nested @Nested
static class WarningTests { static class WarningTests {
@BeforeAll @BeforeAll

View File

@ -81,6 +81,8 @@ public class Samples {
public final static Sample SIMPLE_EXPIRED_BEFORE_ISSUED_ASSERTION_JSON = new Sample("ob20/basic-assertion-expired-before-issued.json", true); public final static Sample SIMPLE_EXPIRED_BEFORE_ISSUED_ASSERTION_JSON = new Sample("ob20/basic-assertion-expired-before-issued.json", true);
// original: test_validation: test_assertion_not_issued_in_future // original: test_validation: test_assertion_not_issued_in_future
public final static Sample SIMPLE_FUTURE_ASSERTION_JSON = new Sample("ob20/basic-assertion-in-future.json", true); public final static Sample SIMPLE_FUTURE_ASSERTION_JSON = new Sample("ob20/basic-assertion-in-future.json", true);
// original: test_validate_related: test_validate_related_language
public final static Sample BASIC_WITH_LANGUAGE_JSON = new Sample("ob20/basic-assertion-with-language.json", true);
} }
public static final class PNG { public static final class PNG {

View File

@ -0,0 +1,15 @@
{
"@context": "https://w3id.org/openbadges/v2",
"type": "BadgeClass",
"id": "https://example.org/badgeclass-with-language.json",
"@language": "es",
"name": "Insignia fantastica",
"description": "For doing awesome things with robots that people think is pretty great.",
"image": "https://example.org/robotics-badge.png",
"criteria": "https://example.org/badgecriteria.json",
"issuer": "https://example.org/organization.json",
"related": {
"id": "https://example.org/robotics-badge.json",
"@language": "en-US"
}
}

View File

@ -0,0 +1,18 @@
{
"@context": "https://w3id.org/openbadges/v2",
"type": "Assertion",
"recipient": {
"type": "email",
"hashed": true,
"salt": "deadsea",
"identity": "sha256$ecf5409f3f4b91ab60cc5ef4c02aef7032354375e70cf4d8e43f6a1d29891942"
},
"id": "https://example.org/beths-robotics-badge.json",
"image": "https://example.org/beths-robot-badge.png",
"evidence": "https://example.org/beths-robot-work.html",
"issuedOn": "2016-12-31T23:59:59Z",
"badge": "https://example.org/badgeclass-with-language.json",
"verification": {
"type": "hosted"
}
}