Add the support for x-ms-secret property (#82)

* Add custom validator to support additional validation work

* Implement writeOnly property

* Update the code to switch back to use 'x-ms-secret'

* Update to use @ts-common/z-schema
This commit is contained in:
Renhe Li 2019-10-12 13:37:02 +08:00 коммит произвёл GitHub
Родитель 4fb8e6f1df
Коммит 906679ca6b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 60 добавлений и 10 удалений

Просмотреть файл

@ -29,8 +29,8 @@ var formatGenerators = require('./validation/format-generators');
var formatValidators = require('./validation/format-validators');
var customValidators = require('./validation/custom-zschema-validators');
var JsonRefs = require('json-refs');
var ZSchemaValidator = require('z-schema/src/JsonValidation');
var ZSchema = require('z-schema');
var ZSchemaValidator = require('@ts-common/z-schema/src/JsonValidation');
var ZSchema = require('@ts-common/z-schema');
// full-date from http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
var dateRegExp = new RegExp(
'^' +
@ -118,7 +118,8 @@ function createJSONValidator () {
var validator = new ZSchema({
breakOnFirstError: false,
ignoreUnknownFormats: true,
reportPathAsArray: true
reportPathAsArray: true,
customValidator: customValidators.customValidator
});
// Add the custom validators

Просмотреть файл

@ -1,7 +1,7 @@
'use strict';
var Report = require('z-schema/src/Report');
var ZSchemaValidator = require('z-schema/src/JsonValidation');
var Report = require('@ts-common/z-schema/src/Report');
var ZSchemaValidator = require('@ts-common/z-schema/src/JsonValidation');
function enumValidator (report, schema, json) {
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.1.2
@ -59,7 +59,12 @@ function requiredPropertyValidator (report, schema, json) {
xMsMutability = (schema.properties && schema.properties[`${requiredPropertyName}`]) && schema.properties[`${requiredPropertyName}`]['x-ms-mutability'];
// If a response has x-ms-mutability property and its missing the read we can skip this step
if (this.validateOptions && this.validateOptions.isResponse && xMsMutability && xMsMutability.indexOf('read') === -1) {
if (
this.validateOptions &&
this.validateOptions.isResponse &&
(xMsMutability &&
xMsMutability.indexOf('read') === -1)
) {
schema.properties[`${requiredPropertyName}`].isRequired = true;
continue;
}
@ -242,9 +247,53 @@ function readOnlyValidator (report, schema, json) {
}
}
function customValidatorFn (report, schema, json) {
if (shouldSkipValidate(this.validateOptions, ['SECRET_PROPERTY'])) {
return
}
var isResponse = this.validateOptions && this.validateOptions.isResponse
var xMsSecret = schema && schema['x-ms-secret']
if (isResponse && schema && xMsSecret && json !== undefined) {
let errorMessage = 'Secret property `"{0}": '
if (schema && schema.type === 'string' && typeof json === 'string') {
errorMessage += '"{1}"'
} else {
errorMessage += '{1}'
}
let propertyName = ''
if (schema.title && typeof schema.title === 'string') {
try {
let result = JSON.parse(schema.title)
if (Array.isArray(result.path) && result.path.length) {
propertyName = result.path[result.path.length - 1]
}
} catch (err) {
// do nothing
}
}
errorMessage += '`, cannot be sent in the response.'
report.addCustomError(
'SECRET_PROPERTY',
errorMessage,
[propertyName, 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;
module.exports.customValidator = customValidatorFn;

Просмотреть файл

@ -29,7 +29,7 @@ var helpers = require('../helpers');
var JsonRefs = require('json-refs');
var supportedHttpMethods = require('swagger-methods');
var swaggerSchema = require('swagger-schema-official/schema');
var jsonSymbol = require('z-schema').jsonSymbol;
var jsonSymbol = require('@ts-common/z-schema').jsonSymbol;
function getSchemaProperties (schema) {
var properties = _.keys(schema.properties); // Start with the defined properties

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "yasway",
"version": "1.8.4",
"version": "1.8.5",
"description": "A library that simplifies Swagger integrations.",
"main": "index.js",
"types": "index.d.ts",
@ -68,7 +68,7 @@
"dependencies": {
"@ts-common/json": "^0.3.0",
"@ts-common/string-map": "^0.3.0",
"z-schema": "^4.1.0",
"@ts-common/z-schema": "^4.1.2",
"debug": "^4.1.1",
"faker": "^4.1.0",
"js-base64": "^2.5.1",
@ -81,4 +81,4 @@
"swagger-methods": "^1.0.8",
"swagger-schema-official": "2.0.0-bab6bed"
}
}
}