AdonisJS v6 is here. Learn more in our release blog post.

before

Validates the value to be before a given date/offset. The rule can be only be used with the date schema type.

import { schema, rules } from '@ioc:Adonis/Core/Validator'
{
joining_date: schema.date({}, [
rules.before(2, 'days')
])
}

The rules.before method accepts a duration and the offset for the duration. Following are some of the examples for the same. You can use the TypeScript intellisense to discover rest of the available offsets.

rules.before(2, 'days')
rules.before(1, 'month')
rules.before(4, 'years')
rules.before(30, 'minutes')

You can also pass the one of the following shorthand keywords.

rules.before('today')
rules.before('yesterday')

Also, you can make use of the beforeOrEqual for enforcing the date to be same or after a given date.

{
joining_date: schema.date({}, [
rules.beforeOrEqual('today')
])
}

Using Luxon dates

For more advanced use cases, you can pass an instance of the luxon DateTime object. Do make sure to pass the value as a ref.

import { DateTime } from 'luxon'
import { schema, rules } from '@ioc:Adonis/Core/Validator'
class UserValidator {
public refs = schema.refs({
allowedDate: DateTime.local().minus({ days: 2 })
})
public schema = schema.create({
checkin_date: schema.date({}, [
rules.before(this.refs.allowedDate)
])
})
}