Use year/month/date triple when discussing with Stripe.

This commit is contained in:
Vesa Luusua 2018-05-17 14:26:06 +03:00
parent 63afe46b90
commit 9101e92a9d
2 changed files with 22 additions and 6 deletions

View file

@ -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;
};

View file

@ -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 =>