-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ValidationErrors): add feature to store errors of the `Validatio…
…nError` type and throw on demand.
- Loading branch information
1 parent
ff2feeb
commit 43a0162
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { CommonErrors } from './common-errors.class'; | ||
import { ValidationError } from './validation-error.class'; | ||
/** | ||
* The `ValidationErrors` is an extension of the `CommonErrors` object that represents multiple identification numbers under which the | ||
* errors of the `ValidationError` type are prepared to throw. | ||
*/ | ||
export class ValidationErrors<Id extends string> extends CommonErrors<Id> { | ||
//#region constructor. | ||
/** | ||
* Creates the `ValidationErrors` instance of unique identification numbers under which the `ValidationError` objects are stored. | ||
* @param id A rest parameter of generic type variable `Id` indicates unique identification numbers under which the `ValidationError` | ||
* objects are stored. | ||
* @angularpackage | ||
*/ | ||
constructor(...id: Id[]) { | ||
super(...id); | ||
} | ||
//#endregion constructor. | ||
|
||
//#region instance public methods. | ||
/** | ||
* Sets the `ValidationError` object with the message built from the given required `problem`, `fix`, `id` on the given or stored | ||
* `template` under the given `id`. | ||
* @param problem Description of the problem of a `string` type. | ||
* @param fix A solution to the given `problem` of a string type. | ||
* @param id The unique identification to the given `problem` of generic type variable `ErrorId`. | ||
* @param template A template of error message with the replaceable `{problem}`, `{fix}`, `{id}` tags. By default, the value is equal | ||
* to the static property `ValidationErrors.template`. | ||
* @returns The return value is an instance of `Errors`. | ||
* @angularpackage | ||
*/ | ||
public set<ErrorId extends Id>( | ||
problem: string, | ||
fix: string, | ||
id: ErrorId, | ||
template = ValidationErrors.template | ||
): this { | ||
this.isAllowedId(id) && | ||
this.errors.set(id, new ValidationError(problem, fix, id, template)); | ||
return this; | ||
} | ||
|
||
/** | ||
* The method returns the JSON object of set errors, where the key is a unique identification. | ||
* @returns The return value is an `object` of set errors. | ||
* @angularpackage | ||
*/ | ||
public toObject(): { [Key in Id]: ValidationError<Id> } { | ||
return Object.fromEntries(this.errors.entries()) as any; | ||
} | ||
|
||
//#endregion instance public methods. | ||
} |