From d13caa54421c43e7360334d2bbc84b1bab178f4e Mon Sep 17 00:00:00 2001 From: Rolf Erik Lekang Date: Thu, 28 Jul 2016 15:14:39 +0200 Subject: [PATCH] fix: Add fallback to iso format for date validator The auto-parsing in moment is deprecated. --- src/rules/date.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/rules/date.js b/src/rules/date.js index 8708b1a..4698528 100644 --- a/src/rules/date.js +++ b/src/rules/date.js @@ -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'; }