-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
ContractDefinitionRequestDto
JsonLD validator (#3159)
feat: contract definition validation
- Loading branch information
Showing
22 changed files
with
654 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
.../common/validator-core/src/main/java/org/eclipse/edc/validator/jsonobject/JsonWalker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.validator.jsonobject; | ||
|
||
import jakarta.json.JsonObject; | ||
|
||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Extract objects from JsonObject sub-path. | ||
*/ | ||
interface JsonWalker { | ||
|
||
/** | ||
* Extract a {@link Stream} of {@link JsonObject} from the path passed that can then be validated. | ||
* | ||
* @param object the {@link JsonObject}. | ||
* @param path the {@link JsonLdPath}. | ||
* @return a {@link Stream} of {@link JsonObject} can never be null. | ||
*/ | ||
Stream<JsonObject> extract(JsonObject object, JsonLdPath path); | ||
} |
56 changes: 56 additions & 0 deletions
56
...common/validator-core/src/main/java/org/eclipse/edc/validator/jsonobject/JsonWalkers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.validator.jsonobject; | ||
|
||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public enum JsonWalkers implements JsonWalker { | ||
|
||
ROOT_OBJECT { | ||
@Override | ||
public Stream<JsonObject> extract(JsonObject object, JsonLdPath path) { | ||
return Stream.of(object); | ||
} | ||
}, | ||
|
||
NESTED_OBJECT { | ||
@Override | ||
public Stream<JsonObject> extract(JsonObject object, JsonLdPath path) { | ||
var array = object.getJsonArray(path.last()); | ||
|
||
if (array == null) { | ||
return Stream.empty(); | ||
} else { | ||
return Stream.of(array.getJsonObject(0)); | ||
} | ||
} | ||
}, | ||
|
||
ARRAY_ITEMS { | ||
@Override | ||
public Stream<JsonObject> extract(JsonObject object, JsonLdPath path) { | ||
var array = object.getJsonArray(path.last()); | ||
|
||
if (array == null) { | ||
return Stream.empty(); | ||
} else { | ||
return array.stream().map(JsonValue::asJsonObject); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...or-core/src/main/java/org/eclipse/edc/validator/jsonobject/validators/MandatoryValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.validator.jsonobject.validators; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.validator.jsonobject.JsonLdPath; | ||
import org.eclipse.edc.validator.spi.ValidationResult; | ||
import org.eclipse.edc.validator.spi.Validator; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.lang.String.format; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; | ||
import static org.eclipse.edc.validator.spi.Violation.violation; | ||
|
||
/** | ||
* Verifies that a @value is present and not blank. | ||
*/ | ||
public class MandatoryValue implements Validator<JsonObject> { | ||
private final JsonLdPath path; | ||
|
||
public MandatoryValue(JsonLdPath path) { | ||
this.path = path; | ||
} | ||
|
||
@Override | ||
public ValidationResult validate(JsonObject input) { | ||
return Optional.ofNullable(input.getJsonArray(path.last())) | ||
.map(it -> it.getJsonObject(0)) | ||
.map(it -> it.getString(VALUE)) | ||
.filter(it -> !it.isBlank()) | ||
.map(it -> ValidationResult.success()) | ||
.orElseGet(() -> ValidationResult.failure(violation(format("mandatory value '%s' is missing or it is blank", path), path.toString()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.