fastman
Version:
快速api测试及文档生成
207 lines • 8.04 kB
JavaScript
;
/**
* @license
* Copyright 2017 Red Hat
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Base class for all validation rules.
*/
var OasValidationRuleUtil = /** @class */ (function () {
function OasValidationRuleUtil() {
}
/**
* Check if a property was defined.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isDefined = function (propertyValue) {
if (propertyValue === undefined) {
return false;
}
else {
return true;
}
};
/**
* Check if the property value exists (is not undefined and is not null).
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.hasValue = function (propertyValue) {
if (propertyValue === undefined) {
return false;
}
if (propertyValue === null) {
return false;
}
return true;
};
/**
* Returns true only if the given value is a valid URL.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidUrl = function (propertyValue) {
var urlRegex = '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$';
var url = new RegExp(urlRegex, 'i');
return propertyValue.length < 2083 && url.test(propertyValue);
};
/**
* Returns true only if the given value is a valid URL template.
* @param {string} propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidUrlTemplate = function (propertyValue) {
// TODO is there a regular expression we can use to validate a URL template??
return true;
};
/**
* Returns true only if the given value is valid GFM style markup.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidGFM = function (propertyValue) {
// TODO implement a regexp to test for a valid Github Flavored Markdown string
return true;
};
/**
* Returns true only if the given value is valid CommonMark style markup.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidCommonMark = function (propertyValue) {
// TODO implement a regexp to test for a valid CommonMark string
return true;
};
/**
* Returns true only if the given value is a valid email address.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidEmailAddress = function (propertyValue) {
var email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return email.test(propertyValue);
};
/**
* Returns true only if the given value is a valid mime-type.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidMimeType = function (propertyValue) {
var mt = /^.*\/.*(;.*)?$/;
for (var _i = 0, propertyValue_1 = propertyValue; _i < propertyValue_1.length; _i++) {
var v = propertyValue_1[_i];
if (!mt.test(v)) {
return false;
}
}
return true;
};
/**
* Returns true if the given value is an item in the enum list.
* @param value
* @param items
*/
OasValidationRuleUtil.isValidEnumItem = function (value, items) {
return items.indexOf(value) != -1;
};
/**
* Resolves a reference from a relative position in the data model.
* @param $ref
* @param from
*/
OasValidationRuleUtil.resolveRef = function ($ref, from) {
var _this = this;
// TODO implement a proper reference resolver including external file resolution: https://github.com/EricWittmann/oai-ts-core/issues/8
var split = $ref.split("/");
var cnode = null;
split.forEach(function (seg) {
if (seg === "#") {
cnode = from.ownerDocument();
}
else if (_this.hasValue(cnode)) {
if (cnode["__instanceof_IOasIndexedNode"]) {
cnode = cnode["getItem"](seg);
}
else {
cnode = cnode[seg];
}
}
});
return cnode;
};
/**
* Returns true only if the given reference can be resolved relative to the given document. Examples
* of $ref values include:
*
* #/definitions/ExampleDefinition
* #/parameters/fooId
* #/responses/NotFoundResponse
*
* @param $ref
* @param oasDocument
*/
OasValidationRuleUtil.canResolveRef = function ($ref, from) {
// Don't try to resolve e.g. external references.
if ($ref.indexOf('#/') !== 0) {
return true;
}
return this.hasValue(OasValidationRuleUtil.resolveRef($ref, from));
};
/**
* Returns true only if the given value is a valid host.
* @param propertyValue
* @return {boolean}
*/
OasValidationRuleUtil.isValidHost = function (propertyValue) {
// TODO implement a regexp to test for a valid host plus optional port
if (propertyValue.indexOf("http:") === 0 || propertyValue.indexOf("https:") === 0) {
return false;
}
return true;
};
/**
* Returns true if the given value is valid according to the schema provided.
* @param value
* @param node
*/
OasValidationRuleUtil.isValidForType = function (value, node) {
// TODO validate the value against the schema
return true;
};
/**
* Returns true if the given status code is a valid HTTP response code.
* @param statusCode
* @return {boolean}
*/
OasValidationRuleUtil.isValidHttpCode = function (statusCode) {
return OasValidationRuleUtil.HTTP_STATUS_CODES.indexOf(statusCode) != -1;
};
/**
* List of valid HTTP response status codes from: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
*/
OasValidationRuleUtil.HTTP_STATUS_CODES = [
"100", "101", "102", "1XX", "10X",
"200", "201", "202", "203", "204", "205", "206", "207", "208", "226", "2XX", "20X", "21X", "22X",
"300", "301", "302", "303", "304", "305", "306", "307", "308", "3XX", "30X",
"400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417", "4XX", "40X", "41X",
"421", "422", "423", "424", "426", "427", "428", "429", "431", "451", "42X", "43X", "44X", "45X",
"500", "501", "502", "503", "504", "505", "506", "507", "508", "510", "511", "5XX", "50X", "51X"
];
return OasValidationRuleUtil;
}());
exports.OasValidationRuleUtil = OasValidationRuleUtil;
//# sourceMappingURL=validation.js.map