@reis/joi-luxon
v3.0.0
Published
A Joi extension to automatically convert ISO date strings to Luxon DateTime objects
Downloads
205
Maintainers
Readme
@reis/joi-luxon
A Joi extension to automatically convert ISO date strings to Luxon DateTime objects
Features
- Validates incoming date values as ISO date strings
- Automatically converts date strings to Luxon
DateTime
objects - Able to set timezone and use modifiers like
startOf
orendOf
- Able to validate against other dates or Joi references using functions like
gt
,lt
,gte
orlte
Installation
#npm
npm install @reis/joi-luxon
#yarn
yarn add @reis/joi-luxon
Usage
import Joi from 'joi'
import JoiLuxon from '@reis/joi-luxon'
export default Joi.extend(JoiLuxon)
Examples
Validate a start and end date, setting the date to start and end of day respectively and validating that the end date is after the start date:
const schema = Joi.object({
startDate: Joi
.luxon()
.setZone(timezone)
.startOf('day'),
endDate: Joi
.luxon()
.setZone(timezone)
.endOf('day')
.gt(Joi.ref('startDate')),
})
Set the value to a given minimum date if it’s less than that:
const schema = Joi.object({
date: Joi
.luxon()
.required()
.min(Joi.ref('$minDate'))
})
const schema = Joi.object({
date: Joi
.luxon()
.required()
.min(new Date())
})
All available rules and helpers:
Joi
//Base validation and conversion to Luxon DateTime object
.luxon()
//Sets the timezone on the object
.setZone(timezone)
//Modified the DateTime instance to the start/end of the given period
.startOf(period)
.endOf(period)
//Validates silently against a minimum / maximum date
.min(date)
.max(date)
//Validate and throw errors if the date is less than or greater than a reference date
.lt(date)
.gt(date)
.lte(date)
.gte(date)
Issues & feature requests
Please report any bugs, issues, suggestions and feature requests in the joi-luxon issue tracker.
License
(MIT License)
Copyright 2022-2023, Adam Reis