diff --git a/db/bill.ts b/db/bill.ts index 623a6f5..8c47808 100644 --- a/db/bill.ts +++ b/db/bill.ts @@ -34,7 +34,8 @@ const BillSchema = new mongoose.Schema( return v <= bill.milageEnd }, - message: (props) => `${props.value} is bigger than milageEnd!`, + message: (props: { value: Number }) => + `${props.value} is bigger than milageEnd!`, }, }, milageEnd: { @@ -47,7 +48,8 @@ const BillSchema = new mongoose.Schema( return v >= bill.milageStart }, - message: (props) => `${props.value} is smaller than milageStart!`, + message: (props: { value: Number }) => + `${props.value} is smaller than milageStart!`, }, }, tarif: { diff --git a/db/booking.ts b/db/booking.ts index 91819a4..fd5f131 100644 --- a/db/booking.ts +++ b/db/booking.ts @@ -1,6 +1,7 @@ import * as mongoose from 'mongoose' import { v4 as uuidv4 } from 'uuid' import { dateFormatBackend, getDays, nowInTz } from '../helpers/date' + import { Bill } from './bill' import { Booker } from './booker' import { BOOKING_STATUS } from './enums' @@ -49,13 +50,23 @@ const BookingSchema = new mongoose.Schema( type: Date, required: true, get: dateFormatBackend, - min: nowInTz(), + validate: { + validator: function (v: Date) { + return v >= nowInTz() + }, + message: (props: { value: Date }) => `${props.value} is in the past`, + }, }, endDate: { type: Date, required: false, get: dateFormatBackend, - min: nowInTz(), + validate: { + validator: function (v: Date) { + return v >= nowInTz() + }, + message: (props: { value: Date }) => `${props.value} is in the past`, + }, }, days: { type: [String], diff --git a/db/index.ts b/db/index.ts index e51a5b6..9df41ac 100644 --- a/db/index.ts +++ b/db/index.ts @@ -72,14 +72,11 @@ export async function createBooking({ bookedDays.includes(day) ) if (doubleBookedDays.length) { - const error = new mongoose.Error.ValidationError(booking) - error.addError( - 'days', - new mongoose.Error.ValidatorError({ - message: `${doubleBookedDays - .map((dateString) => dateFormatFrontend(new Date(dateString))) - .join(', ')} schon gebucht`, - }) + const error = new mongoose.Error.ValidationError(booking.uuid) + error.errors.days = new mongoose.Error.ValidatorError( + `${doubleBookedDays + .map((dateString) => dateFormatFrontend(new Date(dateString))) + .join(', ')} schon gebucht` ) throw error }