Skip to content

Commit

Permalink
fix: Add fallback to iso format for date validator
Browse files Browse the repository at this point in the history
The auto-parsing in moment is deprecated.
  • Loading branch information
relekang committed Jul 28, 2016
1 parent ccb3164 commit d13caa5
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/rules/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ import { get } from 'lodash';
import moment from 'moment';

export default function date(field, value, options) {
if (!moment(value, get(options, 'format'), true).isValid()) {
const format = get(options, 'format', moment.ISO_8601);
if (!moment(value, format, true).isValid()) {
return 'date.format';
}

if (get(options, 'past') && moment(value).diff(moment(), 'days') >= 0) {
const parsedValue = moment(value, format, true);

if (get(options, 'past') && parsedValue.diff(moment(), 'days') >= 0) {
return 'date.past';
}

if (get(options, 'future') && moment(value).diff(moment(), 'days') <= 0) {
if (get(options, 'future') && parsedValue.diff(moment(), 'days') <= 0) {
return 'date.future';
}

const minDate = get(options, 'min');
if (minDate && moment(minDate).diff(moment(value), 'days') > 0) {
if (minDate && moment(minDate, format).diff(parsedValue, 'days') > 0) {
return 'date.min';
}

const maxDate = get(options, 'max');
if (maxDate && moment(maxDate).diff(moment(value), 'days') < 0) {
if (maxDate && moment(maxDate, format).diff(parsedValue, 'days') < 0) {
return 'date.max';
}

Expand Down

0 comments on commit d13caa5

Please sign in to comment.