Skip to content

Commit

Permalink
change ValidationResult type. AG-25272
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 3ede5ba
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Fri Aug 25 19:13:49 2023 +0300

    change ValidationResult

commit 13e7e6d
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Fri Aug 25 19:03:05 2023 +0300

    update todos
  • Loading branch information
slavaleleka committed Aug 28, 2023
1 parent cd61aa2 commit 8e32d36
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 50 deletions.
5 changes: 5 additions & 0 deletions packages/agtree/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe

- Validation of modifier values due to `value_format`

## Changed

- `ModifierValidator.validate()` result type `ValidationResult``valid` property instead of `ok`


## 1.1.2 - 2023-08-14

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/agtree/src/compatibility-tables/modifiers/csp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ adg_os_any:
assignable: true
negatable: false
value_optional: true
# TODO: add custom validator instead of regular expression
# TODO: add custom validator instead of regular expression. AG-25274
value_format: |-
(?xi)
^(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ adg_os_any:
negatable: false
exception_only: true
value_optional: true
# TODO: add custom validator instead of regular expression. AG-25273
value_format: |-
(?x)
^(?!\|)\b(?:(
Expand Down
2 changes: 0 additions & 2 deletions packages/agtree/src/compatibility-tables/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export const enum SpecificKey {
Negatable = 'negatable',
BlockOnly = 'block_only',
ExceptionOnly = 'exception_only',
// TODO: consider removing this field
// and handle whether the value is optional by `value_format`. AG-24028
ValueOptional = 'value_optional',
ValueFormat = 'value_format',
// TODO: following fields should be handled later
Expand Down
15 changes: 7 additions & 8 deletions packages/agtree/src/validator/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { VALIDATION_ERROR_PREFIX } from './constants';

/**
* Result of modifier validation:
* - `{ ok: true }` for valid and _fully supported_ modifier;
* - `{ ok: true, warn: <deprecation notice> }` for valid
* - `{ valid: true }` for valid and _fully supported_ modifier;
* - `{ valid: true, warn: <deprecation notice> }` for valid
* and _still supported but deprecated_ modifier;
* - otherwise `{ ok: true, error: <invalidity reason> }`
* - otherwise `{ valid: true, error: <invalidity reason> }`
*/
export type ValidationResult = {
// TODO: change `ok` to `valid`. note that aglint should be updated as well
ok: boolean,
valid: boolean,
error?: string,
warn?: string,
};
Expand All @@ -32,11 +31,11 @@ export const isValidNoopModifier = (value: string): boolean => {
*
* @param error Error message.
*
* @returns Validation result `{ ok: false, error }`.
* @returns Validation result `{ valid: false, error }`.
*/
export const getInvalidValidationResult = (error: string): ValidationResult => {
return {
ok: false,
valid: false,
error,
};
};
Expand All @@ -47,7 +46,7 @@ export const getInvalidValidationResult = (error: string): ValidationResult => {
*
* @param modifierName Modifier name.
*
* @returns Validation result `{ ok: false, error }`.
* @returns Validation result `{ valid: false, error }`.
*/
export const getValueRequiredValidationResult = (modifierName: string): ValidationResult => {
return getInvalidValidationResult(`${VALIDATION_ERROR_PREFIX.VALUE_REQUIRED}: '${modifierName}'`);
Expand Down
10 changes: 5 additions & 5 deletions packages/agtree/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const validateForSpecificSyntax = (
// prepare the message which is multiline in the yaml file
const warn = specificBlockerData[SpecificKey.DeprecationMessage].replace(NEWLINE, SPACE);
return {
ok: true,
valid: true,
warn,
};
}
Expand All @@ -95,15 +95,15 @@ const validateForSpecificSyntax = (
*/
if (specificBlockerData[SpecificKey.ValueOptional]) {
// no need to check the value if it is optional
return { ok: true };
return { valid: true };
}

if (!modifier.value) {
return getValueRequiredValidationResult(modifierName);
}

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

return { ok: true };
return { valid: true };
};

/**
Expand Down Expand Up @@ -215,7 +215,7 @@ class ModifierValidator {
}
// for 'Common' syntax we cannot check something more
if (syntax === AdblockSyntax.Common) {
return { ok: true };
return { valid: true };
}
return validateForSpecificSyntax(this.modifiersData, syntax, modifier, isException);
};
Expand Down
10 changes: 5 additions & 5 deletions packages/agtree/src/validator/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const customNoNegatedListItemsValidator = (modifierName: string, listItems: List
);
}

return { ok: true };
return { valid: true };
};

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ const customConsistentExceptionsValidator = (modifierName: string, listItems: Li
);
}

return { ok: true };
return { valid: true };
};

/**
Expand Down Expand Up @@ -217,7 +217,7 @@ const validateListItemsModifier = (
} catch (e: unknown) {
if (e instanceof AdblockSyntaxError) {
return {
ok: false,
valid: false,
error: e.message,
};
}
Expand Down Expand Up @@ -248,7 +248,7 @@ const validateListItemsModifier = (
return customListValidator(modifierName, theList.children);
}

return { ok: true };
return { valid: true };
};

/**
Expand Down Expand Up @@ -371,5 +371,5 @@ export const validateValue = (modifier: Modifier, valueFormat: string): Validati
return defaultInvalidValueResult;
}

return { ok: true };
return { valid: true };
};
Loading

0 comments on commit 8e32d36

Please sign in to comment.