MCPcopy Index your code
hub / github.com/darkreader/darkreader / validateSettings

Function validateSettings

src/utils/validation.ts:92–189  ·  view source on GitHub ↗
(settings: Partial<UserSettings>)

Source from the content-addressed store, hash-verified

90}
91
92export function validateSettings(settings: Partial<UserSettings>): SettingValidationResult {
93 if (!isPlainObject(settings)) {
94 return {errors: ['Settings are not a plain object'], settings: DEFAULT_SETTINGS};
95 }
96
97 const {validateProperty, validateArray, errors} = createValidator();
98 const isValidPresetTheme = (theme: Theme) => {
99 if (!isPlainObject(theme)) {
100 return false;
101 }
102 const {errors: themeErrors} = validateTheme(theme);
103 return themeErrors.length === 0;
104 };
105
106 validateProperty(settings, 'schemeVersion', isNumber, DEFAULT_SETTINGS);
107
108 validateProperty(settings, 'enabled', isBoolean, DEFAULT_SETTINGS);
109 validateProperty(settings, 'fetchNews', isBoolean, DEFAULT_SETTINGS);
110
111 validateProperty(settings, 'theme', isPlainObject, DEFAULT_SETTINGS);
112 const {errors: themeErrors} = validateTheme(settings.theme);
113 errors.push(...themeErrors);
114
115 validateProperty(settings, 'presets', isArray, DEFAULT_SETTINGS);
116 validateArray(settings, 'presets', (preset: ThemePreset) => {
117 const presetValidator = createValidator();
118 if (!(isPlainObject(preset) && hasRequiredProperties(preset, ['id', 'name', 'urls', 'theme']))) {
119 return false;
120 }
121 presetValidator.validateProperty(preset, 'id', isNonEmptyString, preset);
122 presetValidator.validateProperty(preset, 'name', isNonEmptyString, preset);
123 presetValidator.validateProperty(preset, 'urls', isNonEmptyArrayOfNonEmptyStrings, preset);
124 presetValidator.validateProperty(preset, 'theme', isValidPresetTheme, preset);
125 return presetValidator.errors.length === 0;
126 });
127
128 validateProperty(settings, 'customThemes', isArray, DEFAULT_SETTINGS);
129 validateArray(settings, 'customThemes', (custom: CustomSiteConfig) => {
130 if (!(isPlainObject(custom) && hasRequiredProperties(custom, ['url', 'theme']))) {
131 return false;
132 }
133 const presetValidator = createValidator();
134 presetValidator.validateProperty(custom, 'url', isNonEmptyArrayOfNonEmptyStrings, custom);
135 presetValidator.validateProperty(custom, 'theme', isValidPresetTheme, custom);
136 return presetValidator.errors.length === 0;
137 });
138
139 validateProperty(settings, 'enabledFor', isArray, DEFAULT_SETTINGS);
140 validateArray(settings, 'enabledFor', isNonEmptyString);
141 validateProperty(settings, 'disabledFor', isArray, DEFAULT_SETTINGS);
142 validateArray(settings, 'disabledFor', isNonEmptyString);
143
144 validateProperty(settings, 'enabledByDefault', isBoolean, DEFAULT_SETTINGS);
145 validateProperty(settings, 'changeBrowserTheme', isBoolean, DEFAULT_SETTINGS);
146 validateProperty(settings, 'syncSettings', isBoolean, DEFAULT_SETTINGS);
147 validateProperty(settings, 'syncSitesFixes', isBoolean, DEFAULT_SETTINGS);
148 validateProperty(settings, 'automation', (automation: Automation) => {
149 if (!isPlainObject(automation)) {

Callers 3

importSettingsFunction · 0.90

Calls 7

isPlainObjectFunction · 0.85
createValidatorFunction · 0.85
validatePropertyFunction · 0.85
validateThemeFunction · 0.85
validateArrayFunction · 0.85
hasRequiredPropertiesFunction · 0.85
isOneOfFunction · 0.85

Tested by

no test coverage detected