Implement stateless ReportFieldValidator
epic-structured-post-session-report-foundation-task-008 — Implement the stateless ReportFieldValidator service that accepts a field definition (FieldConfig) and a raw value, then returns a ValidationResult. Support validation rules: required, minLength, maxLength, regex pattern, min/max numeric range, allowed select options, and date range constraints. All validation logic must be pure functions with no side effects, enabling parallel validation of all fields in a report before submission.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Model ValidationResult as a sealed class or a simple immutable data class using Dart's `final` fields — avoid using exceptions for validation failures (return types are cleaner and force callers to handle errors). Store the rule evaluation order in FieldConfig as a `List
Export only the public surface (`validate`, `validateAll`, `FieldConfig`, `ValidationResult`) from a single `report_field_validator.dart` barrel file; keep rule implementations in private helpers. This makes the dependency boundary explicit and easy to mock in integration tests.
Testing Requirements
Covered by task-011. Internally: implement as a single Dart library file with no framework dependencies so it can be tested with `dart test` without spinning up Flutter. Ensure all 7 rule types have at least one passing and one failing test case, plus boundary and null-value edge cases. Parallel validation path (validateAll) must be tested with a 20-field fixture to confirm no shared mutable state.
Supabase RLS policies for multi-org report access may be more complex than anticipated — coordinators need cross-peer-mentor access within their org but not across orgs, and draft reports should be invisible to coordinators until submitted. Misconfigured RLS could expose sensitive health data or block legitimate access.
Mitigation & Contingency
Mitigation: Define and test RLS policies in isolation before writing repository code. Create a dedicated SQL migration file with policy definitions and an automated integration test suite that verifies each role's access boundaries using real Supabase auth tokens.
Contingency: If RLS proves too complex to express declaratively, implement application-level access control in the repository layer with explicit org and role checks, and add a security audit task before the feature goes to production.
The org field config JSON stored in Supabase may lack a stable, versioned schema contract. If different organisations have drifted to different field-definition formats, org-field-config-loader will fail silently or crash, breaking form rendering for those orgs.
Mitigation & Contingency
Mitigation: Define a canonical JSON Schema for field config and validate all existing org configs against it before implementation begins. Store a schema version field in every config record and handle version migrations explicitly in the loader.
Contingency: If existing configs are too heterogeneous, implement a config normalisation pass in org-field-config-loader that coerces known variants to the canonical format, logging warnings for fields that cannot be normalised so operations can fix them in the admin console.
TTL-based schema cache invalidation may cause peer mentors to use stale field definitions for up to the TTL window after an admin updates the org config, potentially collecting data against outdated field structures.
Mitigation & Contingency
Mitigation: Set a conservative TTL (e.g. 15 minutes) and expose a manual cache-bust mechanism triggered on app foreground-resume. Document the maximum staleness window in the admin console so org admins know to plan config changes outside active reporting windows.
Contingency: If stale schema causes a data quality incident, add a Supabase Realtime subscription to the org config table that invalidates the cache immediately on any config update.