add format scenarios

added support for:
date-time, date, float, double, and verification for byte, binary and password
This commit is contained in:
Luis Tejeda
2022-03-09 13:02:12 -06:00
parent 1d5b475cfa
commit adde11a1d5
7 changed files with 81 additions and 14 deletions

View File

@@ -100,7 +100,7 @@ function validateSchema (schema, valueToUse, options = {}, jsonSchemaDialect) {
return false;
}
if (validationError.keyword === 'type') {
if (validationError.keyword === 'type' || validationError.keyword === 'format') {
let schemaDataPath = formatDataPath(formatSchemaPathFromAJVErrorToConvertToDataPath(validationError.schemaPath)),
schemaToUse = schemaDataPath ? _.get(schema, schemaDataPath) : schema,
valueToValidate = dataPath ? _.get(valueToUse, dataPath) : valueToUse;

View File

@@ -45,13 +45,14 @@ function removeSharpAndSlashFromFirstPosition(schemaPath) {
}
/**
* Remove the type from the last position of a schema path
* Remove the defined word from the last position of a schema path
* @param {string} schemaPath - a defined schemaPath
* @param {string} word - word to remove
* @returns {string} - the schema path with type removed
*/
function removeTypeFromLastPosition(schemaPath) {
function removeWordFromLastPosition(schemaPath, word) {
let splittedDataPath = schemaPath.split('/');
if (splittedDataPath[splittedDataPath.length - 1] === 'type') {
if (splittedDataPath[splittedDataPath.length - 1] === word) {
splittedDataPath.splice(-1);
}
return splittedDataPath.join('/');
@@ -314,7 +315,8 @@ module.exports = {
* @returns {string} - The schemaPath with initial #/ and last "/type" removed
*/
formatSchemaPathFromAJVErrorToConvertToDataPath: function (schemaPath) {
return removeTypeFromLastPosition(removeSharpAndSlashFromFirstPosition(schemaPath));
return removeWordFromLastPosition(removeWordFromLastPosition(removeSharpAndSlashFromFirstPosition(schemaPath),
'type'), 'format');
},
typesMap,

View File

@@ -23,7 +23,9 @@ const _ = require('lodash'),
'regex',
'uuid',
'json-pointer',
'int64'
'int64',
'float',
'double'
],
DEFAULT_SCHEMA_UTILS = require('./30XUtils/schemaUtils30X');

View File

@@ -59,6 +59,9 @@ components:
age:
type: integer
format: int32
created_at:
type: string
format: date-time
isFavorite:
type: integer
format: int32
@@ -78,6 +81,27 @@ components:
type: string
tag:
type: string
created_at:
type: string
format: date-time
birthday:
type: string
format: date
floatField:
type: number
format: float
doubleField:
type: number
format: double
content:
format: byte
type: string
file:
type: string
format: binary
root_pass:
type: string
format: password
Pets:
type: array
items:

View File

@@ -201,8 +201,6 @@ describe('DEREF FUNCTION TESTS ', function() {
],
nonSupportedFormats = [
{ type: 'integer', format: 'int32' },
{ type: 'number', format: 'float' },
{ type: 'number', format: 'double' },
{ type: 'string', format: 'byte' },
{ type: 'string', format: 'binary' },
{ type: 'string', format: 'password' },

View File

@@ -263,4 +263,35 @@ describe('isTypeValue method', function () {
expect(result).to.be.false;
});
it('should return true when value is <dateTime> and type is string,' +
' and format is date-time', function () {
const result = isTypeValue('<dateTime>', { type: 'string', format: 'date-time' });
expect(result).to.be.true;
});
it('should return true when value is <date> and type is string,' +
' and format is date', function () {
const result = isTypeValue('<date>', { type: 'string', format: 'date' });
expect(result).to.be.true;
});
it('should return true when value is <float> and type is number,' +
' and format is float', function () {
const result = isTypeValue('<float>', { type: 'number', format: 'float' });
expect(result).to.be.true;
});
it('should return true when value is <double> and type is number,' +
' and format is double', function () {
const result = isTypeValue('<double>', { type: 'number', format: 'double' });
expect(result).to.be.true;
});
it('should return true when value is <double> and type is number,' +
' and format is double', function () {
const result = isTypeValue('<double>', { type: 'number', format: 'double' });
expect(result).to.be.true;
});
});

View File

@@ -69,12 +69,17 @@ describe('Validation with different resolution parameters options', function ()
let fileData = fs.readFileSync(path.join(__dirname, VALID_OPENAPI_FOLDER_PATH,
'/issue#479_2.yaml'), 'utf8'),
expectedRequestBody =
'{"data":[{"entityId":"<string>","user":{"id":"<long>","age":"<integer>"},' +
'{"data":[{"entityId":"<string>","user":{"id":"<long>","age":"<integer>","created_at":"<dateTime>"},' +
'"isFavorite":"<integer>","needThis":"<string>"},' +
'{"entityId":"<string>","user":{"id":"<long>","age":"<integer>"},' +
'{"entityId":"<string>","user":{"id":"<long>","age":"<integer>","created_at":"<dateTime>"},' +
'"isFavorite":"<integer>","needThis":"<string>"}]}',
expectedResponseBody =
'[{"id":"<long>","name":"<string>","tag":"<string>"},{"id":"<long>","name":"<string>","tag":"<string>"}]',
'[{"id":"<long>","name":"<string>","tag":"<string>","created_at":"<dateTime>","birthday":"<date>"' +
',"floatField":"<float>","doubleField":"<double>","content":"<byte>","file":"<binary>",' +
'"root_pass":"<password>"},' +
'{"id":"<long>","name":"<string>","tag":"<string>","created_at":"<dateTime>","birthday":"<date>"' +
',"floatField":"<float>","doubleField":"<double>","content":"<byte>","file":"<binary>",' +
'"root_pass":"<password>"}]',
options = {
requestParametersResolution: 'Schema',
exampleParametersResolution: 'Schema',
@@ -119,9 +124,9 @@ describe('Validation with different resolution parameters options', function ()
let fileData = fs.readFileSync(path.join(__dirname, VALID_OPENAPI_FOLDER_PATH,
'/issue#479_2.yaml'), 'utf8'),
expectedBody =
'{"data":[{"entityId":"<string>","user":{"id":"<long>","age":"<integer>"},' +
'{"data":[{"entityId":"<string>","user":{"id":"<long>","age":"<integer>","created_at":"<dateTime>"},' +
'"isFavorite":"<integer>","needThis":"<string>"},' +
'{"entityId":"<string>","user":{"id":"<long>","age":"<integer>"},' +
'{"entityId":"<string>","user":{"id":"<long>","age":"<integer>","created_at":"<dateTime>"},' +
'"isFavorite":"<integer>","needThis":"<string>"}]}',
options = {
requestParametersResolution: 'Schema',
@@ -165,7 +170,12 @@ describe('Validation with different resolution parameters options', function ()
let fileData = fs.readFileSync(path.join(__dirname, VALID_OPENAPI_FOLDER_PATH,
'/issue#479_2.yaml'), 'utf8'),
expectedResponseBody =
'[{"id":"<long>","name":"<string>","tag":"<string>"},{"id":"<long>","name":"<string>","tag":"<string>"}]',
'[{"id":"<long>","name":"<string>","tag":"<string>","created_at":"<dateTime>","birthday":"<date>"' +
',"floatField":"<float>","doubleField":"<double>","content":"<byte>","file":"<binary>",' +
'"root_pass":"<password>"},' +
'{"id":"<long>","name":"<string>","tag":"<string>","created_at":"<dateTime>","birthday":"<date>"' +
',"floatField":"<float>","doubleField":"<double>","content":"<byte>","file":"<binary>",' +
'"root_pass":"<password>"}]',
options = {
requestParametersResolution: 'Example',
exampleParametersResolution: 'Schema',