Skip to content

Commit

Permalink
validate dates out of range
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Jan 10, 2025
1 parent 9ffd680 commit de9bb00
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions packages/web-console/src/scenes/Editor/Metrics/date-time-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ const DatePickerItem = ({

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(name, e.target.value)
const dateValue = durationTokenToDate(e.target.value)
if (dateValue === "Invalid date") {
return
}
if (name === "dateFrom") {
onChange([dateValue, dateTo])
} else if (name === "dateTo") {
onChange([dateFrom, dateValue])
try {
const dateValue = durationTokenToDate(e.target.value)
if (dateValue === "Invalid date") {
return
}
if (name === "dateFrom") {
onChange([dateValue, dateTo])
} else if (name === "dateTo") {
onChange([dateFrom, dateValue])
}
} catch (e) {
console.error(e)
}
}

Expand Down Expand Up @@ -192,6 +196,7 @@ export const DateTimePicker = ({
"string.dateInFuture": "Please set a date in the past or use `now`",
"string.fromIsAfterTo": "From date must be before To date",
"string.sameValues": "From and To dates cannot be the same",
"any.custom": "One of the values is invalid",
}

const schema = Joi.object({
Expand All @@ -201,21 +206,25 @@ export const DateTimePicker = ({
const dateValue = durationTokenToDate(value)
const timeValue = new Date(dateValue).getTime()
const timeNow = new Date().getTime()
if (dateValue === "Invalid date") {
return helpers.error("string.invalidDate")
} else if (
timeValue >=
new Date(
durationTokenToDate(helpers.state.ancestors[0].dateTo),
).getTime()
) {
return helpers.error("string.fromIsAfterTo")
} else if (timeValue > timeNow) {
return helpers.error("string.dateInFuture")
} else if (timeValue === timeNow) {
return helpers.error("string.sameValues")
try {
if (dateValue === "Invalid date") {
return helpers.error("string.invalidDate")
} else if (
timeValue >=
new Date(
durationTokenToDate(helpers.state.ancestors[0].dateTo),
).getTime()
) {
return helpers.error("string.fromIsAfterTo")
} else if (timeValue > timeNow) {
return helpers.error("string.dateInFuture")
} else if (timeValue === timeNow) {
return helpers.error("string.sameValues")
}
return value
} catch (e) {
return helpers.error("any.custom")
}
return value
})
.messages(errorMessages),
dateTo: Joi.any()
Expand Down

0 comments on commit de9bb00

Please sign in to comment.