Skip to content

Conversation

@Swathi-eGov
Copy link
Collaborator

@Swathi-eGov Swathi-eGov commented Oct 13, 2025

Summary by CodeRabbit

  • New Features
    • Introduced configurable mobile number validation powered by MDMS with global/default fallbacks.
    • Applied validation (length, numeric range, pattern, prefix, and localized errors) to phone fields in Citizen Create, Employee Create Complaint, and PGR Inbox search.
    • Added loading indicators while validation rules are fetched for a smoother experience.
  • Chores
    • Updated core UI module dependency to the latest patch release.

@coderabbitai
Copy link

coderabbitai bot commented Oct 13, 2025

Walkthrough

Adds a new hook for mobile number validation, exposes it via the PGR hooks index, and integrates MDMS/global-config-driven validation into citizen create, employee create complaint, and PGR inbox pages. Also updates @egovernments/digit-ui-module-core dependency versions in two package.json files.

Changes

Cohort / File(s) Summary
Dependency version bump
frontend/micro-ui/web/package.json, frontend/micro-ui/web/micro-ui-internals/example/package.json
Updated "@egovernments/digit-ui-module-core" from 1.8.44 to 1.8.55.
Hook: mobile validation (add + export)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js, frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/index.js
Introduced useMobileValidation hook; merges global configs, MDMS, and defaults; exposed via Hooks.pgr.useMobileValidation.
Citizen create integration
.../pgr/src/pages/citizen/Create/CitizenCreate.js
Consumes useMobileValidation; injects validation (min/max, minlength/maxlength, pattern, error) into phone field; shows Loader while fetching.
Employee create complaint integration
.../pgr/src/pages/employee/CreateComplaint/index.js
Applies useMobileValidation to contact number field; augments form schema with prefix and validation; loader gated by validation loading.
PGR Inbox search integration
.../pgr/src/pages/employee/PGRInbox.js
Uses useMobileValidation to enhance mobile search field validation; updates loader condition; mutates config when rules available.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor U as User
  participant P as Page (Citizen/Employee/Inbox)
  participant H as Hooks.pgr.useMobileValidation
  participant G as window.globalConfigs
  participant M as MDMS API (custom)
  participant D as Defaults

  U->>P: Open page
  P->>H: useMobileValidation(tenantId, validationName?)
  H->>G: getConfig("CORE_MOBILE_CONFIGS")
  H->>M: fetch ValidationConfigs.mobileNumberValidation
  M-->>H: MDMS rules (optional)
  G-->>H: Global rules (optional)
  H->>D: Read fallback defaults
  H-->>P: {validationRules, isLoading, error, getMinMaxValues}

  alt isLoading
    P-->>U: Show Loader
  else rules available
    P->>P: Inject rules into form/search field config
    U-->>P: Enter mobile number
    P->>P: Validate using pattern/length/min/max
    P-->>U: Show error or proceed
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I twitch my ears at forms anew,
Hop-hop, we validate digits too!
From MDMS burrows, rules arise,
With global hints and fallback wise.
Now fields won’t wobble, neat and tight—
A carrot-check passes, green light! 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly summarizes the primary change of updating the system to fetch mobile number validation rules from global configurations or the MDMS service, matching the main content of the pull request without extraneous details.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mobile_validation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/PGRInbox.js (1)

45-98: Fix undefined getMinMaxValues usage

getMinMaxValues() is invoked here, but the hook destructuring above never pulls it out, so getMinMaxValues is undefined and this block will throw immediately when validationRules resolves. Destructure it from useMobileValidation before using it.

-  const { validationRules, isLoading: isValidationLoading } = Digit.Hooks.pgr.useMobileValidation(tenantId);
+  const { validationRules, isLoading: isValidationLoading, getMinMaxValues } = Digit.Hooks.pgr.useMobileValidation(tenantId);
🧹 Nitpick comments (4)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js (2)

