Skip to content

Instantly share code, notes, and snippets.

@droidMakk
Created April 18, 2019 00:16
Show Gist options
  • Save droidMakk/5e6a7999f21f03de06360977db8d6a5e to your computer and use it in GitHub Desktop.
Save droidMakk/5e6a7999f21f03de06360977db8d6a5e to your computer and use it in GitHub Desktop.
Simple Validator
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)
})
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