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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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', 'index').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 candleSchema = Joi.object({
  16. symbolId: Joi.number().integer().positive().required(),
  17. openTime: Joi.date().iso().required(),
  18. closeTime: Joi.date().iso().required(),
  19. open: Joi.number().precision(15).required(),
  20. high: Joi.number().precision(15).required(),
  21. low: Joi.number().precision(15).required(),
  22. close: Joi.number().precision(15).required(),
  23. volume: Joi.number().precision(15).required()
  24. });
  25. const candleQuerySchema = Joi.object({
  26. symbolId: Joi.number().integer().positive().required(),
  27. startTime: Joi.date().iso(),
  28. endTime: Joi.date().iso().when('startTime', {
  29. is: Joi.exist(),
  30. then: Joi.date().iso().greater(Joi.ref('startTime'))
  31. }),
  32. limit: Joi.number().integer().min(1).max(1000).default(100),
  33. offset: Joi.number().integer().min(0).default(0)
  34. });
  35. // Live price validation schemas
  36. const livePriceSchema = Joi.object({
  37. symbolId: Joi.number().integer().positive().required(),
  38. price: Joi.number().precision(15).positive().required(),
  39. bid: Joi.number().precision(15).positive(),
  40. ask: Joi.number().precision(15).positive(),
  41. bidSize: Joi.number().precision(15).positive(),
  42. askSize: Joi.number().precision(15).positive()
  43. });
  44. // Middleware functions
  45. const validate = (schema) => {
  46. return (req, res, next) => {
  47. const { error, value } = schema.validate(req.body, { abortEarly: false });
  48. if (error) {
  49. error.isJoi = true;
  50. return next(error);
  51. }
  52. req.body = value;
  53. next();
  54. };
  55. };
  56. const validateQuery = (schema) => {
  57. return (req, res, next) => {
  58. const { error, value } = schema.validate(req.query, { abortEarly: false });
  59. if (error) {
  60. error.isJoi = true;
  61. return next(error);
  62. }
  63. req.query = value;
  64. next();
  65. };
  66. };
  67. const validateParams = (schema) => {
  68. return (req, res, next) => {
  69. const { error, value } = schema.validate(req.params, { abortEarly: false });
  70. if (error) {
  71. error.isJoi = true;
  72. return next(error);
  73. }
  74. req.params = value;
  75. next();
  76. };
  77. };
  78. module.exports = {
  79. symbolSchema,
  80. symbolIdSchema,
  81. candleQuerySchema,
  82. livePriceSchema,
  83. validate,
  84. validateQuery,
  85. validateParams
  86. };