Skip to content

Commit

Permalink
Remove moment. Fixes #985.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Nov 12, 2016
1 parent 1e045e9 commit 2963e1b
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 70 deletions.
10 changes: 0 additions & 10 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
- [`date`](#date)
- [`date.min(date)`](#datemindate)
- [`date.max(date)`](#datemaxdate)
- [`date.format(format)`](#dateformatformat)
- [`date.iso()`](#dateiso)
- [`date.timestamp([type])`](#datetimestamptype)
- [`func`](#func)
Expand Down Expand Up @@ -940,15 +939,6 @@ const schema = Joi.object({
});
```
#### `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/).
```js
const schema = Joi.date().format('YYYY/MM/DD');
```
#### `date.iso()`
Requires the string value to be in valid ISO 8601 date format.
Expand Down
29 changes: 4 additions & 25 deletions lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
const Any = require('./any');
const Ref = require('./ref');
const Hoek = require('hoek');
const Moment = require('moment');


// Declare internals
Expand Down Expand Up @@ -45,23 +44,18 @@ internals.Date = class extends Any {
result.errors = this.createError('date.strict', null, state, options);
}
else {
let context = null;
let type;
if (internals.isIsoDate(this._flags.format)) {
type = 'isoDate';
}
else if (this._flags.timestamp) {
type = 'timestamp.' + this._flags.timestamp;
}
else if (this._flags.format) {
type = 'format';
context = { format: this._flags.format };
type = `timestamp.${this._flags.timestamp}`;
}
else {
type = 'base';
}

result.errors = this.createError('date.' + type, context, state, options);
result.errors = this.createError(`date.${type}`, null, state, options);
}

return result;
Expand All @@ -83,14 +77,8 @@ internals.Date = class extends Any {
}

let date;
if (format) {
if (internals.isIsoDate(format)) {
date = format.test(value) ? new Date(value) : internals.invalidDate;
}
else {
date = Moment(value, format, true);
date = date.isValid() ? date.toDate() : internals.invalidDate;
}
if (format && internals.isIsoDate(format)) {
date = format.test(value) ? new Date(value) : internals.invalidDate;
}
else if (timestamp && multiplier) {
date = new Date(value * multiplier);
Expand All @@ -107,15 +95,6 @@ internals.Date = class extends Any {
return null;
}

format(format) {

Hoek.assert(typeof format === 'string' || (Array.isArray(format) && format.every((f) => typeof f === 'string')), 'Invalid format.');

const obj = this.clone();
obj._flags.format = format;
return obj;
}

iso() {

const obj = this.clone();
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"hoek": "4.x.x",
"isemail": "2.x.x",
"items": "2.x.x",
"moment": "2.x.x",
"topo": "2.x.x"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('any', () => {
[Joi.array(), '[1,2,3]'],
[Joi.binary(), 'abc'],
[Joi.boolean(), false],
[Joi.date().format('YYYYMMDD'), '19700101'],
[Joi.date(), '1970/01/01'],
[Joi.number(), '12'],
[Joi.object(), '{ "a": 1 }'],
[Joi.any().strict(), 'abc']
Expand Down
33 changes: 0 additions & 33 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,38 +424,5 @@ describe('date', () => {
done();
});
});

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

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

Helper.validate(Joi.date().format('DD#YYYY$MM'), [
['07#2013$06', true],
['2013-06-07', false, null, '"value" must be a string with one of the following formats DD#YYYY$MM']
], done);
});

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

Helper.validate(Joi.date().format(['DD#YYYY$MM', 'YY|DD|MM']), [
['13|07|06', true],
['2013-06-07', false, null, '"value" must be a string with one of the following formats [DD#YYYY$MM, YY|DD|MM]']
], done);
});

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

expect(() => {

Joi.date().format(true);
}).to.throw('Invalid format.');

expect(() => {

Joi.date().format(['YYYYMMDD', true]);
}).to.throw('Invalid format.');
done();
});
});
});
});

0 comments on commit 2963e1b

Please sign in to comment.