ingeni-validation 1.1.7 → 1.1.8

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.
@@ -308,7 +308,80 @@ async function isValidDate(value, format) {
308
308
  return true;
309
309
  }
310
310
 
311
+ //Stop details Validation "stop_details":[{"30":["Baripada","13:00","13:01","21.9319,86.7465"]}]
312
+
313
+ async function isValidStopDetails(stop_details){
314
+
315
+ if (!Array.isArray(stop_details) || stop_details.length === 0){
316
+ return false;
317
+ }
318
+
319
+ const timeRegex = /^([01][0-9]|2[0-3]):([0-5][0-9])$/;
320
+
321
+ const toMinutes = (t)=>{
322
+ const [h,m] = t.split(":").map(Number);
323
+ return h*60 + m;
324
+ };
325
+
326
+ const latlongRegex = /^-?\d+(\.\d+)?,-?\d+(\.\d+)?$/;
327
+
328
+ for (let stop of stop_details){
329
+
330
+ if (typeof stop !== "object" || stop === null){
331
+ return false;
332
+ }
333
+
334
+ const keys = Object.keys(stop);
335
+
336
+ if (keys.length !== 1){
337
+ return false;
338
+ }
339
+
340
+ const stop_id = keys[0];
341
+
342
+ // stop id must be numeric
343
+ if (!/^\d+$/.test(stop_id)){
344
+ return false;
345
+ }
346
+
347
+ const values = stop[stop_id];
348
+
349
+ if (!Array.isArray(values) || values.length !== 4){
350
+ return false;
351
+ }
352
+
353
+ let [stop_name, arrival_time, departure_time, latlong] = values;
354
+
355
+ // stop name
356
+ if (typeof stop_name !== "string" || stop_name.trim().length === 0){
357
+ return false;
358
+ }
359
+
360
+ // time validation
361
+ if (!timeRegex.test(arrival_time) || !timeRegex.test(departure_time)){
362
+ return false;
363
+ }
364
+
365
+ // arrival must be before departure
366
+ if (toMinutes(arrival_time) >= toMinutes(departure_time)){
367
+ return false;
368
+ }
369
+
370
+ // lat,long validation
371
+ if (typeof latlong !== "string" || !latlongRegex.test(latlong)){
372
+ return false;
373
+ }
374
+
375
+ let [lat, lon] = latlong.split(",").map(Number);
376
+
377
+ if (lat < -90 || lat > 90) return false;
378
+ if (lon < -180 || lon > 180) return false;
379
+
380
+ }
381
+
382
+ return true;
383
+ }
311
384
 
312
385
 
313
386
 
314
- export {validateMandate,validateOptional,isValidMobile,isValidEmail,isValidOtp,isValidPin,isValidNumber,isValidBoolean,checkBooleanParams,isValidDate,isValidTime,isValidPrice};
387
+ export {validateMandate,validateOptional,isValidMobile,isValidEmail,isValidOtp,isValidPin,isValidNumber,isValidBoolean,checkBooleanParams,isValidDate,isValidTime,isValidPrice,isValidStopDetails};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ingeni-validation",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Ingeni Validation",
5
5
  "license": "ISC",
6
6
  "author": "",