# Date Validation

Industry: Core

GMT's validation functions return booleans. They never throw — invalid input yields
`false`. The ambiguity of `false` (invalid vs. genuinely not-the-case) is the core
mistake.

## Mistakes

### Not validating before parsing

Severity: high.

Feeding an unvalidated user input string into parseYearFromDate or any other parser produces an empty string or null silently. Validate first, then parse.

Wrong:
```js
const year = parseYearFromDate(userInput); // "not-a-date" → ""
```

Right:
```js
import { isValidDate, parseYearFromDate } from "@northguild/gmt";

if (isValidDate(userInput)) {
  const year = parseYearFromDate(userInput); // safe — input is known-valid
}
```

### Not validating timezone before use

Severity: high.

Passing an invalid IANA timezone to getZonedNow or any zoned function returns an empty string. Always validate the timezone with isValidTimeZone first.

Wrong:
```js
getZonedNow("not-a-zone"); // ""
```

Right:
```js
import { isValidTimeZone, getZonedNow } from "@northguild/gmt/zoned";

if (isValidTimeZone(tz)) {
  getZonedNow(tz); // safe
}
```

### Using try-catch for validation

Severity: medium.

GMT functions never throw — they return sentinel values. A try-catch block around a GMT call will never fire, giving a false sense of safety.

Wrong:
```js
try {
  const r = parseRfc3339(input); // never throws
} catch (e) {
  // never reached
}
```

Right:
```js
import { parseRfc3339 } from "@northguild/gmt/zoned";

const r = parseRfc3339(input);
if (r === "") {
  // handle invalid input
}
```

### Confusing range validators with interval validators

Severity: medium.

isValidDateRange takes one object, { value1, value2 }, and by default requires value1 strictly before value2 (pass options: { allowEqual: true } to accept equal dates). isValidDateInterval takes two positional dates and accepts start === end, an empty interval. Neither takes a range string. They are not interchangeable.

Wrong:
```js
isValidDateRange("2024-03-15/2024-03-20"); // false — not a range string
isValidDateInterval({ start: "2024-03-15", end: "2024-03-20" }); // false — not an object
```

Right:
```js
import { isValidDateRange, isValidDateInterval } from "@northguild/gmt";

isValidDateRange({ value1: "2024-03-15", value2: "2024-03-20" }); // true
isValidDateInterval("2024-03-15", "2024-03-20"); // true
isValidDateRange({ value1: "2024-03-15", value2: "2024-03-15" }); // false — equal dates need allowEqual
isValidDateInterval("2024-03-15", "2024-03-15"); // true — an interval may be empty
```

### Assuming intervals accept reversed bounds

Severity: medium.

isValidDateInterval returns false when start > end. GMT intervals do not auto-normalize reversed bounds — fix the order before validation.

Wrong:
```js
isValidDateInterval("2024-03-20", "2024-03-15"); // false — start > end
```

Right:
```js
import { isValidDateInterval } from "@northguild/gmt";

isValidDateInterval("2024-03-15", "2024-03-20"); // true — correct order
```

## See also

- [Validation guide](/gmt/guides/core-date-operations/validation/)
- [`isValidDate` reference](/gmt/reference/plain/validate/isValidDate/)
- [`isValidDateRange` reference](/gmt/reference/plain/validate/isValidDateRange/)
- [`isValidDateInterval` reference](/gmt/reference/plain/interval/isValidDateInterval/)
- [`isValidTimeZone` reference](/gmt/reference/zoned/validate/isValidTimeZone/)