68-101: Memoize validationRules and respect isActive in consumers.

validationRules is recreated every render, causing downstream useMemo invalidations. Also, consumers should honor rules.isActive to allow MDMS to disable validation.

Apply memoization and expose a stable function:

+import { useMemo, useCallback } from "react";
@@
-  const validationRules = {
+  const validationRules = useMemo(() => ({
     allowedStartingDigits:
       globalConfig?.mobileNumberAllowedStartingDigits || mdmsConfig?.rules?.allowedStartingDigits || defaultValidation?.rules?.allowedStartingDigits,
@@
     errorMessage:
       globalConfig?.mobileNumberErrorMessage || mdmsConfig?.rules?.errorMessage || defaultValidation?.rules?.errorMessage,
@@
-        : true
-
-  };
+        : true
+  }), [
+    JSON.stringify(globalConfig?.mobileNumberAllowedStartingDigits),
+    globalConfig?.mobilePrefix,
+    globalConfig?.mobileNumberPattern,
+    globalConfig?.mobileNumberLength,
+    globalConfig?.mobileNumberErrorMessage,
+    JSON.stringify(mdmsConfig)
+  ]);

In consumer files, gate application with validationRules.isActive (see suggested diffs there).


104-118: Avoid over‑permissive min/max when starting digits are non‑contiguous.

For allowedStartingDigits like [6,9], min/max will allow 7/8 as well. Prefer restricting min/max only when a single starting digit is allowed or the set is contiguous; otherwise rely on pattern/length alone.

-  const getMinMaxValues = () => {
-    const { allowedStartingDigits, minLength } = validationRules;
-    if (!allowedStartingDigits || allowedStartingDigits.length === 0) {
-      return { min: 0, max: 9999999999 };
-    }
-
-    const minDigit = Math.min(...allowedStartingDigits.map(Number));
-    const maxDigit = Math.max(...allowedStartingDigits.map(Number));
-
-    const min = minDigit * Math.pow(10, minLength - 1);
-    const max = (maxDigit + 1) * Math.pow(10, minLength - 1) - 1;
-
-    return { min, max };
-  };
+  const getMinMaxValues = useCallback(() => {
+    const { allowedStartingDigits, minLength } = validationRules;
+    if (!allowedStartingDigits || allowedStartingDigits.length === 0 || !minLength) {
+      return { min: undefined, max: undefined };
+    }
+    const digits = [...new Set(allowedStartingDigits.map(Number))].sort((a, b) => a - b);
+    const isContiguous = digits.every((d, i) => i === 0 || d === digits[i - 1] + 1);
+    if (digits.length === 1 || isContiguous) {
+      const minDigit = digits[0];
+      const maxDigit = digits[digits.length - 1];
+      const factor = Math.pow(10, minLength - 1);
+      return {
+        min: minDigit * factor,
+        max: (maxDigit + 1) * factor - 1,
+      };
+    }
+    // Non-contiguous starting digits: let pattern/length enforce validity.
+    return { min: undefined, max: undefined };
+  }, [validationRules]);
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/citizen/Create/CitizenCreate.js (2)

31-63: Apply rules only when active; avoid stale dependency; set min/max conditionally.

  • Respect validationRules.isActive to allow MDMS/global to disable validation.
  • Add getMinMaxValues to deps to prevent stale closure.
  • Only set min/max when provided.
-  const config = useMemo(() => {
-    if (!validationRules) return baseConfig;
+  const config = useMemo(() => {
+    if (!validationRules || validationRules.isActive === false) return baseConfig;
 
-    const { min, max } = getMinMaxValues();
+    const { min, max } = getMinMaxValues();
@@
-              return {
+              const populated = {
                 ...field,
                 populators: {
                   ...field.populators,
                   validation: {
-                    min: min,
-                    max: max,
+                    ...(min !== undefined ? { min } : {}),
+                    ...(max !== undefined ? { max } : {}),
                     minlength: validationRules.minLength,
                     maxlength: validationRules.maxLength,
                     pattern: validationRules.pattern,
                   },
                   error: validationRules.errorMessage || field.populators.error,
                 },
-              };
+              };
+              return populated;
             }
             return field;
           }),
         };
       }
       return section;
     });
