Skip to content

[8423] Add repeat group validator to check properties and fields of items#4804

Open
jvega190 wants to merge 5 commits intocraftercms:developfrom
jvega190:bugfix/8423
Open

[8423] Add repeat group validator to check properties and fields of items#4804
jvega190 wants to merge 5 commits intocraftercms:developfrom
jvega190:bugfix/8423

Conversation

@jvega190
Copy link
Member

@jvega190 jvega190 commented Feb 16, 2026

craftercms/craftercms#8423

Summary by CodeRabbit

  • New Features
    • Added validation for repeating form fields with enforcement of minimum/maximum occurrence constraints.
    • Per-item validation across repeat group entries, aggregating results and surfacing localized messages for occurrence violations to improve form feedback and data integrity.

@coderabbitai
Copy link

coderabbitai bot commented Feb 16, 2026

Warning

Rate limit exceeded

@jvega190 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 12 minutes and 39 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

Added a new exported validator, repeatGroupValidator, and wired it into the existing validators map to validate repeat-group fields (min/max occurrences plus async validation of nested subfields) in ui/app/src/components/FormsEngine/lib/validators.ts.

Changes

Cohort / File(s) Summary
Repeat Group Validation
ui/app/src/components/FormsEngine/lib/validators.ts
Added repeatGroupValidator export and implementation; imported RepeatItem type; updated validatorsMap.repeat to call repeatGroupValidator(field, currentValue as Array<RepeatItem>, messages, meta). Enforces minOccurs/maxOccurs and aggregates async validation of nested fields across repeat items.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • rart
🚥 Pre-merge checks | ✅ 2 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description only provides a ticket reference link without explaining what the PR accomplishes or why the changes were made. Add a detailed description explaining the purpose of the repeat group validator, what problems it solves, and how it validates repeat group properties and nested fields.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a repeat group validator to handle validation of repeat group properties and nested fields.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into develop

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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: 2

🤖 Fix all issues with AI agents
Verify each finding against the current code and only fix it if needed.


In `@ui/app/src/components/FormsEngine/lib/validators.ts`:
- Around line 248-265: The checks for minOccurs and maxOccurs use truthiness
which treats 0 as unset; update the validation in the block around
getPropertyValue(field.properties, 'minOccurs') and 'maxOccurs' to explicitly
test for presence (e.g., minOccurs != null or typeof minOccurs === 'number' /
Number.isFinite(minOccurs)) before comparing currentValue.length, so a
configured 0 is enforced; keep pushing messages via messages?.push with
defineMessage and return false on violation as before.
- Line 247: The repeatGroupValidator currently assumes field.fields exists and
calls Object.values(fields) which can throw if fields is undefined; in the
repeatGroupValidator function (and where const fields = field.fields is
declared) add a defensive guard that returns true when fields is missing (e.g.
if (!fields) return true) so the validator safely handles repeat-group
descriptors without a fields array; alternatively, if you prefer stricter
typing, change the function parameter type from ContentTypeField to
ContentTypeRepeatField to require fields, but the quick fix is to add the guard
before iterating over Object.values(fields).
- Around line 269-288: The aggregation of validation results currently uses a
mutable boolean and a results.forEach loop; replace this with an idiomatic
short-circuiting check using Array.prototype.every: after awaiting
Promise.all(validationPromises) use `results.every(r => r.isValid)` (or return
that directly) to compute and return the final boolean. Update the code around
the variables `validationPromises`, `results`, and
`FieldValidityState`/`validateFieldValue` usage so the function returns the
`every` result instead of the `isValid` flag.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Verify each finding against the current code and only fix it if needed.


In `@ui/app/src/components/FormsEngine/lib/validators.ts`:
- Around line 269-288: The aggregation of validation results currently uses a
mutable boolean and a results.forEach loop; replace this with an idiomatic
short-circuiting check using Array.prototype.every: after awaiting
Promise.all(validationPromises) use `results.every(r => r.isValid)` (or return
that directly) to compute and return the final boolean. Update the code around
the variables `validationPromises`, `results`, and
`FieldValidityState`/`validateFieldValue` usage so the function returns the
`every` result instead of the `isValid` flag.
ui/app/src/components/FormsEngine/lib/validators.ts (1)

