mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-03 08:18:42 +08:00
Implement a `datetime` type for site settings and object site settings
You can implement this type as so:
```
test_setting:
default: ""
client: true
type: "datetime"
```
- Add necessary validations
- Add tests
### Preview
<img width="670" height="264" alt="Screenshot 2025-12-29 at 11 59 13 AM"
src="https://github.com/user-attachments/assets/48d330e3-a38c-4d6a-9ea7-c4271b3e360e"
/>
21 lines
565 B
Ruby
21 lines
565 B
Ruby
# frozen_string_literal: true
|
|
|
|
class DatetimeSettingValidator
|
|
def initialize(opts = {})
|
|
@opts = opts
|
|
end
|
|
|
|
def valid_value?(val)
|
|
return true if val.blank?
|
|
# DateTime.iso8601 checks the format but does not enforce timezone presence
|
|
# so we need to do an additional check for the presence of timezone info.
|
|
DateTime.iso8601(val)
|
|
val.include?("T") && (val.end_with?("Z") || val.match?(/[+-]\d{2}:\d{2}$/))
|
|
rescue ArgumentError, TypeError
|
|
false
|
|
end
|
|
|
|
def error_message
|
|
I18n.t("site_settings.errors.invalid_datetime")
|
|
end
|
|
end
|