allow partial dates (#6333)

This commit is contained in:
Gykes 2025-11-27 18:55:18 -06:00 committed by GitHub
parent 97c01c70b3
commit 88747b962a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View file

@ -1571,7 +1571,7 @@
"urls": "URLs", "urls": "URLs",
"validation": { "validation": {
"blank": "${path} must not be blank", "blank": "${path} must not be blank",
"date_invalid_form": "${path} must be in YYYY-MM-DD form", "date_invalid_form": "${path} must be in YYYY, YYYY-MM, or YYYY-MM-DD form",
"end_time_before_start_time": "End time must be greater than or equal to start time", "end_time_before_start_time": "End time must be greater than or equal to start time",
"required": "${path} is a required field", "required": "${path} is a required field",
"unique": "${path} must be unique" "unique": "${path} must be unique"

View file

@ -139,8 +139,22 @@ export function yupDateString(intl: IntlShape) {
name: "date", name: "date",
test(value) { test(value) {
if (!value) return true; if (!value) return true;
if (!value.match(/^\d{4}-\d{2}-\d{2}$/)) return false; // Allow YYYY, YYYY-MM, or YYYY-MM-DD formats
if (Number.isNaN(Date.parse(value))) return false; if (!value.match(/^\d{4}(-\d{2}(-\d{2})?)?$/)) return false;
// Validate the date components
const parts = value.split("-");
const year = parseInt(parts[0], 10);
if (year < 1 || year > 9999) return false;
if (parts.length >= 2) {
const month = parseInt(parts[1], 10);
if (month < 1 || month > 12) return false;
}
if (parts.length === 3) {
const day = parseInt(parts[2], 10);
if (day < 1 || day > 31) return false;
// Full date - validate it parses correctly
if (Number.isNaN(Date.parse(value))) return false;
}
return true; return true;
}, },
message: intl.formatMessage({ id: "validation.date_invalid_form" }), message: intl.formatMessage({ id: "validation.date_invalid_form" }),