diff --git a/API.md b/API.md index 89daaeac3..4c72a504c 100755 --- a/API.md +++ b/API.md @@ -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) @@ -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. diff --git a/lib/date.js b/lib/date.js index 85b4dfdd0..7dc399caa 100755 --- a/lib/date.js +++ b/lib/date.js @@ -5,7 +5,6 @@ const Any = require('./any'); const Ref = require('./ref'); const Hoek = require('hoek'); -const Moment = require('moment'); // Declare internals @@ -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; @@ -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); @@ -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(); diff --git a/package.json b/package.json index cc52c8083..1225a341d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/any.js b/test/any.js index 6bcb5ef5d..61cdfdc08 100755 --- a/test/any.js +++ b/test/any.js @@ -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'] diff --git a/test/date.js b/test/date.js index dac928487..54638689b 100755 --- a/test/date.js +++ b/test/date.js @@ -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(); - }); - }); }); });