ingeni-validation 1.1.9 → 1.1.11

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.
@@ -311,19 +311,23 @@ async function isValidDate(value, format) {
311
311
  //Stop details Validation "stop_details":[{"30":["Baripada","13:00","13:01","21.9319,86.7465"]}]
312
312
 
313
313
  async function isValidStopDetails(stop_details){
314
+ console.log("api stop details"+stop_details);
314
315
 
315
316
  if (!Array.isArray(stop_details) || stop_details.length === 0){
316
317
  return false;
317
318
  }
318
319
 
319
320
  const timeRegex = /^([01][0-9]|2[0-3]):([0-5][0-9])$/;
321
+ const latlongRegex = /^-?\d+(\.\d+)?,-?\d+(\.\d+)?$/;
320
322
 
321
323
  const toMinutes = (t)=>{
322
324
  const [h,m] = t.split(":").map(Number);
323
325
  return h*60 + m;
324
326
  };
325
327
 
326
- const latlongRegex = /^-?\d+(\.\d+)?,-?\d+(\.\d+)?$/;
328
+ let usedIds = new Set();
329
+ let expectedStopId = 1;
330
+ let previousArrival = -1;
327
331
 
328
332
  for (let stop of stop_details){
329
333
 
@@ -339,10 +343,25 @@ async function isValidStopDetails(stop_details){
339
343
 
340
344
  const stop_id = keys[0];
341
345
 
346
+ // stop id numeric
342
347
  if (!/^\d+$/.test(stop_id)){
343
348
  return false;
344
349
  }
345
350
 
351
+ const stopIdNum = Number(stop_id);
352
+
353
+ // 1️⃣ Duplicate stop ID check
354
+ if (usedIds.has(stopIdNum)){
355
+ return false;
356
+ }
357
+ usedIds.add(stopIdNum);
358
+
359
+ // 2️⃣ Sequential stop check
360
+ if (stopIdNum !== expectedStopId){
361
+ return false;
362
+ }
363
+ expectedStopId++;
364
+
346
365
  const values = stop[stop_id];
347
366
 
348
367
  if (!Array.isArray(values) || values.length !== 4){
@@ -351,32 +370,50 @@ async function isValidStopDetails(stop_details){
351
370
 
352
371
  let [stop_name, arrival_time, departure_time, latlong] = values;
353
372
 
354
- if (typeof stop_name !== "string" || stop_name.trim().length === 0){
373
+ if (typeof stop_name !== "string" ||
374
+ typeof arrival_time !== "string" ||
375
+ typeof departure_time !== "string" ||
376
+ typeof latlong !== "string"){
355
377
  return false;
356
378
  }
357
379
 
380
+ stop_name = stop_name.trim();
381
+ arrival_time = arrival_time.trim();
382
+ departure_time = departure_time.trim();
383
+ latlong = latlong.trim();
384
+
385
+ if (stop_name.length === 0) return false;
386
+
358
387
  if (!timeRegex.test(arrival_time) || !timeRegex.test(departure_time)){
359
388
  return false;
360
389
  }
361
390
 
362
- // allow equal time
363
- if (toMinutes(arrival_time) > toMinutes(departure_time)){
391
+ const arrivalMin = toMinutes(arrival_time);
392
+ const departMin = toMinutes(departure_time);
393
+
394
+ // arrival <= departure
395
+ if (arrivalMin > departMin){
364
396
  return false;
365
397
  }
366
398
 
367
- // lat,long optional
368
- if (latlong !== ""){
399
+ // 3️⃣ Next stop must be after previous stop
400
+ if (arrivalMin <= previousArrival){
401
+ return false;
402
+ }
369
403
 
370
- if (typeof latlong !== "string" || !latlongRegex.test(latlong)){
371
- return false;
372
- }
404
+ previousArrival = arrivalMin;
373
405
 
374
- let [lat, lon] = latlong.split(",").map(Number);
406
+ // mandatory coordinates
407
+ if (latlong.length === 0) return false;
375
408
 
376
- if (lat < -90 || lat > 90) return false;
377
- if (lon < -180 || lon > 180) return false;
409
+ if (!latlongRegex.test(latlong)){
410
+ return false;
378
411
  }
379
412
 
413
+ let [lat, lon] = latlong.split(",").map(Number);
414
+
415
+ if (lat < -90 || lat > 90) return false;
416
+ if (lon < -180 || lon > 180) return false;
380
417
  }
381
418
 
382
419
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ingeni-validation",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "Ingeni Validation",
5
5
  "license": "ISC",
6
6
  "author": "",