Created
April 18, 2019 00:16
-
-
Save droidMakk/5e6a7999f21f03de06360977db8d6a5e to your computer and use it in GitHub Desktop.
Simple Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function validator(requestObject, validatingObj){ | |
let paramsError = { | |
missingParams: [], | |
invalidParams: [], | |
message: '' | |
} | |
function constructMessage(_paramsError){ | |
let missingParamsMsg = `Missing the following parameters ${_paramsError.missingParams.join()}`; | |
let invalidParamsMsg = `The Following parameters are invalid ${_paramsError.invalidParams.join()}`; | |
return missingParamsMsg + '. ' + invalidParamsMsg; | |
} | |
await Object.keys(validatingObj).map(validParam => { | |
Object.keys(requestObject).includes(validParam) | |
? typeof validatingObj[validParam] === typeof requestObject[validParam] ? | |
'' : paramsError.invalidParams.push(validParam) : paramsError.missingParams.push(validParam); | |
}) | |
paramsError.message = constructMessage(paramsError); | |
return paramsError; | |
} | |
let requestObject = { username: 'one', mailid: 123123, rides: [] }; | |
let validatingObj = { | |
username: "", | |
mobile: 0, | |
mailid: "", | |
age: 0, | |
rides: { driverid: 0, carModel: '' } | |
} | |
validator(requestObject, validatingObj).then(paramsError => { | |
console.info('paramsError',paramsError); | |
}).catch(err => { | |
console.error("\nError",err) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function validator(requestObject, validatingObj){ | |
let paramsError = { | |
missingParams: [], | |
invalidParams: [], | |
message: '' | |
} | |
function constructMessage(_paramsError){ | |
let missingParamsMsg = `Missing the following parameters ${_paramsError.missingParams.join()}`; | |
let invalidParamsMsg = `The Following parameters are invalid ${_paramsError.invalidParams.join()}`; | |
return missingParamsMsg + '. ' + invalidParamsMsg; | |
} | |
async function checkType(comparator, value){ | |
if(typeof comparator === 'object'){ | |
if(Array.isArray(comparator) && Array.isArray(value)){ | |
return true; | |
} else { | |
if(!Array.isArray(comparator)){ | |
if(!Array.isArray(value)){ | |
console.info(await validator(comparator, value)) | |
} else { | |
return false; | |
} | |
} | |
} | |
} else { | |
return true; | |
} | |
} | |
try{ | |
await Object.keys(validatingObj).map(validParam => { | |
Object.keys(requestObject).includes(validParam) | |
? typeof validatingObj[validParam] === typeof requestObject[validParam] | |
? await checkType(validatingObj[validParam], requestObject[validParam]) ? | |
'' : '' : paramsError.invalidParams.push(validParam) : paramsError.missingParams.push(validParam); | |
}) | |
} catch(err) { | |
console.error('Some error',err) | |
} | |
paramsError.message = constructMessage(paramsError); | |
return paramsError; | |
} | |
let requestObject = { username: 'one', mailid: 123123, rides: [] }; | |
let validatingObj = { | |
username: "", | |
mobile: 0, | |
mailid: "", | |
age: 0, | |
rides: { driverid: 0, carModel: '' } | |
} | |
validator(requestObject, validatingObj).then(paramsError => { | |
console.info('paramsError',paramsError); | |
}).catch(err => { | |
console.error("\nError",err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment