This repository was archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidationExample.java
More file actions
67 lines (52 loc) · 2.44 KB
/
ValidationExample.java
File metadata and controls
67 lines (52 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.mulesoft.amf.examples;
import amf.core.client.Parser;
import amf.validation.AMFValidationReport;
import amf.model.document.BaseUnit;
import java.util.concurrent.ExecutionException;
import static java.lang.System.out;
/**
* Validation example, parsing and then validating using different profiles
*/
public class ValidationExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// We initialise AMF
amf.plugins.document.WebApi.register(); // we need the WebAPI plugin to parse RAML specs
amf.plugins.features.AMFValidation.register(); // we need the AMF Validation plugin for custom validations
amf.Core.init().get(); // we block here
// Create a RAML parser
final Parser parser = amf.Core.parser("RAML 1.0", "application/yaml");
// We parse the model
final BaseUnit model = parser.parseFileAsync("file://examples/api.raml").get();
// Validating using the default RAML validation profile
validate("RAML", model);
// Validating using the default OpenAPI validation profile
validate("OpenAPI", model);
// Custom profile validation
validateCustom("ACME", "file://examples/profile.raml", model);
}
/*
In a standard validation we just need to provide the right value for one of the standard
supported profiles: RAML, OpenAPI or AMF
*/
private static void validate(String profile, BaseUnit model) throws ExecutionException, InterruptedException {
final AMFValidationReport report = amf.Core.validate(model, profile, profile).get();
printReport(profile, report);
}
/*
In a custom validation, we first need to load the validation profile.
Then we can use the profile name to validate a parsed model.
*/
private static void validateCustom(String dialect, String profile, BaseUnit model) throws ExecutionException, InterruptedException {
amf.Core.loadValidationProfile(profile).get();
final AMFValidationReport report = amf.Core.validate(model, dialect, "AMF").get();
printReport(dialect, report);
}
private static void printReport(String dialect, AMFValidationReport report) {
if (report.conforms()) {
out.println("Document validates profile " + dialect);
} else {
out.println("Document DOES NOT validate profile " + dialect);
out.println(report);
}
}
}