This repository has been 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
/
Copy pathValidationExample.js
66 lines (60 loc) · 2.04 KB
/
ValidationExample.js
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
var Promise = require("promise");
var amf = require("amf-client-js");
// We initialise AMF
amf.plugins.document.WebApi.register();
amf.plugins.features.AMFValidation.register();
amf.Core.init().then(function () {
// create a RAML parser
var parser = amf.Core.parser("RAML 1.0", "application/yaml");
// we parse the model and run validations
parser.parseFileAsync("file://examples/api.raml")
.then(function(model) { return validate("RAML", model) }) // Validating using the default RAML validation profile
.then(function(model) { return validate("OpenAPI", model) }) // Validating using the OpenAPI validatinp rofile
.then(function(model) { return validateCustom("ACME","file://examples/profile.raml", model) }) // Validating using a custom profile
.catch(function(err) {
console.log("Error validating")
console.log(err);
});
});
/*
In a standard validation we just need to provide the right value for one of the standard
supported profiles: RAML, OpenAPI or AMF
*/
function validate(profile, model) {
return new Promise(function(resolve, reject){
amf.Core.validate(model, profile, profile)
.then(function(report) {
printReport(profile, report)
resolve(model);
})
.catch(reject)
})
}
/*
In a custom validation, we first need to load the validation profile.
Then we can use the profile name to validate a parsed model.
*/
function validateCustom(dialect, profile, model) {
return new Promise(function(resolve, reject) {
amf.Core.loadValidationProfile(profile)
.then(function() {
amf.Core.validate(model, dialect, dialect)
.then(function(report) {
printReport(dialect, report)
resolve();
})
.catch(reject)
}).catch(reject)
})
}
function printReport(dialect, report) {
console.log(report.toString())
/*
if (report.conforms == true) {
console.log("Document validates profile " + dialect);
} else {
console.log("Document DOES NOT validate profile " + dialect);
console.log(report.toString());
}
*/
}