-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace momentjs by date-fns #37
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,27 +8,16 @@ This version requires **joi** v17 or newer. | |
const Joi = require('joi') | ||
.extend(require('@joi/date')); | ||
|
||
const schema = Joi.date().format('YYYY-MM-DD').utc(); | ||
const schema = Joi.date().format('YYYY-MM-DD'); | ||
``` | ||
|
||
## Rules | ||
|
||
### `date.format(format)` | ||
|
||
Specifies the allowed date format: | ||
- `format` - string or array of strings that follow the `moment.js` [format](http://momentjs.com/docs/#/parsing/string-format/). | ||
- `format` - string that follows the `date-fns` [format](https://date-fns.org/v2.19.0/docs/parse). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably link to: https://date-fns.org/docs/parse |
||
|
||
```js | ||
const schema = Joi.date().format(['YYYY/MM/DD', 'DD-MM-YYYY']); | ||
``` | ||
```js | ||
const schema = Joi.date().format('YYYY-MM-DD HH:mm'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bad example |
||
``` | ||
|
||
### `date.utc()` | ||
|
||
Dates will be parsed as UTC instead of using the machine's local timezone. | ||
|
||
```js | ||
const schema = Joi.date().utc().format(['YYYY/MM/DD', 'DD-MM-YYYY']); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
'use strict'; | ||
|
||
const Moment = require('moment'); | ||
const IsValid = require('date-fns/isValid'); | ||
const Parse = require('date-fns/parse'); | ||
|
||
|
||
const internals = {}; | ||
|
@@ -9,10 +10,7 @@ const internals = {}; | |
module.exports = (joi) => { | ||
|
||
const args = { | ||
format: joi.alternatives([ | ||
joi.string(), | ||
joi.array().items(joi.string().invalid('iso', 'javascript', 'unix')) | ||
]) | ||
format: joi.string() | ||
}; | ||
|
||
return { | ||
|
@@ -29,18 +27,9 @@ module.exports = (joi) => { | |
return; | ||
} | ||
|
||
const date = schema.$_getFlag('utc') ? Moment.utc(value, format, true) : Moment(value, format, true); | ||
if (date.isValid()) { | ||
return { value: date.toDate() }; | ||
} | ||
} | ||
}, | ||
|
||
rules: { | ||
utc: { | ||
method: function (enabled = true) { | ||
|
||
return this.$_setFlag('utc', enabled); | ||
const date = Parse(value, format, new Date()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enabling different results depending in the time of invocation does not seem like a good idea. |
||
if (IsValid(date)) { | ||
return { value: date }; | ||
} | ||
} | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad example, you are parsing 'local week-numbering year' and 'day of a year', which doesn't make sense.