lib/validator.js
function preCast(value, parameterDefinition) {
	switch (parameterDefinition.type) {
		case 'array': {
			let values;

			switch (parameterDefinition.collectionFormat) {
				case 'ssv':
					values = value.split(' ');
					break;
				case 'tsv':
					values = value.split('\t');
					break;
				case 'pipes':
					values = value.split('|');
					break;
				case 'csv':
				default:
					values = value.split(',');
					break;
			}

			const result = values.map(v => {
				return preCast(v, parameterDefinition.items);
			});

			return result;
		}
		case 'boolean': {
			if (typeof (value) == 'string') {
				return value.toLowerCase() === 'true' || value.toLowerCase() === 'false' ?
					value.toLowerCase() === 'true' :
					value;
			} else {
				return value;
			}
		}
		case 'integer': {
			const result = Number(value);
			return Number.isInteger(result) ? result : value;
		}
		case 'number': {
			const result = Number(value);
			return Number.isNaN(result) ? value : result;
		}
		case 'object': {
			try {
				return JSON.parse(value);
			} catch (ex) {
				return value;
			}
		}
		default: {
			return value;
		}
	}
}
function preCast(value, parameterDefinition) {
	switch (parameterDefinition.type) {
		case 'array': {
			let values;

			switch (parameterDefinition.collectionFormat) {
				case 'ssv':
					values = value.split(' ');
					break;
				case 'tsv':
					values = value.split('\t');
					break;
				case 'pipes':
					values = value.split('|');
					break;
				case 'csv':
				default:
					values = value.split(',');
					break;
			}

			const result = values.map(v => {
				return preCast(v, parameterDefinition.items);
			});

			return result;
		}
		case 'boolean': {
			if (typeof (value) == 'string') {
				return value.toLowerCase() === 'true' || value.toLowerCase() === 'false' ?
					value.toLowerCase() === 'true' :
					value;
			} else {
				return value;
			}
		}
		case 'integer': {
			const result = Number(value);
			return Number.isInteger(result) ? result : value;
		}
		case 'number': {
			const result = Number(value);
			return Number.isNaN(result) ? value : result;
		}
		case 'object': {
			try {
				return JSON.parse(value);
			} catch (ex) {
				return value;
			}
		}
		default: {
			return value;
		}
	}
}
function preCast ( value , parameterDefinition ) { switch ( parameterDefinition . type ) { case 'array' : { let values ; switch ( parameterDefinition . collectionFormat ) { case 'ssv' : values = value . split ( ' ' ) ; break ; case 'tsv' : values = value . split ( '\t' ) ; break ; case 'pipes' : values = value . split ( '|' ) ; break ; case 'csv' : default : values = value . split ( ',' ) ; break ; } const result = values . map ( v => { return preCast ( v , parameterDefinition . items ) ; } ) ; return result ; } case 'boolean' : { if ( typeof ( value ) == 'string' ) { return value . toLowerCase ( ) === 'true' || value . toLowerCase ( ) === 'false' ? value . toLowerCase ( ) === 'true' : value ; } else { return value ; } } case 'integer' : { const result = Number ( value ) ; return Number . isInteger ( result ) ? result : value ; } case 'number' : { const result = Number ( value ) ; return Number . isNaN ( result ) ? value : result ; } case 'object' : { try { return JSON . parse ( value ) ; } catch ( ex ) { return value ; } } default : { return value ; } } }
Casts raw values from the request before they are validated
Casts raw values from the request before they are validated