Date type
Ensure the field's value is a string formatted as a date or datetime per the expected formats. The return value is an instance of the Date class.
import vine from '@vinejs/vine'
const schema = vine.object({
published_at: vine.date()
})
By default, the input value should be formatted as a YYYY-MM-DD
or YYYY-MM-DD HH:mm:ss
string. However, you may define custom formats as well. In the following example, we expect the input string to be valid per any of the mentioned formats.
You must check the Day.js documentation to view the available formatting tokens.
import vine from '@vinejs/vine'
const schema = vine.object({
published_at: vine.date({
formats: ['YYYY/DD/MM', 'x']
})
})
You may mark the field as optional
or nullable
using the following modifiers.
See also: Working with undefined
and null
values
{
published_at: vine.date().nullable()
}
{
published_at: vine.date().optional()
}
Defining error message
You may define custom error messages for the following date-based rules.
const messages = {
'date': 'The {{ field }} field must be a datetime value',
'date.equals': 'The {{ field }} field must be a date equal to {{ expectedValue }}',
'date.after': 'The {{ field }} field must be a date after {{ expectedValue }}',
'date.before': 'The {{ field }} field must be a date before {{ expectedValue }}',
'date.afterOrEqual': 'The {{ field }} field must be a date after or equal to {{ expectedValue }}',
'date.beforeOrEqual': 'The {{ field }} field must be a date before or equal to {{ expectedValue }}',
'date.sameAs': 'The {{ field }} field and {{ otherField }} field must be the same',
'date.notSameAs': 'The {{ field }} field and {{ otherField }} field must be different',
'date.afterField': 'The {{ field }} field must be a date after {{ otherField }}',
}
vine.messagesProvider = new SimpleMessagesProvider(messages)
Comparing dates
You may use one of the following validation methods to compare the user input against a specific datetime value.
-
equals
: Ensure the input datetime value is the same as the expected datetime value. -
after
: Ensure the input datetime value is after the expected datetime value. -
afterOrEqual
: Same as theafter
method, but performs a greater than and equal to comparison. -
before
: Ensure the input datetime value is before the expected datetime value. -
beforeOrEqual
: Same as thebefore
method, but performs a less than and equal to comparison.
const schema = vine.object({
enrollment_date: vine
.date()
.equals('2024-01-28')
})
const schema = vine.object({
checkin_date: vine
.date()
.after('today')
})
const schema = vine.object({
checkin_date: vine
.date()
.after('2024-01-01')
})
When using dynamic or computed values, you must use a callback function. Otherwise, the initial value will be cached forever with a precompiled schema.
const schema = vine.object({
enrollment_date: vine
.date()
.afterOrEqual((field) => {
return dayjs().add(2, 'day').format('YYYY-MM-DD')
})
})
How is the comparison performed?
Validation rules use the compare
unit to compare the two dates. By default, the compare unit is set to day
, which means the validation will compare the day
, month
, and the year
values.
Similarly, if you set the compare unit to minutes
, the validation will compare minutes
, hours
, day
, month
and the year
.
Under the hood, the comparison is performed using the Days.js query methods.
vine
.date()
.equals('2024-01-28', {
compare: 'month', // compares month and the year
})
Using a custom format
Validation rules assume the expected datetime format to be an ISO string. However, you may use the format
option to specify a custom format.
vine
.date()
.equals('2024/28/01', {
format: 'YYYY/DD/MM',
})
Comparing against other fields
Alongside performing comparison against a fixed datetime value, you may also use the following validation methods to compare the input value against the value of another field.
-
sameAs
: Ensure the input datetime value is the same as the other field's value. -
notSameAs
: Ensure the input datetime value is not the same as the other field's value. -
afterField
: Ensure the input datetime value is after the other field's value. -
afterOrSameAs
: Same as theafterField
rule, but performs a greater than and equal to comparison. -
beforeField
: Ensure the input datetime value is before the other field's value. -
beforeOrSameAs
: Same as thebeforeField
rule, but performs a less than and equal to comparison.
const schema = vine.object({
entry_date: vine.date(),
exit_date: vine.date().sameAs('entry_date')
})
const schema = vine.object({
checkin_date: vine.date(),
checkout_date: vine.date().afterField('checkin_date')
})
You may specify the comparison unit and the format of the other field using the options object.
See also: How is the comparison performed?
const schema = vine.object({
checkin_date: vine.date({
formats: ['YYYY/MM/DD']
}),
checkout_date: vine.date().afterField('checkin_date', {
compare: 'day',
format: ['YYYY/MM/DD']
})
})
Other validation rules
Following is the list of other (non-comparison) validation rules.
weekend
Ensure the date is a weekend.
const schema = vine.object({
checkin_date: vine.date().weekend()
})
weekday
Ensure the date is a weekday.
const schema = vine.object({
event_date: vine.date().weekday()
})