[MM-45991] Check and return JSON errors (#20735)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
fdee1df6fe
Коммит
1738bd6e92
@@ -91,23 +91,21 @@ func (o *Preference) IsValid() *AppError {
|
||||
if o.Category == PreferenceCategoryTheme {
|
||||
var unused map[string]string
|
||||
if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&unused); err != nil {
|
||||
return NewAppError("Preference.IsValid", "model.preference.is_valid.theme.app_error", nil, "value="+o.Value, http.StatusBadRequest)
|
||||
return NewAppError("Preference.IsValid", "model.preference.is_valid.theme.app_error", nil, "value="+o.Value, http.StatusBadRequest).Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var preUpdateColorPattern = regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`)
|
||||
|
||||
func (o *Preference) PreUpdate() {
|
||||
if o.Category == PreferenceCategoryTheme {
|
||||
// decode the value of theme (a map of strings to string) and eliminate any invalid values
|
||||
var props map[string]string
|
||||
if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&props); err != nil {
|
||||
// just continue, the invalid preference value should get caught by IsValid before saving
|
||||
return
|
||||
}
|
||||
|
||||
colorPattern := regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`)
|
||||
// just continue, the invalid preference value should get caught by IsValid before saving
|
||||
json.NewDecoder(strings.NewReader(o.Value)).Decode(&props)
|
||||
|
||||
// blank out any invalid theme values
|
||||
for name, value := range props {
|
||||
@@ -115,7 +113,7 @@ func (o *Preference) PreUpdate() {
|
||||
continue
|
||||
}
|
||||
|
||||
if !colorPattern.MatchString(value) {
|
||||
if !preUpdateColorPattern.MatchString(value) {
|
||||
props[name] = "#ffffff"
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user