@devp0nt/route0 1.0.0-next.65 → 1.0.0-next.67

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/dist/esm/index.js CHANGED
@@ -434,76 +434,171 @@ class Route0 {
434
434
  unmatched
435
435
  };
436
436
  }
437
- /**
438
- * Safe parser for flat input objects.
439
- *
440
- * Returns structured success/error result instead of throwing.
441
- */
442
- safeParseFlatInput(input, loose) {
443
- loose ??= this.hasLooseSearch;
437
+ _validateParamsInput(input) {
444
438
  const paramsKeys = this.getParamsKeys();
445
439
  if (input === void 0) {
446
440
  if (paramsKeys.length) {
447
441
  return {
448
- success: false,
449
- data: void 0,
450
- error: new Error(`Missing params: ${paramsKeys.map((k) => `"${k}"`).join(", ")}`)
442
+ issues: [
443
+ {
444
+ message: `Missing params: ${paramsKeys.map((k) => `"${k}"`).join(", ")}`
445
+ }
446
+ ]
451
447
  };
452
448
  }
453
449
  input = {};
454
450
  }
455
451
  if (typeof input !== "object" || input === null) {
456
452
  return {
457
- success: false,
458
- data: void 0,
459
- error: new Error("Invalid input: expected object")
453
+ issues: [{ message: "Invalid input: expected object" }]
460
454
  };
461
455
  }
462
- const inputKeys = Object.keys(input);
456
+ const inputObj = input;
457
+ const inputKeys = Object.keys(inputObj);
463
458
  const notDefinedKeys = paramsKeys.filter((k) => !inputKeys.includes(k));
464
459
  if (notDefinedKeys.length) {
465
460
  return {
466
- success: false,
467
- data: void 0,
468
- error: new Error(`Missing params: ${notDefinedKeys.map((k) => `"${k}"`).join(", ")}`)
461
+ issues: [
462
+ {
463
+ message: `Missing params: ${notDefinedKeys.map((k) => `"${k}"`).join(", ")}`
464
+ }
465
+ ]
469
466
  };
470
467
  }
471
468
  const data = {};
472
- const filterKeys = !loose ? [...paramsKeys, ...this.getSearchKeys()] : false;
473
- for (const [k, v] of Object.entries(input)) {
474
- if (filterKeys && !filterKeys.includes(k)) {
475
- continue;
469
+ for (const k of paramsKeys) {
470
+ const v = inputObj[k];
471
+ if (typeof v === "string") {
472
+ data[k] = v;
473
+ } else if (typeof v === "number") {
474
+ data[k] = String(v);
475
+ } else {
476
+ return {
477
+ issues: [{ message: `Invalid input: expected string, number, got ${typeof v} for "${k}"` }]
478
+ };
476
479
  }
480
+ }
481
+ return {
482
+ value: data
483
+ };
484
+ }
485
+ _validateSearchInput(input, loose) {
486
+ if (input === void 0) {
487
+ input = {};
488
+ }
489
+ if (typeof input !== "object" || input === null) {
490
+ return {
491
+ issues: [{ message: "Invalid input: expected object" }]
492
+ };
493
+ }
494
+ const inputObj = input;
495
+ const paramsKeys = this.getParamsKeys();
496
+ const searchKeys = this.getSearchKeys();
497
+ const data = {};
498
+ for (const [k, v] of Object.entries(inputObj)) {
499
+ if (k === "hash") continue;
500
+ if (paramsKeys.includes(k)) continue;
501
+ if (!loose && !searchKeys.includes(k)) continue;
502
+ if (v === void 0) continue;
477
503
  if (typeof v === "string") {
478
504
  data[k] = v;
479
505
  } else if (typeof v === "number") {
480
506
  data[k] = String(v);
481
507
  } else {
482
- const isParamKey = paramsKeys.includes(k);
483
508
  return {
484
- success: false,
485
- data: void 0,
486
- error: new Error(
487
- `Invalid input: expected string, number,${!isParamKey ? " or undefined," : ""} got ${typeof v} for "${k}"`
488
- )
509
+ issues: [{ message: `Invalid input: expected string, number, or undefined, got ${typeof v} for "${k}"` }]
489
510
  };
490
511
  }
491
512
  }
492
513
  return {
493
- success: true,
494
- data,
495
- error: void 0
514
+ value: data
515
+ };
516
+ }
517
+ _validateFlatInput(input, loose) {
518
+ const paramsResult = this._validateParamsInput(input);
519
+ if ("issues" in paramsResult) {
520
+ return {
521
+ issues: paramsResult.issues ?? []
522
+ };
523
+ }
524
+ const searchResult = this._validateSearchInput(input, loose);
525
+ if ("issues" in searchResult) {
526
+ return {
527
+ issues: searchResult.issues ?? []
528
+ };
529
+ }
530
+ return {
531
+ value: {
532
+ ...searchResult.value,
533
+ ...paramsResult.value
534
+ }
496
535
  };
497
536
  }
498
- /** Throwing variant of `safeParseFlatInput()`. */
499
- parseFlatInput(input, loose) {
500
- loose ??= this.hasLooseSearch;
501
- const result = this.safeParseFlatInput(input, loose);
502
- if (result.error) {
503
- throw result.error;
537
+ _safeParseSchemaResult(result) {
538
+ if ("issues" in result) {
539
+ return {
540
+ success: false,
541
+ data: void 0,
542
+ error: new Error(result.issues?.[0]?.message ?? "Invalid input")
543
+ };
504
544
  }
505
- return result.data;
545
+ return {
546
+ success: true,
547
+ data: result.value,
548
+ error: void 0
549
+ };
506
550
  }
551
+ _parseSchemaResult(result) {
552
+ const safeResult = this._safeParseSchemaResult(result);
553
+ if (safeResult.error) {
554
+ throw safeResult.error;
555
+ }
556
+ return safeResult.data;
557
+ }
558
+ /** Standard Schema for route params input. */
559
+ paramsInputSchema = {
560
+ "~standard": {
561
+ version: 1,
562
+ vendor: "route0",
563
+ validate: (value) => this._validateParamsInput(value),
564
+ types: void 0
565
+ },
566
+ parse: (value) => this._parseSchemaResult(this._validateParamsInput(value)),
567
+ safeParse: (value) => this._safeParseSchemaResult(this._validateParamsInput(value))
568
+ };
569
+ /** Standard Schema for strict search input. */
570
+ strictSearchInputSchema = {
571
+ "~standard": {
572
+ version: 1,
573
+ vendor: "route0",
574
+ validate: (value) => this._validateSearchInput(value, false),
575
+ types: void 0
576
+ },
577
+ parse: (value) => this._parseSchemaResult(this._validateSearchInput(value, false)),
578
+ safeParse: (value) => this._safeParseSchemaResult(this._validateSearchInput(value, false))
579
+ };
580
+ /** Standard Schema for loose search input. */
581
+ looseSearchInputSchema = {
582
+ "~standard": {
583
+ version: 1,
584
+ vendor: "route0",
585
+ validate: (value) => this._validateSearchInput(value, true),
586
+ types: void 0
587
+ },
588
+ parse: (value) => this._parseSchemaResult(this._validateSearchInput(value, true)),
589
+ safeParse: (value) => this._safeParseSchemaResult(this._validateSearchInput(value, true))
590
+ };
591
+ /** Standard Schema for route flat input (uses route default strict/loose mode). */
592
+ flatInputSchema = {
593
+ "~standard": {
594
+ version: 1,
595
+ vendor: "route0",
596
+ validate: (value) => this._validateFlatInput(value, this.hasLooseSearch),
597
+ types: void 0
598
+ },
599
+ parse: (value) => this._parseSchemaResult(this._validateFlatInput(value, this.hasLooseSearch)),
600
+ safeParse: (value) => this._safeParseSchemaResult(this._validateFlatInput(value, this.hasLooseSearch))
601
+ };
507
602
  /** True when path structure is equal (param names are ignored). */
508
603
  isSame(other) {
509
604
  return this.pathDefinition.replace(/:([A-Za-z0-9_]+)/g, "__PARAM__") === other.pathDefinition.replace(/:([A-Za-z0-9_]+)/g, "__PARAM__");