269-288: Consider simplifying the validity aggregation.

The forEach + mutable boolean can be replaced with every, which is more idiomatic and short-circuits on the first invalid result.

Suggested refactor
 	const results = await Promise.all(validationPromises);
-	let isValid = true;
-	results.forEach((result) => {
-		if (!result.isValid) {
-			isValid = false;
-		}
-	});
-	return isValid;
+	return results.every((result) => result.isValid);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ui/app/src/components/FormsEngine/lib/validators.ts` around lines 269 - 288,
The aggregation of validation results currently uses a mutable boolean and a
results.forEach loop; replace this with an idiomatic short-circuiting check
using Array.prototype.every: after awaiting Promise.all(validationPromises) use
`results.every(r => r.isValid)` (or return that directly) to compute and return
the final boolean. Update the code around the variables `validationPromises`,
`results`, and `FieldValidityState`/`validateFieldValue` usage so the function
returns the `every` result instead of the `isValid` flag.

@jvega190 jvega190 marked this pull request as ready for review February 16, 2026 22:44
@jvega190 jvega190 requested a review from rart as a code owner February 16, 2026 22:44
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.

🤖 Fix all issues with AI agents
Verify each finding against the current code and only fix it if needed.


In `@ui/app/src/components/FormsEngine/lib/validators.ts`:
- Around line 247-248: The explicit "as number" casts on minOccurs and maxOccurs
mask getPropertyValue's possible undefined/null return and contradict the nnou()
runtime guard; remove the unsafe casts and declare them with a nullable type
(e.g., number | undefined) or let TypeScript infer the correct type from
getPropertyValue, then use the existing nnou(minOccurs) / nnou(maxOccurs) checks
to narrow before any numeric usage; update any downstream usage sites in this
file to respect the nullable type until nnou() has validated them.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Verify each finding against the current code and only fix it if needed.


In `@ui/app/src/components/FormsEngine/lib/validators.ts`:
- Around line 247-248: The explicit "as number" casts on minOccurs and maxOccurs
mask getPropertyValue's possible undefined/null return and contradict the nnou()
runtime guard; remove the unsafe casts and declare them with a nullable type
(e.g., number | undefined) or let TypeScript infer the correct type from
getPropertyValue, then use the existing nnou(minOccurs) / nnou(maxOccurs) checks
to narrow before any numeric usage; update any downstream usage sites in this
file to respect the nullable type until nnou() has validated them.
ui/app/src/components/FormsEngine/lib/validators.ts (1)

247-248: The as number cast contradicts the nnou() guard and masks the actual return type.

getPropertyValue may return undefined/null, so the declared type : number is inaccurate. Although nnou() handles this at runtime, the cast silences any future compiler warnings if these variables are used unsafely elsewhere.

Suggested fix
-	const minOccurs: number = getPropertyValue(field.properties, 'minOccurs') as number;
-	const maxOccurs: number = getPropertyValue(field.properties, 'maxOccurs') as number;
+	const minOccurs = getPropertyValue(field.properties, 'minOccurs') as number | undefined;
+	const maxOccurs = getPropertyValue(field.properties, 'maxOccurs') as number | undefined;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ui/app/src/components/FormsEngine/lib/validators.ts` around lines 247 - 248,
The explicit "as number" casts on minOccurs and maxOccurs mask
getPropertyValue's possible undefined/null return and contradict the nnou()
runtime guard; remove the unsafe casts and declare them with a nullable type
(e.g., number | undefined) or let TypeScript infer the correct type from
getPropertyValue, then use the existing nnou(minOccurs) / nnou(maxOccurs) checks
to narrow before any numeric usage; update any downstream usage sites in this
file to respect the nullable type until nnou() has validated them.

Copy link
Member

@rart rart left a comment

Choose a reason for hiding this comment

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

The validators can return multiple messages. Are we generally stopping after the first error? Are the validation errors able to render multiple errors for a single control?

@jvega190
Copy link
Member Author

@rart multiple error can render for a single control (is supported and other validators do return multiple errors). Updated the validator to avoid returning after first error.

@jvega190 jvega190 requested a review from rart February 19, 2026 20:09
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.

2 participants

Comments