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

Add support for UTC parsing mode #9

Merged
merged 1 commit into from
Apr 18, 2018
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
.c9
.idea
.vscode
.project
*.iml
npm-debug.log
dump.rdb
node_modules
yarn.lock
yarn-error.log
results.tap
results.xml
npm-shrinkwrap.json
Expand Down
21 changes: 20 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ module.exports = (joi) => ({
}

if (options.convert && this._flags.momentFormat) {
const date = Moment(value, this._flags.momentFormat, true);
const date = this._flags.utc
? Moment.utc(value, this._flags.momentFormat, true)
: Moment(value, this._flags.momentFormat, true);

if (date.isValid()) {
return date.toDate();
}
Expand Down Expand Up @@ -51,6 +54,22 @@ module.exports = (joi) => ({
// No-op just to enable description
return value;
}
},
{
name: 'utc',
description(params) {

return 'Date should be interpreted in UTC';
},
setup(params) {

this._flags.utc = true;
},
validate(params, value, state, options) {

// No-op just to enable description
return value;
}
}
]

Expand Down
46 changes: 45 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ describe('date', () => {
], done);
});

it('supports utc mode', (done) => {

Helper.validate(Joi.date().utc().format('YYYY-MM-DD'), [
['2018-01-01', true, null, new Date('2018-01-01:00:00:00.000Z')]
], done);
});

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

expect(() => {
Expand All @@ -88,7 +95,7 @@ describe('date', () => {
});
});

it('should be correctly described', (done) => {
it('should be correctly described (local mode)', (done) => {

const schema = Joi.date().format(['DD#YYYY$MM', 'YY|DD|MM']);
expect(schema.describe()).to.equal({
Expand Down Expand Up @@ -118,5 +125,42 @@ describe('date', () => {
});
done();
});

it('should be correctly described (utc mode)', (done) => {

const schema = Joi.date().utc().format(['DD#YYYY$MM', 'YY|DD|MM']);
expect(schema.describe()).to.equal({
type: 'date',
flags: {
utc: true,
momentFormat: ['DD#YYYY$MM', 'YY|DD|MM']
},
options: {
language: {
date: {
format: 'must be a string with one of the following formats {{format}}'
}
}
},
rules: [
{
name: 'utc',
description: 'Date should be interpreted in UTC',
arg: {}
},
{
name: 'format',
description: 'Date should respect format DD#YYYY$MM,YY|DD|MM',
arg: {
format: [
'DD#YYYY$MM',
'YY|DD|MM'
]
}
}
]
});
done();
});
});
});