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

Function createValidator

src/utils/validation.ts:55–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

53}
54
55function createValidator() {
56 const errors: string[] = [];
57
58 function validateProperty<T extends Record<string, unknown>>(obj: T, key: keyof T, validator: (x: any) => boolean, fallback: T) {
59 if (!obj.hasOwnProperty(key) || validator(obj[key])) {
60 return;
61 }
62 errors.push(`Unexpected value for "${key as string}": ${JSON.stringify(obj[key])}`);
63 obj[key] = fallback[key];
64 }
65
66 function validateArray<T extends Record<string, unknown>, V>(obj: T, key: keyof T, validator: (x: V) => boolean) {
67 if (!obj.hasOwnProperty(key)) {
68 return;
69 }
70 const wrongValues = new Set();
71 const arr: any[] = obj[key] as any;
72 for (let i = 0; i < arr.length; i++) {
73 if (!validator(arr[i])) {
74 wrongValues.add(arr[i]);
75 arr.splice(i, 1);
76 i--;
77 }
78 }
79 if (wrongValues.size > 0) {
80 errors.push(`Array "${key as string}" has wrong values: ${Array.from(wrongValues).map((v) => JSON.stringify(v)).join('; ')}`);
81 }
82 }
83
84 return {validateProperty, validateArray, errors};
85}
86
87interface SettingValidationResult {
88 settings: Partial<UserSettings>;

Callers 2

validateSettingsFunction · 0.85
validateThemeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected