Date Validation
isValidDateRange()
Validates that a date range is valid for a reservation.
Signature:
function isValidDateRange(checkIn, checkOut): boolean
checkIn
Temporal.PlainDate
required
The check-in date
checkOut
Temporal.PlainDate
required
The check-out date
True if the date range is valid (check-in is today or future AND check-out is on or after check-in)
Validates:
- Check-in date is today or in the future
- Check-out date is on or after the check-in date
Example:
import { isValidDateRange } from './utils/validation.js'
const checkIn = Temporal.PlainDate.from('2026-03-10')
const checkOut = Temporal.PlainDate.from('2026-03-15')
if (isValidDateRange(checkIn, checkOut)) {
console.log('Date range is valid')
}
isDateTodayOrFuture()
Validates that a date is not in the past.
Signature:
function isDateTodayOrFuture(checkIn): boolean
checkIn
Temporal.PlainDate
required
The date to validate
True if the date is today or in the future, false if in the past
Purpose:
Ensures that a date is not in the past by comparing it with the current date.
Example:
import { isDateTodayOrFuture } from './utils/validation.js'
const futureDate = Temporal.PlainDate.from('2026-12-31')
const pastDate = Temporal.PlainDate.from('2025-01-01')
console.log(isDateTodayOrFuture(futureDate)) // true
console.log(isDateTodayOrFuture(pastDate)) // false
isDateTodayOrPast()
Validates that a date is not in the future.
Signature:
function isDateTodayOrPast(checkIn): boolean
checkIn
Temporal.PlainDate
required
The date to validate
True if the date is today or in the past, false if in the future
Purpose:
Ensures that a date is not in the future by comparing it with the current date.
Example:
import { isDateTodayOrPast } from './utils/validation.js'
const pastDate = Temporal.PlainDate.from('2025-01-01')
const futureDate = Temporal.PlainDate.from('2026-12-31')
console.log(isDateTodayOrPast(pastDate)) // true
console.log(isDateTodayOrPast(futureDate)) // false
isDateInRange()
Validates that a date falls within a specific date range.
Signature:
function isDateInRange(date, checkIn, checkOut): boolean
date
Temporal.PlainDate
required
The date to check
checkIn
Temporal.PlainDate
required
The start of the date range (inclusive)
checkOut
Temporal.PlainDate
required
The end of the date range (inclusive)
True if the date is within the range (inclusive), false otherwise
Example:
import { isDateInRange } from './utils/validation.js'
const date = Temporal.PlainDate.from('2026-03-12')
const checkIn = Temporal.PlainDate.from('2026-03-10')
const checkOut = Temporal.PlainDate.from('2026-03-15')
console.log(isDateInRange(date, checkIn, checkOut)) // true
The range is inclusive on both ends - dates equal to checkIn or checkOut will return true.
isRangeInRange()
Validates that one date range falls completely within another date range.
Signature:
function isRangeInRange(dateStart, dateEnd, checkIn, checkOut): boolean
dateStart
Temporal.PlainDate
required
The start date of the outer range
dateEnd
Temporal.PlainDate
required
The end date of the outer range
checkIn
Temporal.PlainDate
required
The start date of the inner range to check
checkOut
Temporal.PlainDate
required
The end date of the inner range to check
True if both checkIn and checkOut fall within the dateStart-dateEnd range
Purpose:
Checks if a date range (checkIn to checkOut) is completely contained within another date range (dateStart to dateEnd).
Example:
import { isRangeInRange } from './utils/validation.js'
const outerStart = Temporal.PlainDate.from('2026-03-01')
const outerEnd = Temporal.PlainDate.from('2026-03-31')
const innerStart = Temporal.PlainDate.from('2026-03-10')
const innerEnd = Temporal.PlainDate.from('2026-03-15')
console.log(isRangeInRange(outerStart, outerEnd, innerStart, innerEnd)) // true
isEndDateOnOrAfterStartDate()
Validates that the end date is not before the start date.
Signature:
function isEndDateOnOrAfterStartDate(checkIn, checkOut): boolean
checkIn
Temporal.PlainDate
required
The start date
checkOut
Temporal.PlainDate
required
The end date
True if checkOut is on or after checkIn, false if checkOut is before checkIn
Example:
import { isEndDateOnOrAfterStartDate } from './utils/validation.js'
const checkIn = Temporal.PlainDate.from('2026-03-10')
const checkOut = Temporal.PlainDate.from('2026-03-15')
console.log(isEndDateOnOrAfterStartDate(checkIn, checkOut)) // true
Reservation Validation
validateReservation()
Comprehensively validates that a reservation can be created.
Signature:
function validateReservation(hotel, roomNumber, guestData, checkIn, checkOut): ValidationResult
The room number to reserve
The guest information object containing email, dni, and phone
checkIn
Temporal.PlainDate
required
The check-in date
checkOut
Temporal.PlainDate
required
The check-out date
Object with status (boolean) and message (string) properties
Validates:
- The room exists
- The room is available for the specified dates
- The date range is valid
- The stay is 14 nights or less
- Guest data is valid (email, DNI, phone)
Example:
import { validateReservation } from './utils/validation.js'
const guestData = {
name: 'John Doe',
email: '[email protected]',
dni: '12345678A',
phone: '+34123456789'
}
const validation = validateReservation(
hotel,
101,
guestData,
Temporal.PlainDate.from('2026-03-10'),
Temporal.PlainDate.from('2026-03-15')
)
if (validation.status) {
console.log('Reservation is valid')
} else {
console.error(validation.message)
}
Possible Error Messages:
"La habitación número {roomNumber} no existe."
"La habitación número {roomNumber} está reservada para esas fechas."
"El rango de fechas no es válido."
"Solo se puede reservar hasta 14 noches."
"Los datos del huesped no son válidos"
This is the main validation function that should be used before creating a reservation. It combines all necessary checks in a single call.
Guest Data Validation
Email Validation
Validates email addresses using regex pattern matching.
Regex Pattern:
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Pattern Breakdown:
^[a-zA-Z0-9._-]+ - One or more alphanumeric characters, dots, underscores, or hyphens before @
@ - Required @ symbol
[a-zA-Z0-9.-]+ - One or more alphanumeric characters, dots, or hyphens for domain
\. - Required dot before TLD
[a-zA-Z]{2,}$ - Two or more letters for top-level domain
Valid Examples:
DNI Validation
Validates Spanish DNI (Documento Nacional de Identidad) format.
Regex Pattern:
Pattern Breakdown:
^\d{1,8} - 1 to 8 digits at the start
[A-Za-z]$ - Exactly one letter (uppercase or lowercase) at the end
Valid Examples:
"12345678A" // ✓ Valid (8 digits + letter)
"1234567B" // ✓ Valid (7 digits + letter)
"123456C" // ✓ Valid (6 digits + letter)
"1A" // ✓ Valid (1 digit + letter)
"123456789A" // ✗ Invalid (9 digits, max is 8)
"12345678" // ✗ Invalid (no letter)
"A12345678" // ✗ Invalid (letter first)
This validation only checks the format, not the validity of the check letter according to the official DNI algorithm.
Phone Validation
Validates phone numbers with optional country code.
Function:
export function isValidPhone(phone): boolean
Regex Pattern:
/^(\+?\d{1,3})?[\s.-]?\d{9}$/
Pattern Breakdown:
^(\+?\d{1,3})? - Optional country code: optional +, followed by 1-3 digits
[\s.-]? - Optional separator (space, dot, or hyphen)
\d{9}$ - Exactly 9 digits for the phone number
The phone number to validate
True if the phone number matches the pattern, false otherwise
Valid Examples:
import { isValidPhone } from './utils/validation.js'
console.log(isValidPhone('123456789')) // ✓ Valid (9 digits)
console.log(isValidPhone('+34123456789')) // ✓ Valid (country code + 9 digits)
console.log(isValidPhone('+1 123456789')) // ✓ Valid (country code, space, 9 digits)
console.log(isValidPhone('34.123456789')) // ✓ Valid (country code, dot separator)
console.log(isValidPhone('+999-123456789')) // ✓ Valid (3-digit country code, hyphen)
console.log(isValidPhone('12345678')) // ✗ Invalid (only 8 digits)
console.log(isValidPhone('+12345678901')) // ✗ Invalid (too many digits after code)
This function is exported and can be used independently for phone validation throughout the application.