Skip to content

A platform for community discussion. Free, open, simple.

FIX: Date params now use native Date() fallback and diverge from server validation (#39932)

discourse-patch-triage[bot]bedf6057eb5f
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/param-input-form.gjs b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/param-input-form.gjs
index 1effd28b2d9..be1aaa25206 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/param-input-form.gjs
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/param-input-form.gjs
@@ -49,6 +49,8 @@ export const ERRORS = {
INVALID_TIME: (time) => i18n("explorer.form.errors.invalid_time", { time }),
};
+const DATE_FORMATS = ["YYYY-MM-DD", "D MMM YYYY", "D MMMM YYYY"];
+
function digitalizeCategoryId(value) {
value = String(value || "");
const isPositiveInt = /^\d+$/.test(value);
@@ -248,17 +250,17 @@ export default class ParamInputForm extends Component {
}
return value;
case "date":
- const m = value && moment(value, "YYYY-MM-DD", true);
- if (m?.isValid()) {
- return m.format("YYYY-MM-DD");
- } else {
- return this.formatMoment(
- info,
- value,
- "YYYY-MM-DD",
- ERRORS.INVALID_DATE
- );
+ if (!value) {
+ return null;
}
+
+ const date = moment(value, DATE_FORMATS, true);
+ if (date.isValid()) {
+ return date.format("YYYY-MM-DD");
+ }
+
+ this.addError(info.identifier, ERRORS.INVALID_DATE(String(value)));
+ return null;
case "time":
return this.formatMoment(
info,
diff --git a/plugins/discourse-data-explorer/test/javascripts/components/param-input-test.gjs b/plugins/discourse-data-explorer/test/javascripts/components/param-input-test.gjs
index 7bb5dd34514..e69507ce253 100644
--- a/plugins/discourse-data-explorer/test/javascripts/components/param-input-test.gjs
+++ b/plugins/discourse-data-explorer/test/javascripts/components/param-input-test.gjs
@@ -347,6 +347,38 @@ module("Component | param-input", function (hooks) {
.hasError(`${ERRORS.NO_SUCH_GROUP}: invalid_group_name`);
});
+ test("date initial values reject native-only formats", async function (assert) {
+ this.setProperties({
+ param_info: [
+ {
+ identifier: "date",
+ type: "date",
+ default: null,
+ nullable: false,
+ },
+ ],
+ initialValues: {
+ date: "01/19/2038",
+ },
+ onRegisterApi: ({ submit }) => {
+ this.submit = submit;
+ },
+ });
+
+ await render(
+ <template>
+ <ParamInputForm
+ @initialValues={{this.initialValues}}
+ @paramInfo={{this.param_info}}
+ @onRegisterApi={{this.onRegisterApi}}
+ />
+ </template>
+ );
+
+ assert.form().field("date").hasError(ERRORS.INVALID_DATE("01/19/2038"));
+ await assert.rejects(this.submit());
+ });
+
test("date, time, datetime with initial value in other formats", async function (assert) {
this.setProperties({
param_info: [