-  }, [validationRules]);
+  }, [validationRules, getMinMaxValues]);

84-89: Remove console logs before shipping.

Avoid noisy logs in production bundles.

-     <FormComposerCitizen config={config} onSubmit={onFormSubmit}
-     onFormValueChange={(setValue, formData, formState, reset, setError, clearErrors, trigger, getValues) => {
-          console.log(formData, "formData");
-        }}
+     <FormComposerCitizen
+       config={config}
+       onSubmit={onFormSubmit}
+       onFormValueChange={(setValue, formData, formState, reset, setError, clearErrors, trigger, getValues) => {}}
      nextStepLabel={"CMN_NEXT_BTN"} submitLabel={"CMN_SUBMIT"} baseRoute="name"  sessionKey="PGR_CITIZEN_CREATE"
      ></FormComposerCitizen>
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c657b12 and bc4cd89.

📒 Files selected for processing (7)
  • frontend/micro-ui/web/micro-ui-internals/example/package.json (1 hunks)
  • frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/index.js (2 hunks)
  • frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js (1 hunks)
  • frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/citizen/Create/CitizenCreate.js (4 hunks)
  • frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/CreateComplaint/index.js (2 hunks)
  • frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/PGRInbox.js (3 hunks)
  • frontend/micro-ui/web/package.json (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js (3)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/citizen/Create/CitizenCreate.js (2)
  • tenantId (13-13)
  • config (31-63)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/CreateComplaint/index.js (2)
  • tenantId (27-27)
  • getMinMaxValues (60-60)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/PGRInbox.js (2)
  • tenantId (36-36)
  • getMinMaxValues (66-66)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/citizen/Create/CitizenCreate.js (1)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js (3)
  • Digit (42-42)
  • validationRules (68-101)
  • getMinMaxValues (105-118)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/CreateComplaint/index.js (3)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/citizen/Create/CitizenCreate.js (2)
  • isValidationLoading (17-17)
  • tenantId (13-13)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/PGRInbox.js (5)
  • isValidationLoading (45-45)
  • tenantId (36-36)
  • configs (62-62)
  • mdmsData (48-59)
  • getMinMaxValues (66-66)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js (3)
  • Digit (42-42)
  • validationRules (68-101)
  • getMinMaxValues (105-118)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/employee/PGRInbox.js (2)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/pgr/useMobileValidation.js (3)
  • Digit (42-42)
  • validationRules (68-101)
  • getMinMaxValues (105-118)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/configs/PGRSearchInboxConfig.js (2)
  • tenantId (14-14)
  • PGRSearchInboxConfig (13-250)
🔇 Additional comments (4)
frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/pages/citizen/Create/CitizenCreate.js (2)

41-56: Verify validation keys for number inputs.

If FormComposer maps to native input type="number", pattern/minlength/maxlength may be ignored by the browser. Confirm the component enforces these in JS; else consider using a text input with numeric keyboard and pattern enforcement.

Please confirm FormComposer’s behavior for:

  • validation.pattern on number fields
  • validation.minlength/maxlength on number fields

If unsupported, switching field.type to a text-like input that enforces pattern is advisable.


78-80: Loader gating looks good.

frontend/micro-ui/web/micro-ui-internals/packages/modules/pgr/src/hooks/index.js (1)

9-20: Good addition to public Hooks API.

Exporting pgr.useMobileValidation aligns with usage across pages.

frontend/micro-ui/web/micro-ui-internals/example/package.json (1)

14-14: Workspace versions consistent for digit-ui-module-core
Confirmed @egovernments/digit-ui-module-core is at 1.8.55 in all package.json files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants