Skip to content

Commit

Permalink
change ValidationResult
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Aug 25, 2023
1 parent 13e7e6d commit 3ede5ba
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 47 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
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 3ede5ba

Please sign in to comment.