Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor

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.

```

## 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).
Copy link
Contributor

Choose a reason for hiding this comment

The 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');
Copy link
Contributor

Choose a reason for hiding this comment

The 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']);
```
23 changes: 6 additions & 17 deletions lib/index.js
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 = {};
Expand All @@ -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 {
Expand All @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The 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 };
}
}
},
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
"schema",
"validation",
"date",
"moment",
"joi",
"extension"
],
"dependencies": {
"moment": "2.x.x"
"date-fns": "2.x.x"
},
"devDependencies": {
"@hapi/code": "8.x.x",
Expand Down
92 changes: 29 additions & 63 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const Code = require('@hapi/code');
const Joi = require('@hapi/joi');
const JoiDate = require('..');
const Lab = require('@hapi/lab');
const Moment = require('moment');

const Helper = require('./helper');

Expand All @@ -24,7 +23,7 @@ describe('date', () => {

it('validates an empty date', () => {

const schema = custom.date().format('YYYY-MM-DD');
const schema = custom.date().format('yyyy-MM-dd');
expect(schema.validate(undefined).error).to.not.exist();
});

Expand All @@ -44,7 +43,7 @@ describe('date', () => {
}]
]);

Helper.validate(custom.date().format('YYYY-MM-DD'), [
Helper.validate(custom.date().format('yyyy-MM-dd'), [
[now, true, new Date(now)],
[date, true],
[new Date(NaN), false, {
Expand All @@ -54,10 +53,10 @@ describe('date', () => {
context: { value: new Date(NaN), label: 'value' }
}],
['xxx', false, {
message: '"value" must be in YYYY-MM-DD format',
message: '"value" must be in yyyy-MM-dd format',
path: [],
type: 'date.format',
context: { value: 'xxx', label: 'value', format: 'YYYY-MM-DD' }
context: { value: 'xxx', label: 'value', format: 'yyyy-MM-dd' }
}]
]);
});
Expand All @@ -78,7 +77,7 @@ describe('date', () => {

it('errors without convert enabled', () => {

Helper.validate(custom.date().format('YYYY-MM-DD').options({ convert: false }), [
Helper.validate(custom.date().format('yyyy-MM-dd').options({ convert: false }), [
['2000-01-01', false, {
message: '"value" must be a valid date',
path: [],
Expand All @@ -99,119 +98,86 @@ describe('date', () => {

it('validates custom format', () => {

Helper.validate(custom.date().format('DD#YYYY$MM'), [
['07#2013$06', true, Moment('07#2013$06', 'DD#YYYY$MM', true).toDate()],
['2013-06-07', false, {
message: '"value" must be in DD#YYYY$MM format',
path: [],
type: 'date.format',
context: { value: '2013-06-07', label: 'value', format: 'DD#YYYY$MM' }
}]
]);
});

it('enforces format', () => {

const schema = custom.date().format('YYYY-MM-DD');

Helper.validate(schema, [
['1', false, '"value" must be in YYYY-MM-DD format'],
['10', false, '"value" must be in YYYY-MM-DD format'],
['1000', false, '"value" must be in YYYY-MM-DD format'],
['100x', false, '"value" must be in YYYY-MM-DD format'],
['1-1', false, '"value" must be in YYYY-MM-DD format']
]);
});

it('validates several custom formats', () => {

const schema = custom.date()
.format(['DD#YYYY$MM', 'YY|DD|MM']);
.format('dd$yy#MM');

Helper.validate(schema, [
['13|07|06', true, Moment('13|07|06', 'YY|DD|MM', true).toDate()],
['13$07#06', true, new Date(2007, 5, 13)],
['2013-06-07', false, {
message: '"value" must be in [DD#YYYY$MM, YY|DD|MM] format',
message: '"value" must be in dd$yy#MM format',
path: [],
type: 'date.format',
context: { value: '2013-06-07', label: 'value', format: ['DD#YYYY$MM', 'YY|DD|MM'] }
context: { value: '2013-06-07', label: 'value', format: 'dd$yy#MM' }
}]
]);
});
it('enforces format', () => {

it('supports utc mode', () => {
const schema = custom.date().format('yyyy-MM-dd');

Helper.validate(custom.date().utc().format('YYYY-MM-DD'), [
['2018-01-01', true, new Date('2018-01-01:00:00:00.000Z')]
Helper.validate(schema, [
['1', false, '"value" must be in yyyy-MM-dd format'],
['10', false, '"value" must be in yyyy-MM-dd format'],
['1000', false, '"value" must be in yyyy-MM-dd format'],
['100x', false, '"value" must be in yyyy-MM-dd format'],
['1-1', false, '"value" must be in yyyy-MM-dd format']
]);
});

it('fails with bad formats', () => {

expect(() => custom.date().format(true)).to.throw('Invalid format "value" must be one of [string, array]');
expect(() => custom.date().format([true])).to.throw('Invalid format "[0]" must be a string');
expect(() => custom.date().format(true)).to.throw('Invalid format "value" must be a string');
});

it('fails without convert', () => {

const schema = custom.date().format('YYYY-MM-DD');
const schema = custom.date().format('yyyy-MM-dd');
expect(schema.validate('foo', { convert: false }).error).to.be.an.error('"value" must be a valid date');
});

it('fails with overflow dates', () => {

Helper.validate(custom.date().format('YYYY-MM-DD'), [
Helper.validate(custom.date().format('yyyy-MM-dd'), [
['1999-02-31', false, {
message: '"value" must be in YYYY-MM-DD format',
message: '"value" must be in yyyy-MM-dd format',
path: [],
type: 'date.format',
context: { value: '1999-02-31', label: 'value', format: 'YYYY-MM-DD' }
context: { value: '1999-02-31', label: 'value', format: 'yyyy-MM-dd' }
}],
['2005-13-01', false, {
message: '"value" must be in YYYY-MM-DD format',
message: '"value" must be in yyyy-MM-dd format',
path: [],
type: 'date.format',
context: { value: '2005-13-01', label: 'value', format: 'YYYY-MM-DD' }
context: { value: '2005-13-01', label: 'value', format: 'yyyy-MM-dd' }
}],
['2010-01-32', false, {
message: '"value" must be in YYYY-MM-DD format',
message: '"value" must be in yyyy-MM-dd format',
path: [],
type: 'date.format',
context: { value: '2010-01-32', label: 'value', format: 'YYYY-MM-DD' }
context: { value: '2010-01-32', label: 'value', format: 'yyyy-MM-dd' }
}]
]);
});

it('should support .allow()', () => {

const schema = custom.date().format('YYYY-MM-DD').allow('epoch');
const schema = custom.date().format('yyyy-MM-dd').allow('epoch');
expect(schema.validate('epoch')).to.equal({ value: 'epoch' });
});

describe('describe()', () => {

it('describes custom formats', () => {

const schema = custom.date().format(['DD#YYYY$MM', 'YY|DD|MM']);
const schema = custom.date().format('dd#yyyy$MM');
expect(schema.describe()).to.equal({
type: 'date',
flags: {
format: ['DD#YYYY$MM', 'YY|DD|MM']
format: 'dd#yyyy$MM'
}
});
});

it('describes utc mode', () => {

const schema = custom.date().utc().format(['DD#YYYY$MM', 'YY|DD|MM']);
expect(schema.describe()).to.equal({
type: 'date',
flags: {
format: ['DD#YYYY$MM', 'YY|DD|MM'],
utc: true
}
});
});
});
});
});