|
|
@@ -1,8 +1,5 @@
|
|
|
import { ApiClientError } from "@/lib/frontend/apiClient";
|
|
|
-import {
|
|
|
- isValidIsoDateYmd,
|
|
|
- isInvalidIsoDateRange,
|
|
|
-} from "@/lib/frontend/search/dateRange";
|
|
|
+import { getSearchDateRangeValidation } from "@/lib/frontend/search/searchDateValidation";
|
|
|
|
|
|
function isNonEmptyString(value) {
|
|
|
return typeof value === "string" && value.trim().length > 0;
|
|
|
@@ -12,21 +9,15 @@ function toTrimmedOrNull(value) {
|
|
|
return isNonEmptyString(value) ? value.trim() : null;
|
|
|
}
|
|
|
|
|
|
-function buildValidationError(code, message, details) {
|
|
|
- return new ApiClientError({
|
|
|
- status: 400,
|
|
|
- code,
|
|
|
- message,
|
|
|
- details,
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Build a local validation error for date filters.
|
|
|
*
|
|
|
* This is intentionally independent from the search query "q" so the UI can
|
|
|
* validate immediately while the user edits the date range.
|
|
|
*
|
|
|
+ * Single Source of Truth:
|
|
|
+ * - Uses getSearchDateRangeValidation(...) as the canonical validator.
|
|
|
+ *
|
|
|
* @param {{ from?: string|null, to?: string|null }} input
|
|
|
* @returns {ApiClientError|null}
|
|
|
*/
|
|
|
@@ -37,28 +28,13 @@ export function buildDateFilterValidationError({
|
|
|
const f = toTrimmedOrNull(from);
|
|
|
const t = toTrimmedOrNull(to);
|
|
|
|
|
|
- if (f && !isValidIsoDateYmd(f)) {
|
|
|
- return buildValidationError("VALIDATION_SEARCH_DATE", "Invalid from date", {
|
|
|
- from: f,
|
|
|
- });
|
|
|
- }
|
|
|
+ const v = getSearchDateRangeValidation(f, t);
|
|
|
+ if (!v) return null;
|
|
|
|
|
|
- if (t && !isValidIsoDateYmd(t)) {
|
|
|
- return buildValidationError("VALIDATION_SEARCH_DATE", "Invalid to date", {
|
|
|
- to: t,
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // IMPORTANT:
|
|
|
- // - from === to is valid and represents a single-day search.
|
|
|
- // - Only from > to is invalid.
|
|
|
- if (isInvalidIsoDateRange(f, t)) {
|
|
|
- return buildValidationError(
|
|
|
- "VALIDATION_SEARCH_RANGE",
|
|
|
- "Invalid date range",
|
|
|
- { from: f, to: t }
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
+ return new ApiClientError({
|
|
|
+ status: 400,
|
|
|
+ code: v.code,
|
|
|
+ message: v.message,
|
|
|
+ details: v.details,
|
|
|
+ });
|
|
|
}
|