@nuelink/nuelink-cli 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/validation.js CHANGED
@@ -41,11 +41,11 @@ const SUPPORTED_MEDIA_TYPES = new Map([
41
41
  [".mp4", "video/mp4"],
42
42
  [".mov", "video/quicktime"],
43
43
  ]);
44
- const DEFAULT_MAX_MEDIA_SIZE_BYTES = Number(process.env.NUELINK_MAX_MEDIA_SIZE_BYTES || 100 * 1024 * 1024);
45
- const QUEUE_SLOT_PATTERN = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
44
+ const DEFAULT_MAX_MEDIA_SIZE_BYTES = 100 * 1024 * 1024;
45
+ const QUEUE_SLOT_PATTERN = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
46
46
  const SQL_DATETIME_PATTERN = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
47
47
 
48
- function splitCsv(value) {
48
+ function splitCsv(value) {
49
49
  if (!value) {
50
50
  return [];
51
51
  }
@@ -54,7 +54,40 @@ function splitCsv(value) {
54
54
  .split(",")
55
55
  .map((item) => item.trim())
56
56
  .filter(Boolean);
57
- }
57
+ }
58
+
59
+ function validateRequiredString(value, maximum, label) {
60
+ const normalized = String(value ?? "").trim();
61
+
62
+ if (!normalized) {
63
+ throw new Error(`${label} must not be blank.`);
64
+ }
65
+
66
+ if (normalized.length > maximum) {
67
+ throw new Error(`${label} must not exceed ${maximum} characters.`);
68
+ }
69
+
70
+ return normalized;
71
+ }
72
+
73
+ function readPositiveIntegerEnvironment(name, fallback) {
74
+ const raw = process.env[name];
75
+ if (raw === undefined || raw === "") {
76
+ return fallback;
77
+ }
78
+
79
+ const normalized = String(raw).trim();
80
+ if (!/^\d+$/.test(normalized)) {
81
+ throw new Error(`${name} must be a positive integer.`);
82
+ }
83
+
84
+ const parsed = Number(normalized);
85
+ if (!Number.isSafeInteger(parsed) || parsed <= 0) {
86
+ throw new Error(`${name} must be a positive integer.`);
87
+ }
88
+
89
+ return parsed;
90
+ }
58
91
 
59
92
  function normalizeEnum(value, label, allowedValues) {
60
93
  const normalized = String(value ?? "").trim().toUpperCase();
@@ -151,7 +184,7 @@ function validateHttpUrl(value, label) {
151
184
  return parsedUrl.toString();
152
185
  }
153
186
 
154
- function validateQueueSlots(queueSlots) {
187
+ function validateQueueSlots(queueSlots) {
155
188
  queueSlots.forEach((slot) => {
156
189
  if (!QUEUE_SLOT_PATTERN.test(slot)) {
157
190
  throw new Error(`Invalid queue slot \"${slot}\". Expected values like Mon 10:10.`);
@@ -159,8 +192,8 @@ function validateQueueSlots(queueSlots) {
159
192
  });
160
193
 
161
194
  return queueSlots;
162
- }
163
-
195
+ }
196
+
164
197
  function validateScheduledAt(value) {
165
198
  if (value === undefined || value === null || value === "" || value === "null") {
166
199
  return null;
@@ -209,7 +242,12 @@ function parseJsonString(value, label) {
209
242
  }
210
243
  }
211
244
 
212
- function validateMediaFile(filePath, fileStat) {
245
+ function validateMediaFile(filePath, fileStat) {
246
+ const maximumSize = readPositiveIntegerEnvironment(
247
+ "NUELINK_MAX_MEDIA_SIZE_BYTES",
248
+ DEFAULT_MAX_MEDIA_SIZE_BYTES,
249
+ );
250
+
213
251
  if (!fileStat.isFile()) {
214
252
  throw new Error("Invalid file. Expected a regular file path.");
215
253
  }
@@ -218,8 +256,8 @@ function validateMediaFile(filePath, fileStat) {
218
256
  throw new Error("Invalid file. File must not be empty.");
219
257
  }
220
258
 
221
- if (fileStat.size > DEFAULT_MAX_MEDIA_SIZE_BYTES) {
222
- throw new Error(`Invalid file. File exceeds ${DEFAULT_MAX_MEDIA_SIZE_BYTES} bytes.`);
259
+ if (fileStat.size > maximumSize) {
260
+ throw new Error(`Invalid file. File exceeds ${maximumSize} bytes.`);
223
261
  }
224
262
 
225
263
  const extension = path.extname(filePath).toLowerCase();
@@ -248,9 +286,10 @@ export {
248
286
  parsePositiveInteger,
249
287
  splitCsv,
250
288
  validateHttpUrl,
251
- validateMediaFile,
289
+ validateMediaFile,
252
290
  validatePollOptions,
253
291
  validateScheduledAt,
254
- validateQueueSlots,
255
- normalizeEnum,
256
- };
292
+ validateQueueSlots,
293
+ validateRequiredString,
294
+ normalizeEnum,
295
+ };