зеркало из https://github.com/Azure/sway.git
Add support for validating readOnly properties in the request payload
This commit is contained in:
Родитель
d535b41dc0
Коммит
02b56f9399
|
@ -113,6 +113,7 @@ function createJSONValidator () {
|
|||
ZSchemaValidator.JsonValidators.type = customValidators.typeValidator;
|
||||
ZSchemaValidator.JsonValidators.required = customValidators.requiredPropertyValidator;
|
||||
ZSchemaValidator.JsonValidators.oneOf = customValidators.oneOf;
|
||||
ZSchemaValidator.JsonValidators.readOnly = customValidators.readOnlyValidator;
|
||||
|
||||
var validator = new ZSchema({
|
||||
breakOnFirstError: false,
|
||||
|
|
|
@ -205,8 +205,31 @@ function shouldSkipValidate (options, errors) {
|
|||
});
|
||||
}
|
||||
|
||||
function readOnlyValidator (report, schema, json) {
|
||||
var isResponse = this.validateOptions && this.validateOptions.isResponse;
|
||||
|
||||
if (!isResponse && schema && schema.readOnly && json) {
|
||||
let errorMessage = 'ReadOnly property `"{0}": ';
|
||||
|
||||
if (schema && schema.type === 'string') {
|
||||
errorMessage += '"{1}"';
|
||||
} else {
|
||||
errorMessage += '{1}';
|
||||
}
|
||||
errorMessage += '`, cannot be sent in the request.';
|
||||
report.addCustomError(
|
||||
'READONLY_PROPERTY_NOT_ALLOWED_IN_REQUEST',
|
||||
errorMessage,
|
||||
[report.parentReport.path[0], json],
|
||||
null,
|
||||
schema
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.shouldSkipValidate = shouldSkipValidate;
|
||||
module.exports.enumValidator = enumValidator;
|
||||
module.exports.requiredPropertyValidator = requiredPropertyValidator;
|
||||
module.exports.typeValidator = typeValidator;
|
||||
module.exports.oneOf = oneOf;
|
||||
module.exports.readOnlyValidator = readOnlyValidator;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "yasway",
|
||||
"version": "1.7.2",
|
||||
"version": "1.7.3",
|
||||
"description": "A library that simplifies Swagger integrations.",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
|
@ -81,4 +81,4 @@
|
|||
"swagger-methods": "^1.0.8",
|
||||
"swagger-schema-official": "2.0.0-bab6bed"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче