Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | 1x 1x 16x 16x 14x 12x 11x 10x 20x 20x 20x 2x 18x 18x 18x 1x 17x 1x 16x 2x 14x 3x 3x 2x 12x 12x 3x 2x 10x 10x 2x 9x 9x 1x 1x 8x 16x 16x 1x 15x 1x 14x 10x 1x 10x 4x 2x 2x 12x 2x 2x 1x 1x 11x 2x 2x 2x 5x 1x 10x 1x 9x 1x 8x 1x 7x 8x 7x 7x 2x 1x 6x 2x 2x 2x 2x 2x 1x 1x | /**
* validate.js
*
* Input validation for MarketFeed config.
* Throws clear, actionable errors before anything starts.
* No silent failures like old code.
*/
"use strict";
const { MARKET_TYPES } = require("../core/marketClock");
const { INTERVALS } = require("../core/candleManager");
/**
* Validate the full MarketFeed config
* @param {Object} config
* @throws {Error} with clear message if invalid
*/
function validateConfig(config) {
Iif (!config || typeof config !== "object") {
throw new Error(
"[MarketFeed] Config is required.\n" +
"Example:\n" +
' new MarketFeed({ type: "crypto", pairs: [...] })',
);
}
_validateType(config.type);
_validateMarketHours(config.type, config.marketHours);
_validateInterval(config.interval);
_validateCandleIntervals(config.candleIntervals);
_validatePairs(config.pairs, config.source ?? "simulation");
}
/**
* Validate a single pair config
* @param {Object} pair
* @param {number} index - position in pairs array (for error messages)
* @throws {Error}
*/
function validatePair(pair, index = 0, source = "simulation") {
const prefix = `[MarketFeed] pairs[${index}]`;
Iif (!pair || typeof pair !== "object") {
throw new Error(`${prefix} must be an object.`);
}
// symbol
if (!pair.symbol || typeof pair.symbol !== "string") {
throw new Error(
`${prefix}.symbol is required and must be a string.\n` +
`Example: { symbol: 'BTC/USDT', startPrice: 45000 }`,
);
}
Iif (pair.symbol.trim().length === 0) {
throw new Error(`${prefix}.symbol cannot be empty.`);
}
// startPrice
Eif (source === "simulation" || source === undefined) {
if (pair.startPrice === undefined || pair.startPrice === null) {
throw new Error(
`${prefix}.startPrice is required for simulation.\n` +
`Example: { symbol: '${pair.symbol}', startPrice: 45000 }\n` +
`Using a live connector? Set source: 'binance' and remove startPrice.`,
);
}
if (typeof pair.startPrice !== "number" || isNaN(pair.startPrice)) {
throw new Error(
`${prefix}.startPrice must be a number. ` + `Got: ${pair.startPrice}`,
);
}
if (pair.startPrice <= 0) {
throw new Error(
`${prefix}.startPrice must be greater than 0. ` +
`Got: ${pair.startPrice}`,
);
}
}
// volatility (optional)
if (pair.volatility !== undefined) {
Iif (typeof pair.volatility !== "number" || isNaN(pair.volatility)) {
throw new Error(
`${prefix}.volatility must be a number. ` + `Got: ${pair.volatility}`,
);
}
if (pair.volatility <= 0 || pair.volatility >= 1) {
throw new Error(
`${prefix}.volatility must be between 0 and 1 (exclusive).\n` +
`Example: 0.002 = 0.2% max move per tick. Got: ${pair.volatility}`,
);
}
}
// trend (optional)
Iif (pair.trend !== undefined) {
if (typeof pair.trend !== "number" || isNaN(pair.trend)) {
throw new Error(`${prefix}.trend must be a number. Got: ${pair.trend}`);
}
if (Math.abs(pair.trend) >= 1) {
throw new Error(
`${prefix}.trend should be a small number close to 0.\n` +
`Example: 0.0001 (slight up), -0.0001 (slight down). Got: ${pair.trend}`,
);
}
}
// precision (optional)
if (pair.precision !== undefined) {
if (
typeof pair.precision !== "number" ||
!Number.isInteger(pair.precision) ||
pair.precision < 0 ||
pair.precision > 10
) {
throw new Error(
`${prefix}.precision must be an integer between 0 and 10. ` +
`Got: ${pair.precision}`,
);
}
}
// tickSize (optional)
Iif (pair.tickSize !== undefined) {
if (typeof pair.tickSize !== "number" || pair.tickSize <= 0) {
throw new Error(
`${prefix}.tickSize must be a positive number. ` +
`Got: ${pair.tickSize}`,
);
}
}
// volume (optional)
if (pair.volume !== undefined) {
_validateVolumeConfig(pair.volume, prefix);
}
// minPrice / maxPrice (optional)
Iif (pair.minPrice !== undefined && pair.minPrice <= 0) {
throw new Error(
`${prefix}.minPrice must be greater than 0. Got: ${pair.minPrice}`,
);
}
if (pair.maxPrice !== undefined && pair.minPrice !== undefined) {
Eif (pair.maxPrice <= pair.minPrice) {
throw new Error(
`${prefix}.maxPrice must be greater than minPrice.\n` +
`Got: minPrice=${pair.minPrice}, maxPrice=${pair.maxPrice}`,
);
}
}
Iif (pair.maxPrice !== undefined && pair.maxPrice <= pair.startPrice * 0.1) {
throw new Error(
`${prefix}.maxPrice seems too low relative to startPrice.\n` +
`startPrice=${pair.startPrice}, maxPrice=${pair.maxPrice}`,
);
}
}
// ── Private validators ─────────────────────── //
function _validateType(type) {
const valid = Object.values(MARKET_TYPES);
if (!type) {
throw new Error(
`[MarketFeed] type is required.\n` +
`Valid values: ${valid.join(", ")}\n` +
`Example: { type: 'crypto', ... }`,
);
}
if (!valid.includes(type)) {
throw new Error(
`[MarketFeed] Invalid type "${type}".\n` +
`Valid values: ${valid.join(", ")}`,
);
}
}
function _validateMarketHours(type, marketHours) {
// Crypto doesn't need market hours
if (type === MARKET_TYPES.CRYPTO) {
if (marketHours) {
console.warn(
"[MarketFeed] marketHours is ignored for crypto — " +
"crypto markets are always open.",
);
}
return;
}
// Forex and equity require market hours
if (!marketHours) {
throw new Error(
`[MarketFeed] marketHours is required for type "${type}".\n` +
`Example:\n` +
` marketHours: {\n` +
` open: '09:30',\n` +
` close: '16:00',\n` +
` timezone: 'America/New_York'\n` +
` }`,
);
}
Iif (typeof marketHours !== "object") {
throw new Error("[MarketFeed] marketHours must be an object.");
}
}
function _validateInterval(interval) {
if (interval === undefined) return; // optional, has default
Iif (typeof interval !== "number" || isNaN(interval)) {
throw new Error(
`[MarketFeed] interval must be a number (milliseconds).\n` +
`Example: 1000 = 1 tick per second. Got: ${interval}`,
);
}
if (interval < 100) {
throw new Error(
`[MarketFeed] interval must be at least 100ms to avoid overwhelming consumers.\n` +
`Got: ${interval}ms`,
);
}
Iif (interval > 60 * 60 * 1000) {
console.warn(
`[MarketFeed] interval is ${interval}ms (${interval / 1000}s). ` +
`This is quite slow — is this intentional?`,
);
}
}
function _validateCandleIntervals(candleIntervals) {
if (candleIntervals === undefined) return; // optional
const valid = Object.keys(INTERVALS);
Iif (!Array.isArray(candleIntervals)) {
throw new Error(
`[MarketFeed] candleIntervals must be an array.\n` +
`Valid values: ${valid.join(", ")}\n` +
`Example: candleIntervals: ['1m', '5m', '1h']`,
);
}
candleIntervals.forEach((interval) => {
if (!valid.includes(interval)) {
throw new Error(
`[MarketFeed] Invalid candleInterval "${interval}".\n` +
`Valid values: ${valid.join(", ")}`,
);
}
});
}
function _validatePairs(pairs, source) {
if (!pairs) {
throw new Error(
`[MarketFeed] pairs is required.\n` +
`Example:\n` +
` pairs: [\n` +
` { symbol: 'BTC/USDT', startPrice: 45000 }\n` +
` ]`,
);
}
if (!Array.isArray(pairs)) {
throw new Error("[MarketFeed] pairs must be an array.");
}
if (pairs.length === 0) {
throw new Error(
"[MarketFeed] pairs cannot be empty. " + "Provide at least one pair.",
);
}
Iif (pairs.length > 100) {
console.warn(
`[MarketFeed] You have ${pairs.length} pairs configured. ` +
`Large numbers of pairs may impact performance.`,
);
}
// Check for duplicate symbols
const symbols = pairs.map((p) => p?.symbol);
const unique = new Set(symbols);
if (unique.size !== symbols.length) {
const dupes = symbols.filter((s, i) => symbols.indexOf(s) !== i);
throw new Error(
`[MarketFeed] Duplicate symbols found: ${dupes.join(", ")}. ` +
`Each symbol must be unique.`,
);
}
// Validate each pair
pairs.forEach((pair, i) => validatePair(pair, i, source));
}
function _validateVolumeConfig(volume, prefix) {
Iif (typeof volume !== "object") {
throw new Error(
`${prefix}.volume must be an object.\n` +
`Example: { min: 100, max: 10000 }`,
);
}
Iif (volume.min === undefined || volume.max === undefined) {
throw new Error(
`${prefix}.volume must have min and max.\n` +
`Example: { min: 100, max: 10000 }`,
);
}
Iif (typeof volume.min !== "number" || volume.min < 0) {
throw new Error(
`${prefix}.volume.min must be a non-negative number. ` +
`Got: ${volume.min}`,
);
}
Iif (typeof volume.max !== "number" || volume.max <= 0) {
throw new Error(
`${prefix}.volume.max must be a positive number. ` + `Got: ${volume.max}`,
);
}
if (volume.max <= volume.min) {
throw new Error(
`${prefix}.volume.max must be greater than volume.min.\n` +
`Got: min=${volume.min}, max=${volume.max}`,
);
}
}
module.exports = { validateConfig, validatePair };
|