ingeni-validation 1.1.6 → 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.
@@ -275,6 +275,28 @@ async function isValidDate(value, format) {
275
275
 
276
276
 
277
277
 
278
+
279
+ //Validate Time (hh:mm:ss or hh:mm)
280
+
281
+ async function isValidTime(value){
282
+
283
+ if (value === null || value === undefined) return false;
284
+
285
+ if (typeof value === "boolean") return false;
286
+
287
+ if (typeof value !== "string") return false;
288
+
289
+ if (value.length === 0) return false;
290
+
291
+ // reject spaces anywhere
292
+ if (value.includes(" ")) return false;
293
+
294
+ const regex = /^([01][0-9]|2[0-3]):([0-5][0-9])(:([0-5][0-9]))?$/;
295
+
296
+ return regex.test(value);
297
+ }
298
+
299
+
278
300
  //Validate Price
279
301
 
280
302
  async function isValidPrice(price){
@@ -286,6 +308,80 @@ async function isValidDate(value, format) {
286
308
  return true;
287
309
  }
288
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
+ }
384
+
289
385
 
290
386
 
291
- export {validateMandate,validateOptional,isValidMobile,isValidEmail,isValidOtp,isValidPin,isValidNumber,isValidBoolean,checkBooleanParams,isValidDate,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.6",
3
+ "version": "1.1.8",
4
4
  "description": "Ingeni Validation",
5
5
  "license": "ISC",
6
6
  "author": "",