1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-26 22:41:17 +02:00

Desktop: Fixes #12419: Ensure min and max validation is enforced when setting is not yet present (#12553)

This commit is contained in:
mrjo118
2025-06-28 20:19:07 +01:00
committed by GitHub
parent 6a5c85d3d7
commit 813f077312
2 changed files with 26 additions and 8 deletions

View File

@@ -456,4 +456,18 @@ describe('models/Setting', () => {
await Setting.saveAll();
}
});
test('should enforce min and max values for when the setting is already in the cache and when it is not', async () => {
await Setting.reset();
Setting.setValue('revisionService.ttlDays', 0);
expect(Setting.value('revisionService.ttlDays')).toBe(1);
Setting.setValue('revisionService.ttlDays', 100000);
expect(Setting.value('revisionService.ttlDays')).toBe(99999);
await Setting.reset();
Setting.setValue('revisionService.ttlDays', 100000);
expect(Setting.value('revisionService.ttlDays')).toBe(99999);
Setting.setValue('revisionService.ttlDays', 0);
expect(Setting.value('revisionService.ttlDays')).toBe(1);
});
});

View File

@@ -657,11 +657,18 @@ class Setting extends BaseModel {
value = this.formatValue(key, value);
value = this.filterValue(key, value);
const md = this.settingMetadata(key);
const enforceLimits = (value: SettingValueType<T>) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code before rule was applied
if ('minimum' in md && value < md.minimum) value = md.minimum as any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code before rule was applied
if ('maximum' in md && value > md.maximum) value = md.maximum as any;
return value;
};
for (let i = 0; i < this.cache_.length; i++) {
const c = this.cache_[i];
if (c.key === key) {
const md = this.settingMetadata(key);
if (md.isEnum === true) {
if (!this.isAllowedEnumOption(key, value)) {
throw new Error(_('Invalid option value: "%s". Possible values are: %s.', value, this.enumOptionsDoc(key)));
@@ -675,12 +682,7 @@ class Setting extends BaseModel {
// Don't log this to prevent sensitive info (passwords, auth tokens...) to end up in logs
// logger.info('Setting: ' + key + ' = ' + c.value + ' => ' + value);
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code before rule was applied
if ('minimum' in md && value < md.minimum) value = md.minimum as any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code before rule was applied
if ('maximum' in md && value > md.maximum) value = md.maximum as any;
c.value = value;
c.value = enforceLimits(value);
this.dispatch({
type: 'SETTING_UPDATE_ONE',
@@ -694,6 +696,8 @@ class Setting extends BaseModel {
}
}
value = enforceLimits(value);
this.cache_.push({
key: key,
value: this.formatValue(key, value),