-
Notifications
You must be signed in to change notification settings - Fork 10
Referring mobile number validations from global configs or from mdms #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughAdds 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
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this 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 undefinedgetMinMaxValuesusage
getMinMaxValues()is invoked here, but the hook destructuring above never pulls it out, sogetMinMaxValuesis undefined and this block will throw immediately whenvalidationRulesresolves. Destructure it fromuseMobileValidationbefore 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
📒 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.
Summary by CodeRabbit