-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
49 lines (41 loc) · 1.41 KB
/
index.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
'use strict';
var Promise = require('bluebird');
var Validator = require('jsonschema').Validator;
function validate(doc, schema, options) {
return new Promise(function (resolve, reject) {
options = options || {};
options.throwError = false;
try {
var obj = doc;
if (typeof doc === 'string' || doc instanceof String) {
obj = JSON.parse(doc);
}
var v = new Validator();
v.customFormats.payloadFormat = function (input) {
return input.cards;
};
var result = v.validate(obj, schema, options);
if (result.valid) {
resolve(obj);
} else {
reject(result.errors);
}
} catch (err) {
reject([{ // eslint-disable-line prefer-promise-reject-errors
property: options.propertyName ? options.propertyName : 'instance',
message: err.message
}]);
}
});
}
module.exports = {
Card: function (str, options) {
return validate(str, require('./lib/card-schema'), options);
},
DiscoveryResponse: function (str, options) {
return validate(str, require('./lib/discovery-response-schema'), options);
},
ServiceRequest: function (str, options) {
return validate(str, require('./lib/service-request-schema.js'), options);
}
};