Opening hours validation accepts input the server cannot parse #3

Closed
opened 2026-10-10 17:01:53 +02:00 by pepa · 0 comments
Owner

Found while writing unit tests (#2).

Problem

WeekHours validation (src/backend/data.rs) accepts strings that DayHours::try_new then rejects. The form passes client-side validation, update_hours fails on DayHours::try_new(hours.hours())? and the user sees a generic server error instead of a validation message.

Verified inputs — all pass WeekHours::validate() and all fail DayHours::try_new():

  • 08:00 - 12:00, nonsense
  • 08:00 - 12:00 garbage
  • 08:00 - 12:00, (trailing comma)

Cause

  • RE_HOURS is ^$|(^\d{2}:\d{2} ?- ?\d{2}:\d{2} ?(\(\d+\))?,? ?)+ — it has no $ at the end, so anything after the first valid interval is accepted. The ^ inside the repeated group also means the group can only match once.
  • validate_hours returns Ok(()) when DayHours::try_new fails, so the parse error is silently ignored there too.

Suggested fix

  • Anchor the regex at the end and move ^ out of the repeated group, or drop the regex and validate by calling DayHours::try_new directly.
  • Make validate_hours return a validation error when parsing fails.
  • Add unit tests for the inputs above to week_hours_rejects_wrong_format.
Found while writing unit tests (#2). ## Problem `WeekHours` validation (`src/backend/data.rs`) accepts strings that `DayHours::try_new` then rejects. The form passes client-side validation, `update_hours` fails on `DayHours::try_new(hours.hours())?` and the user sees a generic server error instead of a validation message. Verified inputs — all pass `WeekHours::validate()` and all fail `DayHours::try_new()`: - `08:00 - 12:00, nonsense` - `08:00 - 12:00 garbage` - `08:00 - 12:00,` (trailing comma) ## Cause - `RE_HOURS` is `^$|(^\d{2}:\d{2} ?- ?\d{2}:\d{2} ?(\(\d+\))?,? ?)+` — it has no `$` at the end, so anything after the first valid interval is accepted. The `^` inside the repeated group also means the group can only match once. - `validate_hours` returns `Ok(())` when `DayHours::try_new` fails, so the parse error is silently ignored there too. ## Suggested fix - Anchor the regex at the end and move `^` out of the repeated group, or drop the regex and validate by calling `DayHours::try_new` directly. - Make `validate_hours` return a validation error when parsing fails. - Add unit tests for the inputs above to `week_hours_rejects_wrong_format`.
pepa closed this issue 2026-10-10 17:15:44 +02:00
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ITsolved/rezervovator#3