chore: written data validation via joi library
This commit is contained in:
parent
6bc34697e0
commit
0f5e473ea7
3 changed files with 86 additions and 0 deletions
35
models/Currency.js
Normal file
35
models/Currency.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const Joi = require('joi');
|
||||
|
||||
const currencySchema = Joi.object({
|
||||
from_currency: Joi.string().length(3).required().messages({
|
||||
'string.base': 'from_currency must be a string',
|
||||
'string.length': 'from_currency must be exactly 3 characters long',
|
||||
'any.required': 'from_currency is required'
|
||||
}),
|
||||
conv_currency: Joi.string().length(3).required().messages({
|
||||
'string.base': 'conv_currency must be a string',
|
||||
'string.length': 'conv_currency must be exactly 3 characters long',
|
||||
'any.required': 'conv_currency is required'
|
||||
}),
|
||||
date: Joi.date().iso().required().messages({
|
||||
'date.base': 'date must be a valid ISO date',
|
||||
'any.required': 'date is required'
|
||||
}),
|
||||
rate: Joi.number().precision(6).positive().required().messages({
|
||||
'number.base': 'rate must be a number',
|
||||
'number.positive': 'rate must be a positive number',
|
||||
'number.precision': 'rate must have no more than 6 decimal places',
|
||||
'any.required': 'rate is required'
|
||||
})
|
||||
});
|
||||
|
||||
async function validateCurrency(data) {
|
||||
try {
|
||||
const validatedData = await currencySchema.validateAsync(data);
|
||||
return validatedData;
|
||||
} catch (error) {
|
||||
throw new Error(`Validation error \n${JSON.stringify(error.details[0], null, '\t')}`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { validateCurrency };
|
50
package-lock.json
generated
50
package-lock.json
generated
|
@ -12,9 +12,46 @@
|
|||
"axios": "^1.8.4",
|
||||
"cron-validator": "^1.3.1",
|
||||
"hjson": "^3.2.2",
|
||||
"joi": "^17.13.3",
|
||||
"node-schedule": "^2.1.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@hapi/hoek": {
|
||||
"version": "9.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz",
|
||||
"integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==",
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/@hapi/topo": {
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
|
||||
"integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"@hapi/hoek": "^9.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@sideway/address": {
|
||||
"version": "4.1.5",
|
||||
"resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.5.tgz",
|
||||
"integrity": "sha512-IqO/DUQHUkPeixNQ8n0JA6102hT9CmaljNTPmQ1u8MEhBo/R4Q8eKLN/vGZxuebwOroDB4cbpjheD4+/sKFK4Q==",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"@hapi/hoek": "^9.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@sideway/formula": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.1.tgz",
|
||||
"integrity": "sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg==",
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/@sideway/pinpoint": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz",
|
||||
"integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==",
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/asynckit": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
|
@ -284,6 +321,19 @@
|
|||
"hjson": "bin/hjson"
|
||||
}
|
||||
},
|
||||
"node_modules/joi": {
|
||||
"version": "17.13.3",
|
||||
"resolved": "https://registry.npmjs.org/joi/-/joi-17.13.3.tgz",
|
||||
"integrity": "sha512-otDA4ldcIx+ZXsKHWmp0YizCweVRZG96J10b0FevjfuncLO1oX59THoAmHkNubYJ+9gWsYsp5k8v4ib6oDv1fA==",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"@hapi/hoek": "^9.3.0",
|
||||
"@hapi/topo": "^5.1.0",
|
||||
"@sideway/address": "^4.1.5",
|
||||
"@sideway/formula": "^3.0.1",
|
||||
"@sideway/pinpoint": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/long-timeout": {
|
||||
"version": "0.1.1",
|
||||
"resolved": "https://registry.npmjs.org/long-timeout/-/long-timeout-0.1.1.tgz",
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
"axios": "^1.8.4",
|
||||
"cron-validator": "^1.3.1",
|
||||
"hjson": "^3.2.2",
|
||||
"joi": "^17.13.3",
|
||||
"node-schedule": "^2.1.1"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue