forked from apigee-127/swagger-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
205 lines (167 loc) · 5.75 KB
/
Copy pathindex.js
File metadata and controls
205 lines (167 loc) · 5.75 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 2014 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// Module dependencies
var _ = require('lodash');
var fs = require('fs');
var jjv = require('jjv');
var jjve = require('jjve');
var validatorDefaults = {
useDefault: false,
useCoerce: false,
checkRequired: true,
removeAdditional: false
};
var throwUnsupportedVersion = function (version) {
throw new Error(version + ' is an unsupported Swagger specification version');
};
/**
* Creates a new Swagger specification object.
*
* @param {string} version - The Swagger version
* @param {object} [options] - The specification options (Currently used to pass validator options)
* @param {boolean} [options.useDefault=false] - If true it modifies the object to have the default values for missing
* non-required fields
* @param {boolean} [options.useCoerce=false] - If true it enables type coercion where defined
* @param {boolean} [options.checkRequired=true] - If true it reports missing required properties, otherwise it allows
* missing required properties
* @param {boolean} [options.removeAdditional=false] - If true it removes all attributes of an object which are not
* matched by the schema's specification
* @constructor
*/
var Specification = function Specification (version, options) {
var docsUrl;
var schemasUrl;
options = _.defaults(options || {}, validatorDefaults);
switch (version) {
case '1.2':
docsUrl = 'https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md';
schemasUrl = 'https://github.com/wordnik/swagger-spec/tree/master/schemas/v1.2';
break;
default:
throwUnsupportedVersion(version);
}
this.docsUrl = docsUrl;
this.options = options;
this.schemasUrl = schemasUrl;
this.version = version;
// Load the schema files
this.schemas = {};
fs.readdirSync('./schemas/' + version)
.filter(function (name) {
return name.match(/^(.*)\.json$/);
})
.forEach(function (name) {
this.schemas[name] = require('./schemas/' + version + '/' + name);
}.bind(this));
// Create the validators
this.validators = {};
switch (version) {
case '1.2':
Object.keys(this.schemas).forEach(function (schemaName) {
var validator = jjv();
var toCompile = [];
// Disable the 'uri' format checker as it's got issues: https://github.com/acornejo/jjv/issues/24
validator.addFormat('uri', function() {
return true;
});
// Since some schemas depend on others, bring them in appropriately
switch (schemaName) {
case 'apiDeclaration.json':
toCompile = [
'dataTypeBase.json',
'modelsObject.json',
'oauth2GrantType.json',
'authorizationObject.json',
'parameterObject.json',
'operationObject.json'
];
break;
case 'authorizationObject.json':
toCompile.push('oauth2GrantType.json');
break;
case 'modelsObject.json':
toCompile.push('dataTypeBase.json');
break;
case 'operationObject.json':
toCompile = [
'dataTypeBase.json',
'authorizationObject.json',
'oauth2GrantType.json',
'parameterObject.json'
];
break;
case 'parameterObject.json':
toCompile.push('dataTypeBase.json');
break;
case 'resourceListing.json':
toCompile = [
'resourceObject.json',
'infoObject.json',
'oauth2GrantType.json',
'authorizationObject.json'
];
break;
}
toCompile.push(schemaName);
toCompile.forEach(function (schemaName) {
this.schemas[schemaName].id = schemaName;
validator.addSchema(schemaName, this.schemas[schemaName]);
}.bind(this));
validator.je = jjve(validator);
this.validators[schemaName] = validator;
}.bind(this));
break;
}
};
/**
* Returns the result of the validation of the Swagger document against its schema.
*
* @param {object} data - The object representing the Swagger document/fragment
* @param {string} [schemaName='apiDeclaration.json'] - The schema name to use to validate the document/fragment
*
* @returns undefined if validation passes or an array of error objects
*/
Specification.prototype.validate = function (data, schemaName) {
if (_.isUndefined(data)) {
throw new Error('data is required');
} else if (!_.isObject(data)) {
throw new TypeError('data must be an object');
}
var schema;
var validator;
var result;
switch (this.version) {
case '1.2':
// Default to 'apiDeclaration.json'
schemaName = schemaName || 'apiDeclaration.json';
schema = this.schemas[schemaName];
break;
default:
throwUnsupportedVersion(this.version);
}
if (!schema) {
throw new Error('dataSchema is not valid. Valid schema names: ' + Object.keys(this.schemas).join(', '));
}
validator = this.validators[schemaName];
result = validator.validate(schema, data);
if (result) {
return validator.je(schema, data, result);
} else {
return undefined;
}
};
var v1_2 = module.exports.v1_2 = new Specification('1.2'); // jshint ignore:line