@sergiomarques/sword-validation
Version:
Field validation tooling and defaults for SWORD Health Web Clients
46 lines (38 loc) • 1.31 kB
JavaScript
import dayjs from 'dayjs';
import { helpers } from 'vuelidate/lib/validators';
import { splitDate } from './scripts/utils.js';
const validDay = () => {
return helpers.withParams({ type: 'validDay', min: 1, max: 31 }, (date) => {
console.log(date);
const dateSplit = splitDate(date);
if (!dateSplit.day) {
return false;
}
let dateRef = dayjs().set('month', dateSplit.month - 1);
if (dateSplit.year && dateSplit.year.length === 4) {
dateRef = dateRef.set('year', dateSplit.year);
}
const lastDayInMonth = dateRef.endOf('month').$D;
return lastDayInMonth >= dateSplit.day;
});
};
const validMonth = (date) => {
const dateSplit = splitDate(date);
return !!dateSplit.month;
};
const validYear = (min, max) =>
helpers.withParams({ type: 'validYear', min: min, max: max }, (date) => {
const year = splitDate(date).year;
if (!date || !year || year.length < 4) {
return false;
}
return !!year && min <= year && year <= max;
});
const isNotFuture = (date) => {
return date ? !dayjs(date).isAfter(new Date()) : false;
};
const requiredDate = (date) => {
const dateSplit = splitDate(date);
return dateSplit.day && dateSplit.month && dateSplit.year;
};
export { validDay, validMonth, validYear, isNotFuture, requiredDate };