diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 83dc81d77..b5bf25bcc 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -335,21 +335,23 @@ "Model is not valid": "El campo \" {{key}}\" no es válido", "Property is not defined in this model": "La propiedad que ha modificado no existe", "postCode": "Código postal", - "postcode": "Código postal", + "postcode": "Código postal", "fi": "NIF/CIF", "nif": "NIF/CIF", - "Account": "Cuenta", + "Account": "Cuenta", "socialName": "Razón social", - "street":"Dirección fiscal", - "city":"Población", - "countryFk":"País", - "provinceFk":"Provincia", - "supplierFk":"Actividad del proveedor", - "healthRegister":"Registro sanitario", - "sageTaxTypeFk":"Tipo de impuesto Sage", - "sageTransactionTypeFk":"Tipo de transacción Sage", - "sageWithholdingFk" : "Sage con tenencia", + "street": "Dirección fiscal", + "city": "Población", + "countryFk": "País", + "provinceFk": "Provincia", + "supplierFk": "Actividad del proveedor", + "healthRegister": "Registro sanitario", + "sageTaxTypeFk": "Tipo de impuesto Sage", + "sageTransactionTypeFk": "Tipo de transacción Sage", + "sageWithholdingFk": "Sage con tenencia", "Cannot past travels with entries": "No se pueden pasar envíos con entradas", "It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}", - "Field are invalid": "El campo '{{tag}}' no es válido" -} + "Field are invalid": "El campo '{{tag}}' no es válido", + "additionalData": "additionalData", + "isToBeMailed": "isToBeMailed" +} \ No newline at end of file diff --git a/loopback/server/middleware/error-handler.js b/loopback/server/middleware/error-handler.js index 076122aa0..b0b4cb55a 100644 --- a/loopback/server/middleware/error-handler.js +++ b/loopback/server/middleware/error-handler.js @@ -1,15 +1,26 @@ const UserError = require('../../util/user-error'); const logToConsole = require('strong-error-handler/lib/logger'); +function isJsonString(str) { + try { + let json = JSON.parse(str); + return (typeof json === 'object'); + } catch (e) { + return false; + } +} const validations = [{ validation: message => String(message).startsWith('Value is not'), message: ({__: $t}, _tag) => $t('Field are invalid', {tag: $t(_tag)}), - handleError: ({method, originalUrl, body}) => { + handleError: ({method: verb, originalUrl, body}) => { const {models} = require('vn-loopback/server/server'); let tag = null; try { - let [module, id, path] = originalUrl.split('api/')[1].split('/'); + let [module, ...path] = originalUrl.split('?')[0].split('api/')[1]; + path = path.join('/'); + // let module = url.split('/')[0]; + // let [id, path] = url.substring(url.indexOf('/') + 1).split('/'); let model = models[module]; if (!model) { @@ -17,12 +28,46 @@ const validations = [{ model = models[module]; } if (!model) throw new Error('No matching model found'); - const {accepts} = model.sharedClass.methods().find(method => method.name === path); + const currentMethod = model.sharedClass.methods().find(method => { + const methodMatch = [method.http].flat().filter(el => el.verb === verb.toLowerCase()); + if (methodMatch.length > 0) { + const http = methodMatch.find(el => el.path.endsWith(path)); + if (http) + return method; + } + } + ); + const {accepts} = currentMethod; for (const [key, value] of Object.entries(body)) { const accept = accepts.find(acc => acc.arg === key); - if (!value && accept.type !== 'any') { - tag = key; - break; + if (accept.type !== 'any') { + let isValid = false; + if (value) { + switch (accept.type) { + case 'object': + isValid = isJsonString(value); + break; + case 'number': + isValid = /^[0-9]*$/.test(value); + break; + + case 'boolean': + isValid = value instanceof Boolean; + break; + + case 'date': + isValid = (new Date(date) !== 'Invalid Date') && !isNaN(new Date(date)); + break; + + case 'string': + isValid = typeof value == 'string'; + break; + } + } + if (!value || !isValid) { + tag = key; + break; + } } } return tag;