From 9101e92a9d4b0ef2dfce1d2f03ad416b2ef1b895 Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Thu, 17 May 2018 14:26:06 +0300 Subject: [PATCH] Use year/month/date triple when discussing with Stripe. --- .../FieldBirthdayInput/FieldBirthdayInput.js | 6 ++--- src/util/validators.js | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/components/FieldBirthdayInput/FieldBirthdayInput.js b/src/components/FieldBirthdayInput/FieldBirthdayInput.js index c1adb3ac..f952bd36 100644 --- a/src/components/FieldBirthdayInput/FieldBirthdayInput.js +++ b/src/components/FieldBirthdayInput/FieldBirthdayInput.js @@ -29,8 +29,8 @@ const pad = num => { }; const parseNum = str => { - const num = parseInt(str, 10); - return isNaN(num) ? null : num; + const num = Number.parseInt(str, 10); + return Number.isNaN(num) ? null : num; }; // Validate that the given date has the same info as the selected @@ -54,7 +54,7 @@ const dateFromSelected = ({ day, month, year }) => { const yearNum = parseNum(year); if (dayNum !== null && monthNum !== null && yearNum !== null) { const d = new Date(Date.UTC(yearNum, monthNum - 1, dayNum)); - return isValidDate(d, yearNum, monthNum, dayNum) ? d : null; + return isValidDate(d, yearNum, monthNum, dayNum) ? { year, month, day } : null; } return null; }; diff --git a/src/util/validators.js b/src/util/validators.js index dd0c81e0..58bd1fae 100644 --- a/src/util/validators.js +++ b/src/util/validators.js @@ -114,10 +114,26 @@ export const moneySubUnitAmountAtLeast = (message, minValue) => value => { return value instanceof Money && value.amount >= minValue ? VALID : message; }; +const parseNum = str => { + const num = Number.parseInt(str, 10); + return Number.isNaN(num) ? null : num; +}; + export const ageAtLeast = (message, minYears) => value => { - const now = moment(); - const ageInYears = now.diff(moment(value), 'years', true); - return value && value instanceof Date && ageInYears >= minYears ? VALID : message; + const { year, month, day } = value; + const dayNum = parseNum(day); + const monthNum = parseNum(month); + const yearNum = parseNum(year); + + // day, month, and year needs to be numbers + if (dayNum !== null && monthNum !== null && yearNum !== null) { + const now = moment(); + const age = new Date(yearNum, monthNum - 1, dayNum); + const ageInYears = now.diff(moment(age), 'years', true); + + return age && age instanceof Date && ageInYears >= minYears ? VALID : message; + } + return message; }; export const composeValidators = (...validators) => value =>