diff --git a/lib/helpers.js b/lib/helpers.js index 28671bf..3f6b382 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -179,7 +179,8 @@ var convertValue = module.exports.convertValue = function (schema, options, valu var isDateTime; // If there is an explicit type provided, make sure it's one of the supported ones - if (_.has(schema, 'type') && types.indexOf(type) === -1) { + if (_.has(schema, 'type') && (((typeof type.valueOf() === 'string' && types.indexOf(type) === -1) || + (Array.isArray(type) && type.some((t) => { return types.indexOf(t) === -1; }))))) { throw new TypeError('Invalid \'type\' value: ' + type); } @@ -209,15 +210,27 @@ var convertValue = module.exports.convertValue = function (schema, options, valu } // Attempt to parse the string as JSON if the type is array or object - if (['array', 'object'].indexOf(type) > -1) { + if (type && type.valueOf() === 'string' && ['array', 'object'].indexOf(type) > -1) { try { value = JSON.parse(value); } catch (err) { // Nothing to do here, just fall through } } - - switch (type) { + let typeToCheck = type; + if (type && Array.isArray(type)) { + if (Array.isArray(value) && type.indexOf('array') !== -1) { + typeToCheck = 'array'; + } else if (value && typeof value.valueOf() === 'string' && type.indexOf('string') !== -1) { + typeToCheck = 'string'; + if (options.collectionFormat) typeToCheck = 'array'; + } else if (value && typeof value === 'number' && type.indexOf('number') !== -1) { + typeToCheck = 'number'; + } else if (value && typeof value === 'boolean' && type.indexOf('boolean') !== -1) { + typeToCheck = 'boolean'; + } + } + switch (typeToCheck) { case 'array': if (_.isString(value)) { if (collectionFormats.indexOf(options.collectionFormat) === -1) {