Skip to content

Commit

Permalink
Merge pull request #140 from danielspaniel/master
Browse files Browse the repository at this point in the history
fixed moment date parsing deprecations
  • Loading branch information
offirgolan committed Mar 15, 2016
2 parents 8a0a745 + 458acc7 commit ef27faa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
42 changes: 25 additions & 17 deletions app/validators/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,47 @@ const {
* @extends Base
*/
export default Base.extend({

_parseDate(dateStr, format) {
if (dateStr === 'now' || isEmpty(dateStr)) {
return moment();
} else {
return format ? moment(dateStr, format) : moment(new Date(dateStr));
}
},

validate(value, options) {
var errorFormat = options.errorFormat || 'MMM Do, YYYY';
var now = moment();
var date = moment(value);
let {format, before, after} = options;

if (options.allowBlank && isEmpty(value)) {
return true;
}

let date = this._parseDate(value, format);

if (!date.isValid()) {
return this.createErrorMessage('date', value, options);
}

if (options.format && !moment(value, options.format, true).isValid()) {
if (format && !moment(value, format, true).isValid()) {
return this.createErrorMessage('wrongDateFormat', value, options);
}

if (options.before === 'now') {
options.before = now;
}

if (options.after === 'now') {
options.after = now;
}

if (options.before && (moment(options.before) < date)) {
options.before = moment(options.before).format(errorFormat);
return this.createErrorMessage('before', value, options);
if (before) {
before = this._parseDate(before, format);
if (before < date) {
options.before = before.format(errorFormat);
return this.createErrorMessage('before', value, options);
}
}

if (options.after && (moment(options.after) > date)) {
options.after = moment(options.after).format(errorFormat);
return this.createErrorMessage('after', value, options);
if (after) {
after = this._parseDate(after, format);
if (after > date) {
options.after = after.format(errorFormat);
return this.createErrorMessage('after', value, options);
}
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions tests/dummy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

var App;
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/resolver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Resolver from 'ember/resolver';
import Resolver from 'ember-resolver';
import config from '../../config/environment';

var resolver = Resolver.create();
Expand Down

0 comments on commit ef27faa

Please sign in to comment.