120 lines
3.8 KiB
JavaScript
120 lines
3.8 KiB
JavaScript
const bodyValidation=(schema,body)=>{
|
|
schema.array.forEach(element => {
|
|
if(element.required){
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
exports.checkKeyMatch=(arr1, arr2,section="Body")=> {
|
|
// Iterate over each object in arr1
|
|
var errors=[]
|
|
if(arr1){
|
|
for (let obj1 of arr1) {
|
|
if (obj1.required) {
|
|
let key = obj1.itemName.toLowerCase()
|
|
|
|
if(arr2.hasOwnProperty(obj1.itemName) || arr2.hasOwnProperty(key) ){
|
|
|
|
let value = arr2[obj1.itemName]? arr2[obj1.itemName]:arr2[key];
|
|
if(obj1.value !=null || obj1.value !=""){
|
|
if(obj1.value=="multipart/form-data"){
|
|
if (!value.includes(obj1.value)) {
|
|
errors.push(section+" data not matching: "+obj1.itemName+" , should be "+obj1.value);
|
|
|
|
}
|
|
}else{
|
|
if (obj1.value != value) {
|
|
errors.push(section+" data not matching: "+obj1.itemName+" , should be "+obj1.value);
|
|
|
|
}
|
|
}
|
|
}else{
|
|
if(obj1.validation=="Email" && !validateEmail(value)){
|
|
errors.push(section+" data["+obj1.itemName+"] not valid :"+value+" , should be valid email");
|
|
}else if(obj1.validation=="Number" && !validateNumber(value)){
|
|
errors.push(section+" data["+obj1.itemName+"] not valid :"+value+" , should be valid Number");
|
|
}else if(obj1.validation=="Integer" && !validateInteger(value)){
|
|
errors.push(section+" data["+obj1.itemName+"] not valid :"+value+" , should be valid Integer");
|
|
}else if(obj1.validation=="Date" && !validateDate(value)){
|
|
errors.push(section+" data["+obj1.itemName+"] not valid :"+value+" , should be valid Date");
|
|
}else if(obj1.validation=="String-Required" && value.length==0){
|
|
errors.push(section+" data["+obj1.itemName+"] not valid :"+value+" , should be valid String");
|
|
}
|
|
}
|
|
}else{
|
|
errors.push(section+" data not available: "+obj1.itemName);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
return errors; //
|
|
}
|
|
|
|
exports.checkFileMatch=(validFileTypes,file)=> {
|
|
// Iterate over each object in arr1
|
|
var errors=[]
|
|
|
|
console.log(validFileTypes)
|
|
console.log(file)
|
|
if(!isValidFileType(validFileTypes,file)){
|
|
errors.push("File type not valid supports "+validFileTypes.join(","))
|
|
}
|
|
return errors; //
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateEmail(email) {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return emailRegex.test(email);
|
|
}
|
|
|
|
function validateUsername(username) {
|
|
// Username can contain letters, numbers, underscores, and dots
|
|
const usernameRegex = /^[a-zA-Z0-9_.-]{4,}$/;
|
|
return usernameRegex.test(username);
|
|
}
|
|
|
|
function validateNumber(number) {
|
|
return !isNaN(number);
|
|
}
|
|
|
|
function validateInteger(integer) {
|
|
return Number.isInteger(Number(integer));
|
|
}
|
|
|
|
function validateDate(date) {
|
|
return !isNaN(Date.parse(date));
|
|
}
|
|
|
|
function validateString(string, minLength, maxLength) {
|
|
return typeof string === 'string' && string.length >= minLength && string.length <= maxLength;
|
|
}
|
|
|
|
function validateURL(url) {
|
|
const urlRegex = /^(http|https):\/\/[^ "]+$/;
|
|
return urlRegex.test(url);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isValidFileType(fileTypes, file) {
|
|
const allowedTypes = {
|
|
"XML":"application/xml",
|
|
"JSON":"application/json",
|
|
"GIF":"image/gif",
|
|
"PNG":"image/png",
|
|
"JPG":"image/jpeg",
|
|
"PDF":"application/pdf",
|
|
"DOC":"application/doc",
|
|
}
|
|
const validMimetypes = fileTypes.map(type => allowedTypes[type]);
|
|
|
|
// Check if the file's mimetype is in the validMimetypes array
|
|
return validMimetypes.includes(file.mimetype);
|
|
} |