Market Data Service is a high-performance financial data API that provides comprehensive Symbol prices of different markets through both RESTful endpoints and real-time WebSocket connections.

validation.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const Joi = require('joi');
  2. // Symbol validation schemas
  3. const symbolSchema = Joi.object({
  4. symbol: Joi.string().max(50).required(),
  5. baseAsset: Joi.string().max(50),
  6. quoteAsset: Joi.string().max(50),
  7. exchange: Joi.string().max(50),
  8. instrumentType: Joi.string().valid('crypto', 'stock', 'forex', 'commodity').required(),
  9. isActive: Joi.boolean().default(true)
  10. });
  11. const symbolIdSchema = Joi.object({
  12. id: Joi.number().integer().positive().required()
  13. });
  14. // Candle validation schemas
  15. const candleQuerySchema = Joi.object({
  16. symbolId: Joi.number().integer().positive().required(),
  17. startTime: Joi.date().iso(),
  18. endTime: Joi.date().iso().when('startTime', {
  19. is: Joi.exist(),
  20. then: Joi.date().iso().greater(Joi.ref('startTime'))
  21. }),
  22. limit: Joi.number().integer().min(1).max(1000).default(100),
  23. offset: Joi.number().integer().min(0).default(0)
  24. });
  25. // Live price validation schemas
  26. const livePriceSchema = Joi.object({
  27. symbolId: Joi.number().integer().positive().required(),
  28. price: Joi.number().precision(8).positive().required(),
  29. bid: Joi.number().precision(8).positive(),
  30. ask: Joi.number().precision(8).positive(),
  31. bidSize: Joi.number().precision(8).positive(),
  32. askSize: Joi.number().precision(8).positive()
  33. });
  34. // Middleware functions
  35. const validate = (schema) => {
  36. return (req, res, next) => {
  37. const { error, value } = schema.validate(req.body, { abortEarly: false });
  38. if (error) {
  39. error.isJoi = true;
  40. return next(error);
  41. }
  42. req.body = value;
  43. next();
  44. };
  45. };
  46. const validateQuery = (schema) => {
  47. return (req, res, next) => {
  48. const { error, value } = schema.validate(req.query, { abortEarly: false });
  49. if (error) {
  50. error.isJoi = true;
  51. return next(error);
  52. }
  53. req.query = value;
  54. next();
  55. };
  56. };
  57. const validateParams = (schema) => {
  58. return (req, res, next) => {
  59. const { error, value } = schema.validate(req.params, { abortEarly: false });
  60. if (error) {
  61. error.isJoi = true;
  62. return next(error);
  63. }
  64. req.params = value;
  65. next();
  66. };
  67. };
  68. module.exports = {
  69. symbolSchema,
  70. symbolIdSchema,
  71. candleQuerySchema,
  72. livePriceSchema,
  73. validate,
  74. validateQuery,
  75. validateParams
  76. };