function SwaggerParser() { $RefParser.apply(this, arguments); }
n/a
bundle = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().bundle(schema, options, callback); }
n/a
dereference = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().dereference(schema, options, callback); }
n/a
function ParserOptions(options) { this.validate = { schema: true, spec: true }; $RefParserOptions.apply(this, arguments); }
n/a
parse = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().parse(schema, options, callback); }
n/a
resolve = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().resolve(schema, options, callback); }
n/a
function $RefParser() {
/**
* The parsed (and possibly dereferenced) JSON schema object
*
* @type {object}
* @readonly
*/
this.schema = null;
/**
* The resolved JSON references
*
* @type {$Refs}
*/
this.$refs = new $Refs();
/**
* The file path or URL of the main JSON schema file.
* This will be empty if the schema was passed as an object rather than a path.
*
* @type {string}
* @protected
*/
this._basePath = '';
}
n/a
validate = function (api, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().validate(api, options, callback); }
...
- [Swagger Express Middleware](https://github.com/BigstickCarpet/swagger-express-middleware)
Example
--------------------------
```javascript
SwaggerParser.validate(myAPI, function(err, api) {
if (err) {
console.error(err);
}
else {
console.log("API name: %s, Version: %s", api.info.title, api.info.version);
}
});
...
function yamlParse(text, reviver) { try { return yaml.safeLoad(text); } catch (e) { if (e instanceof Error) { throw e; } else { // https://github.com/nodeca/js-yaml/issues/153 throw ono(e, e.message); } } }
n/a
function yamlStringify(value, replacer, space) { try { var indent = (typeof space === 'string' ? space.length : space) || 2; return yaml.safeDump(value, {indent: indent}); } catch (e) { if (e instanceof Error) { throw e; } else { // https://github.com/nodeca/js-yaml/issues/153 throw ono(e, e.message); } } }
n/a
function ParserOptions(options) { this.validate = { schema: true, spec: true }; $RefParserOptions.apply(this, arguments); }
n/a
function $RefParserOptions(options) {
/**
* Determines what types of files can be parsed
*/
this.allow = {
/**
* Are JSON files allowed?
* If false, then all schemas must be in YAML format.
* @type {boolean}
*/
json: true,
/**
* Are YAML files allowed?
* If false, then all schemas must be in JSON format.
* @type {boolean}
*/
yaml: true,
/**
* Are zero-byte files allowed?
* If false, then an error will be thrown if a file is empty.
* @type {boolean}
*/
empty: true,
/**
* Can unknown file types be $referenced?
* If true, then they will be parsed as Buffers (byte arrays).
* If false, then an error will be thrown.
* @type {boolean}
*/
unknown: true
};
/**
* Determines the types of JSON references that are allowed.
*/
this.$refs = {
/**
* Allow JSON references to other parts of the same file?
* @type {boolean}
*/
internal: true,
/**
* Allow JSON references to external files/URLs?
* @type {boolean}
*/
external: true,
/**
* Allow circular (recursive) JSON references?
* If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
* If "ignore", then circular references will not be dereferenced.
* @type {boolean|string}
*/
circular: true
};
/**
* How long to cache files (in seconds).
*/
this.cache = {
/**
* How long to cache local files, in seconds.
* @type {number}
*/
fs: 60, // 1 minute
/**
* How long to cache files downloaded via HTTP, in seconds.
* @type {number}
*/
http: 5 * 60, // 5 minutes
/**
* How long to cache files downloaded via HTTPS, in seconds.
* @type {number}
*/
https: 5 * 60 // 5 minutes
};
merge(options, this);
}
n/a
function $RefParser() {
/**
* The parsed (and possibly dereferenced) JSON schema object
*
* @type {object}
* @readonly
*/
this.schema = null;
/**
* The resolved JSON references
*
* @type {$Refs}
*/
this.$refs = new $Refs();
/**
* The file path or URL of the main JSON schema file.
* This will be empty if the schema was passed as an object rather than a path.
*
* @type {string}
* @protected
*/
this._basePath = '';
}
n/a
bundle = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().bundle(schema, options, callback); }
n/a
dereference = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().dereference(schema, options, callback); }
n/a
parse = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().parse(schema, options, callback); }
n/a
resolve = function (schema, options, callback) { var Class = this; // eslint-disable-line consistent-this return new Class().resolve(schema, options, callback); }
n/a
bundle = function (schema, options, callback) { var me = this; var args = normalizeArgs(arguments); return this.resolve(args.schema, args.options) .then(function() { bundle(me, args.options); return maybe(args.callback, Promise.resolve(me.schema)); }) .catch(function(err) { return maybe(args.callback, Promise.reject(err)); }); }
n/a
dereference = function (schema, options, callback) { var me = this; var args = normalizeArgs(arguments); return this.resolve(args.schema, args.options) .then(function() { dereference(me, args.options); return maybe(args.callback, Promise.resolve(me.schema)); }) .catch(function(err) { return maybe(args.callback, Promise.reject(err)); }); }
n/a
parse = function (schema, options, callback) { var args = normalizeArgs(arguments); if (args.schema && typeof args.schema === 'object') { // The schema is an object, not a path/url this.schema = args.schema; this._basePath = ''; var $ref = new $Ref(this.$refs, this._basePath); $ref.setValue(this.schema, args.options); return maybe(args.callback, Promise.resolve(this.schema)); } if (!args.schema || typeof args.schema !== 'string') { var err = ono('Expected a file path, URL, or object. Got %s', args.schema); return maybe(args.callback, Promise.reject(err)); } var me = this; // Resolve the absolute path of the schema args.schema = util.path.localPathToUrl(args.schema); args.schema = url.resolve(util.path.cwd(), args.schema); this._basePath = util.path.stripHash(args.schema); // Read the schema file/url return read(args.schema, this.$refs, args.options) .then(function(cached$Ref) { var value = cached$Ref.$ref.value; if (!value || typeof value !== 'object' || value instanceof Buffer) { throw ono.syntax('"%s" is not a valid JSON Schema', me._basePath); } else { me.schema = value; return maybe(args.callback, Promise.resolve(me.schema)); } }) .catch(function(e) { return maybe(args.callback, Promise.reject(e)); }); }
n/a
resolve = function (schema, options, callback) { var me = this; var args = normalizeArgs(arguments); return this.parse(args.schema, args.options) .then(function() { return resolve(me, args.options); }) .then(function() { return maybe(args.callback, Promise.resolve(me.$refs)); }) .catch(function(err) { return maybe(args.callback, Promise.reject(err)); }); }
n/a
function debug() { // disabled? if (!debug.enabled) return; var self = debug; // set `diff` timestamp var curr = +new Date(); var ms = curr - (prevTime || curr); self.diff = ms; self.prev = prevTime; self.curr = curr; prevTime = curr; // turn the `arguments` into a proper Array var args = new Array(arguments.length); for (var i = 0; i < args.length; i++) { args[i] = arguments[i]; } args[0] = exports.coerce(args[0]); if ('string' !== typeof args[0]) { // anything else let's inspect with %O args.unshift('%O'); } // apply any `formatters` transformations var index = 0; args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) { // if we encounter an escaped % then don't increase the array index if (match === '%%') return match; index++; var formatter = exports.formatters[format]; if ('function' === typeof formatter) { var val = args[index]; match = formatter.call(self, val); // now we need to remove `args[index]` since it's inlined in the `format` args.splice(index, 1); index--; } return match; }); // apply env-specific formatting (colors, etc.) exports.formatArgs.call(self, args); var logFn = debug.log || exports.log || console.log.bind(console); logFn.apply(self, args); }
...
/**
* Validates the given Swagger API against the Swagger 2.0 schema.
*
* @param {SwaggerObject} api
*/
function validateSchema(api) {
util.debug('Validating against the Swagger 2.0 schema');
// Validate the API against the Swagger schema
var isValid = ZSchema.validate(api, swaggerSchema);
if (isValid) {
util.debug(' Validated successfully');
}
...
format = function (f) { if (typeof f !== 'string') { const objects = new Array(arguments.length); for (var index = 0; index < arguments.length; index++) { objects[index] = inspect(arguments[index]); } return objects.join(' '); } var argLen = arguments.length; if (argLen === 1) return f; var str = ''; var a = 1; var lastPos = 0; for (var i = 0; i < f.length;) { if (f.charCodeAt(i) === 37/*'%'*/ && i + 1 < f.length) { switch (f.charCodeAt(i + 1)) { case 100: // 'd' if (a >= argLen) break; if (lastPos < i) str += f.slice(lastPos, i); str += Number(arguments[a++]); lastPos = i = i + 2; continue; case 106: // 'j' if (a >= argLen) break; if (lastPos < i) str += f.slice(lastPos, i); str += tryStringify(arguments[a++]); lastPos = i = i + 2; continue; case 115: // 's' if (a >= argLen) break; if (lastPos < i) str += f.slice(lastPos, i); str += String(arguments[a++]); lastPos = i = i + 2; continue; case 37: // '%' if (lastPos < i) str += f.slice(lastPos, i); str += '%'; lastPos = i = i + 2; continue; } } ++i; } if (lastPos === 0) str = f; else if (lastPos < f.length) str += f.slice(lastPos); while (a < argLen) { const x = arguments[a++]; if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) { str += ' ' + x; } else { str += ' ' + inspect(x); } } return str; }
...
* @param {string} [indent] - The whitespace used to indent the error message
* @returns {string}
*/
function formatZSchemaError(errors, indent) {
indent = indent || ' ';
var message = '';
errors.forEach(function(error, index) {
message += util.format('%s%s at #/%s\n', indent, error.message, error.path
.join('/'));
if (error.inner) {
message += formatZSchemaError(error.inner, indent + ' ');
}
});
return message;
}
...
inherits = function (ctor, superCtor) { if (ctor === undefined || ctor === null) throw new TypeError('The constructor to "inherits" must not be ' + 'null or undefined'); if (superCtor === undefined || superCtor === null) throw new TypeError('The super constructor to "inherits" must not ' + 'be null or undefined'); if (superCtor.prototype === undefined) throw new TypeError('The super constructor to "inherits" must ' + 'have a prototype'); ctor.super_ = superCtor; Object.setPrototypeOf(ctor.prototype, superCtor.prototype); }
...
schema: true,
spec: true
};
$RefParserOptions.apply(this, arguments);
}
util.inherits(ParserOptions, $RefParserOptions);
...