use of io.apicurio.datamodels.core.models.ValidationProblem in project apicurio-data-models by Apicurio.
the class Library method validateDocument.
/**
* Called to validate a data model node. All validation rules will be evaluated and reported. The list
* of validation problems found during validation is returned. In addition, validation problems will be
* reported on the individual nodes themselves. Validation problem severity is determined by checking
* with the included severity registry. If the severity registry is null, a default registry is used.
* Custom validators can be passed to provide additional validation rules beyond what this Library offers out of the box.
*
* @param node The document to be validated
* @param severityRegistry Supply a custom severity registry. If nothing is passed, the default severity registry will be used
* @param extensions Supply an optional list of validation extensions, enabling the use of 3rd-party validators or custom validation rules
* @return full list of the validation problems found in the document
*/
public static CompletableFuture<List<ValidationProblem>> validateDocument(Node node, IValidationSeverityRegistry severityRegistry, List<IDocumentValidatorExtension> extensions) {
List<ValidationProblem> totalValidationProblems = Library.validate(node, severityRegistry);
if (extensions != null && !extensions.isEmpty()) {
for (IDocumentValidatorExtension extension : extensions) {
CompletableFuture<List<ValidationProblem>> extensionValidationProblems = extension.validateDocument(node);
extensionValidationProblems.thenAccept(problems -> problems.forEach(p -> {
totalValidationProblems.add(p);
node.ownerDocument().addValidationProblem(p.errorCode, p.nodePath, p.property, p.message, p.severity);
}));
}
}
return CompletableFuture.completedFuture(totalValidationProblems);
}
use of io.apicurio.datamodels.core.models.ValidationProblem in project apicurio-data-models by Apicurio.
the class TestValidationExtension method validateDocument.
@Override
public CompletableFuture<List<ValidationProblem>> validateDocument(Node document) {
List<ValidationProblem> fakeProblems = new ArrayList<>();
fakeProblems.add(new ValidationProblem("TEST-001", new NodePath("testpath"), "test", "Test problem", ValidationProblemSeverity.low));
return CompletableFuture.completedFuture(fakeProblems);
}
use of io.apicurio.datamodels.core.models.ValidationProblem in project apicurio-data-models by Apicurio.
the class TestValidationExtension method testValidate.
@Test
public void testValidate() throws Exception {
String jsonString = "{\r\n" + " \"openapi\": \"3.0.9\",\r\n" + " \"info\": {\r\n" + " \"title\": \"Very Simple API\",\r\n" + " \"version\": \"1.0.0\"\r\n" + " }\r\n" + "}";
Document doc = Library.readDocumentFromJSONString(jsonString);
List<ValidationProblem> problems = Library.validateDocument(doc, null, null).get();
Assert.assertTrue(problems.size() > 0);
}
use of io.apicurio.datamodels.core.models.ValidationProblem in project apicurio-data-models by Apicurio.
the class TestValidationExtension method testValidateDocumentWithCustomExtension.
@Test
public void testValidateDocumentWithCustomExtension() {
String jsonString = "{\r\n" + " \"openapi\": \"3.0.9\",\r\n" + " \"info\": {\r\n" + " \"title\": \"Very Simple API\",\r\n" + " \"version\": \"1.0.0\"\r\n" + " }\r\n" + "}";
Document doc = Library.readDocumentFromJSONString(jsonString);
List<IDocumentValidatorExtension> extensions = new ArrayList<>();
extensions.add(new TestValidationExtension());
CompletableFuture<List<ValidationProblem>> problems = Library.validateDocument(doc, null, extensions);
problems.whenCompleteAsync((validationProblems, done) -> {
List<String> expectedErrorCodes = Arrays.asList("R-003", "TEST-001");
List<String> documentErrorCodes = doc.getValidationProblemCodes();
Assert.assertEquals(expectedErrorCodes, documentErrorCodes);
List<String> problemErrorCodes = validationProblems.stream().map(p -> p.errorCode).collect(Collectors.toList());
Assert.assertEquals(expectedErrorCodes, problemErrorCodes);
});
}
use of io.apicurio.datamodels.core.models.ValidationProblem in project apicurio-data-models by Apicurio.
the class ValidationTestRunner method runChild.
/**
* @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
*/
@Override
protected void runChild(ValidationTestCase child, RunNotifier notifier) {
Description description = this.describeChild(child);
Statement statement = new Statement() {
@Override
public void evaluate() throws Throwable {
String testCP = "fixtures/validation/" + child.getTest();
URL testUrl = Thread.currentThread().getContextClassLoader().getResource(testCP);
Assert.assertNotNull("Could not load test resource: " + testCP, testUrl);
// Read the test source
String original = loadResource(testUrl);
Assert.assertNotNull(original);
// Parse into a Json object
Object originalParsed = mapper.readTree(original);
// Parse into a data model
Document doc = Library.readDocument(originalParsed);
// Validate the document
IValidationSeverityRegistry severityRegistry = null;
if (child.getSeverity() != null) {
final ValidationProblemSeverity severity = ValidationProblemSeverity.valueOf(child.getSeverity());
severityRegistry = new IValidationSeverityRegistry() {
@Override
public ValidationProblemSeverity lookupSeverity(ValidationRuleMetaData rule) {
return severity;
}
};
}
List<ValidationProblem> problems = Library.validateDocument(doc, severityRegistry, Collections.emptyList()).get();
// Now compare with expected
String actual = formatProblems(problems);
String expectedCP = testCP + ".expected";
URL expectedUrl = Thread.currentThread().getContextClassLoader().getResource(expectedCP);
Assert.assertNotNull("Could not load test resource: " + expectedCP, expectedUrl);
String expected = loadResource(expectedUrl);
Assert.assertEquals(normalize(expected), normalize(actual));
}
};
runLeaf(statement, description, notifier);
}
Aggregations