adonis-mqcontroller
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -48,3 +48,3 @@ 'use strict' | ||
validateFields(data, allowFields = {}, path = ""){ | ||
validateFields(data, allowFields = {}, path = "", rootData = null){ | ||
debug("path: ", path) | ||
@@ -55,4 +55,9 @@ debug("data: ", data), | ||
error: false, | ||
message: "OK" | ||
message: "OK", | ||
} | ||
let root = false; | ||
if(rootData == null){ | ||
root = true | ||
rootData = data | ||
} | ||
if(typeof allowFields == "string"){ | ||
@@ -64,2 +69,3 @@ debug("type is string...") | ||
let isExists = data!== undefined | ||
if(path[path.length - 1] == ".") path = path.substring(0,path.length - 1) | ||
@@ -81,3 +87,3 @@ if(isRequired && !isExists){ //nếu field là bắt buộc như lại không tồn tại trong data. | ||
typeAllowed = !isNaN(data) | ||
if(typeAllowed) data = Number(data) | ||
if(typeAllowed) _.set(rootData, path, Number(data)) | ||
} | ||
@@ -87,11 +93,11 @@ else if(typeOfField == "boolean"){ | ||
typeAllowed = ["true","false","1","0",1,0, true, false].includes(data) | ||
if(typeAllowed) data = ["true","1",1, true].includes(data) | ||
if(typeAllowed) _.set(rootData, path, ["true","1",1, true].includes(data)) | ||
} | ||
else if(typeOfField == "date" || typeOfField == "moment"){ | ||
typeAllowed = new Date(data) != "Invalid Date" | ||
if(typeAllowed) data = new Date(data) | ||
if(typeAllowed) _.set(rootData, path, new Date(data)) | ||
} | ||
else if(typeOfField == "objectid"){ | ||
typeAllowed = ObjectId.isValid(data) | ||
if(typeAllowed) data = new ObjectId(data) | ||
if(typeAllowed) data = _.set(rootData, path, new ObjectId(data)) | ||
} | ||
@@ -123,3 +129,3 @@ } | ||
for(let i in fieldValue){ | ||
result = this.validateFields(fieldValue[i], typeOfField[0], `${path}${fieldName}[${i}].`) | ||
result = this.validateFields(fieldValue[i], typeOfField[0], `${path}${fieldName}.${i}.`, rootData) | ||
if(result.error) return result | ||
@@ -131,9 +137,13 @@ } | ||
if(fieldValue == undefined){ | ||
return this.validateError("required",{path: `${path}${fieldName}`}) | ||
//return this.validateError("required",{path: `${path}${fieldName}`}) | ||
result = this.validateFields(fieldValue, typeOfField[0], `${path}${fieldName}[0].`) | ||
} | ||
return this.validateError("Invalid Type", { | ||
path: `${path}${fieldName}`, | ||
typeOfField: "array", | ||
realType: typeof fieldValue | ||
}) | ||
else{ | ||
return this.validateError("Invalid Type", { | ||
path: `${path}${fieldName}`, | ||
typeOfField: "array", | ||
realType: typeof fieldValue | ||
}) | ||
} | ||
//result = this.validateFields(fieldValue, typeOfField[0], `${path}${fieldName}[0].`) | ||
@@ -146,3 +156,3 @@ } | ||
debug("case 3: check object:") | ||
result = this.validateFields(fieldValue, typeOfField, `${path}${fieldName}.`) | ||
result = this.validateFields(fieldValue, typeOfField, `${path}${fieldName}.`, rootData) | ||
} | ||
@@ -152,7 +162,24 @@ else{ | ||
debug("case 4: check type is string:") | ||
result = this.validateFields(fieldValue, typeOfField, `${path}${fieldName}`) | ||
result = this.validateFields(fieldValue, typeOfField, `${path}${fieldName}`, rootData) | ||
} | ||
if(result.error) return result | ||
if(result.error) { | ||
return result | ||
} | ||
else{ | ||
result = { | ||
...result, | ||
data: { | ||
...result.data, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if(root){ | ||
return { | ||
...result, | ||
data: rootData | ||
} | ||
} | ||
return result | ||
@@ -159,0 +186,0 @@ } |
{ | ||
"name": "adonis-mqcontroller", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "main": "./BaseController.js", |
10156
166