Skip to content

Commit 8e32d36

Browse files
committed
change ValidationResult type. AG-25272
Squashed commit of the following: commit 3ede5ba Author: Slava Leleka <[email protected]> Date: Fri Aug 25 19:13:49 2023 +0300 change ValidationResult commit 13e7e6d Author: Slava Leleka <[email protected]> Date: Fri Aug 25 19:03:05 2023 +0300 update todos
1 parent cd61aa2 commit 8e32d36

File tree

8 files changed

+53
-50
lines changed

8 files changed

+53
-50
lines changed

packages/agtree/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe
1010

1111
- Validation of modifier values due to `value_format`
1212

13+
## Changed
14+
15+
- `ModifierValidator.validate()` result type `ValidationResult``valid` property instead of `ok`
16+
17+
1318
## 1.1.2 - 2023-08-14
1419

1520
### Fixed

packages/agtree/src/compatibility-tables/modifiers/csp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ adg_os_any:
1414
assignable: true
1515
negatable: false
1616
value_optional: true
17-
# TODO: add custom validator instead of regular expression
17+
# TODO: add custom validator instead of regular expression. AG-25274
1818
value_format: |-
1919
(?xi)
2020
^(

packages/agtree/src/compatibility-tables/modifiers/stealth.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ adg_os_any:
66
negatable: false
77
exception_only: true
88
value_optional: true
9+
# TODO: add custom validator instead of regular expression. AG-25273
910
value_format: |-
1011
(?x)
1112
^(?!\|)\b(?:(

packages/agtree/src/compatibility-tables/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export const enum SpecificKey {
2222
Negatable = 'negatable',
2323
BlockOnly = 'block_only',
2424
ExceptionOnly = 'exception_only',
25-
// TODO: consider removing this field
26-
// and handle whether the value is optional by `value_format`. AG-24028
2725
ValueOptional = 'value_optional',
2826
ValueFormat = 'value_format',
2927
// TODO: following fields should be handled later

packages/agtree/src/validator/helpers.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import { VALIDATION_ERROR_PREFIX } from './constants';
44

55
/**
66
* Result of modifier validation:
7-
* - `{ ok: true }` for valid and _fully supported_ modifier;
8-
* - `{ ok: true, warn: <deprecation notice> }` for valid
7+
* - `{ valid: true }` for valid and _fully supported_ modifier;
8+
* - `{ valid: true, warn: <deprecation notice> }` for valid
99
* and _still supported but deprecated_ modifier;
10-
* - otherwise `{ ok: true, error: <invalidity reason> }`
10+
* - otherwise `{ valid: true, error: <invalidity reason> }`
1111
*/
1212
export type ValidationResult = {
13-
// TODO: change `ok` to `valid`. note that aglint should be updated as well
14-
ok: boolean,
13+
valid: boolean,
1514
error?: string,
1615
warn?: string,
1716
};
@@ -32,11 +31,11 @@ export const isValidNoopModifier = (value: string): boolean => {
3231
*
3332
* @param error Error message.
3433
*
35-
* @returns Validation result `{ ok: false, error }`.
34+
* @returns Validation result `{ valid: false, error }`.
3635
*/
3736
export const getInvalidValidationResult = (error: string): ValidationResult => {
3837
return {
39-
ok: false,
38+
valid: false,
4039
error,
4140
};
4241
};
@@ -47,7 +46,7 @@ export const getInvalidValidationResult = (error: string): ValidationResult => {
4746
*
4847
* @param modifierName Modifier name.
4948
*
50-
* @returns Validation result `{ ok: false, error }`.
49+
* @returns Validation result `{ valid: false, error }`.
5150
*/
5251
export const getValueRequiredValidationResult = (modifierName: string): ValidationResult => {
5352
return getInvalidValidationResult(`${VALIDATION_ERROR_PREFIX.VALUE_REQUIRED}: '${modifierName}'`);

packages/agtree/src/validator/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const validateForSpecificSyntax = (
6969
// prepare the message which is multiline in the yaml file
7070
const warn = specificBlockerData[SpecificKey.DeprecationMessage].replace(NEWLINE, SPACE);
7171
return {
72-
ok: true,
72+
valid: true,
7373
warn,
7474
};
7575
}
@@ -95,15 +95,15 @@ const validateForSpecificSyntax = (
9595
*/
9696
if (specificBlockerData[SpecificKey.ValueOptional]) {
9797
// no need to check the value if it is optional
98-
return { ok: true };
98+
return { valid: true };
9999
}
100100

101101
if (!modifier.value) {
102102
return getValueRequiredValidationResult(modifierName);
103103
}
104104

105105
/**
106-
* TODO: consider to return `{ ok: true, warn: 'Modifier value may be specified' }` (???)
106+
* TODO: consider to return `{ valid: true, warn: 'Modifier value may be specified' }` (???)
107107
* for $stealth modifier without a value
108108
* but only after the extension will support value for $stealth:
109109
* https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2107
@@ -121,7 +121,7 @@ const validateForSpecificSyntax = (
121121
return getInvalidValidationResult(`${VALIDATION_ERROR_PREFIX.VALUE_FORBIDDEN}: '${modifierName}'`);
122122
}
123123

124-
return { ok: true };
124+
return { valid: true };
125125
};
126126

127127
/**
@@ -215,7 +215,7 @@ class ModifierValidator {
215215
}
216216
// for 'Common' syntax we cannot check something more
217217
if (syntax === AdblockSyntax.Common) {
218-
return { ok: true };
218+
return { valid: true };
219219
}
220220
return validateForSpecificSyntax(this.modifiersData, syntax, modifier, isException);
221221
};

packages/agtree/src/validator/value.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const customNoNegatedListItemsValidator = (modifierName: string, listItems: List
144144
);
145145
}
146146

147-
return { ok: true };
147+
return { valid: true };
148148
};
149149

150150
/**
@@ -180,7 +180,7 @@ const customConsistentExceptionsValidator = (modifierName: string, listItems: Li
180180
);
181181
}
182182

183-
return { ok: true };
183+
return { valid: true };
184184
};
185185

186186
/**
@@ -217,7 +217,7 @@ const validateListItemsModifier = (
217217
} catch (e: unknown) {
218218
if (e instanceof AdblockSyntaxError) {
219219
return {
220-
ok: false,
220+
valid: false,
221221
error: e.message,
222222
};
223223
}
@@ -248,7 +248,7 @@ const validateListItemsModifier = (
248248
return customListValidator(modifierName, theList.children);
249249
}
250250

251-
return { ok: true };
251+
return { valid: true };
252252
};
253253

254254
/**
@@ -371,5 +371,5 @@ export const validateValue = (modifier: Modifier, valueFormat: string): Validati
371371
return defaultInvalidValueResult;
372372
}
373373

374-
return { ok: true };
374+
return { valid: true };
375375
};

0 commit comments

Comments
 (0)