Skip to content

Commit 0bbd886

Browse files
docs(README.md): update.
1 parent e6fb177 commit 0bbd886

1 file changed

Lines changed: 135 additions & 11 deletions

File tree

README.md

Lines changed: 135 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Manages an [`Error`][js-error].
4343
* [Api](#api)
4444
* [`ValidationError`](#validationerror)
4545
* [Interface](#interface)
46+
* [Type](#type)
4647
* [Experimental](#experimental)
4748
* [Changelog](#changelog)
4849
* [Git](#git)
@@ -168,19 +169,21 @@ Manages an [`Error`][js-error] of validation.
168169

169170
**Instance methods:**
170171

171-
| ValidationError.prototype. | Description |
172-
| :------------------------------------------ | :---------- |
173-
| [`setFix()`][error-method-setfix] | Sets the fix a possible solution to the described [`problem`][error-property-problem]. |
174-
| [`setMessage()`][error-method-setmessage] | Sets the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. |
175-
| [`setProblem()`][error-method-setproblem] | Sets description problem of a [`ValidationError`](#validationerror). |
176-
| [`setTemplate()`][error-method-settemplate] | Sets the template of validation error message. |
177-
| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with actual settings. |
172+
| ValidationError.prototype. | Description |
173+
| :---------------------------------------------- | :---------- |
174+
| [`setFix()`][error-method-setfix] | Sets the fix a possible solution to the described [`problem`][error-property-problem]. |
175+
| [`setMessage()`][error-method-setmessage] | Sets the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. |
176+
| [`setProblem()`][error-method-setproblem] | Sets description problem of a [`ValidationError`](#validationerror). |
177+
| [`setTemplate()`][error-method-settemplate] | Sets the template of validation error message. |
178+
| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with actual settings. |
179+
| [`updateMessage()`][error-method-updatemessage] | Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. |
178180

179181
[error-method-setfix]: #validationerrorprototypesetfix
180182
[error-method-setmessage]: #validationerrorprototypesetmessage
181183
[error-method-setproblem]: #validationerrorprototypesetproblem
182184
[error-method-settemplate]: #validationerrorprototypesettemplate
183185
[error-method-throw]: #validationerrorprototypethrow
186+
[error-method-updatemessage]: #validationerrorprototypeupdatemessage
184187

185188
<br>
186189

@@ -301,7 +304,6 @@ public static defineMessage(
301304
| Name: type | Description |
302305
| :-------------------------- | :---------- |
303306
| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of a [`string`][js-string] type. The value is checked against the proper [`object`][js-object] |
304-
| `template: string` | A message template of a [`string`][js-string] type with replaceable `[problem]` and `[fix]` from the given `message`. The value is checked against a [`string`][js-string]. By default, it's set to `Problem: [problem] => Fix: [fix]` |
305307
| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided message contains required `problem` and `fix` properties |
306308

307309
**Returns:**
@@ -444,7 +446,57 @@ const validationError = new ValidationError({ fix, problem });
444446

445447
```typescript
446448
// Example usage with callback.
449+
import { ValidationError } from '@angular-package/error';
450+
451+
// Define a fix.
452+
const fix = 'There is no solution to the described problem.';
453+
454+
// Define a problem.
455+
const problem = 'The problem has no solution.';
447456

457+
// Define a template.
458+
const template = 'PROBLEM: [problem] FIX: [fix]';
459+
460+
// Initialize an instance.
461+
const validationError = new ValidationError(
462+
{ fix, problem, template },
463+
(callback) => {
464+
callback
465+
/*
466+
Console: false,
467+
{
468+
"fix": "There is no solution to the described problem.",
469+
"problem": "The problem has no solution.",
470+
"template": "PROBLEM: [problem] FIX: [fix]"
471+
}
472+
473+
Console: true,
474+
{
475+
"fix": "There is no solution to the described problem.",
476+
"problem": "The problem has no solution.",
477+
"template": "PROBLEM: [problem] FIX: [fix]"
478+
}
479+
*/
480+
.setResultCallback('setFix', (result, payload) =>
481+
console.log(`setFix`, result, payload);
482+
)
483+
484+
// Console: 'setFix true There is no solution to the described problem.'
485+
.setResultCallback('setMessage', (result, payload) =>
486+
console.log(`setMessage`, result, payload);
487+
)
488+
489+
// Console: 'setProblem true The problem has no solution.'
490+
.setResultCallback('setProblem', (result, payload) =>
491+
console.log(`setProblem`, result, payload);
492+
)
493+
494+
// Console: 'setTemplate true PROBLEM: [problem] FIX: [fix]'
495+
.setResultCallback('setTemplate', (result, payload) =>
496+
console.log(`setTemplate`, result, payload);
497+
);
498+
}
499+
);
448500
```
449501

450502
<br>
@@ -479,7 +531,7 @@ public setFix(
479531
| Name: type | Description |
480532
| :------------------------------------------- | :---------- |
481533
| `fix: string` | A possible solution to the described problem guarded by a [`string`][js-string] type. |
482-
| `callback?: ResultCallback<CallbackPayload>` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`fix`][error-property-fix] is a [`string`][js-string]. |
534+
| `callback?: ResultCallback<CallbackPayload>` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`fix`][error-property-fix] is a [`string`][js-string]. It can be initially set by callback |
483535

484536
**Returns:**
485537

@@ -796,6 +848,68 @@ validationError.throw();
796848

797849
<br>
798850

851+
#### `ValidationError.prototype.updateMessage()`
852+
853+
![new]
854+
855+
Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template].
856+
857+
```typescript
858+
public updateMessage(): void {
859+
this.message = ValidationError.defineMessage({
860+
fix: this.#fix,
861+
problem: this.#problem,
862+
template: this.#tpl,
863+
});
864+
}
865+
```
866+
867+
**Returns:**
868+
869+
The **return value** is an instance of an [`ValidationError`](#validationerror).
870+
871+
**Usage:**
872+
873+
```typescript
874+
// Example usage.
875+
import { ValidationError } from '@angular-package/error';
876+
877+
// Define a fix.
878+
const fix = 'There is no solution to the described problem.';
879+
880+
// Define a problem.
881+
const problem = 'The problem has no solution.';
882+
883+
// Define a template.
884+
const template = 'PROBLEM: [problem] FIX: [fix]';
885+
886+
// Initialize an instance.
887+
const validationError = new ValidationError();
888+
889+
// Sets defined above fix, problem, and template.
890+
validationError.setProblem(problem).setFix(fix).setTemplate(template);
891+
892+
// Returns empty string.
893+
validationError.message;
894+
895+
// Update the message with actual settings.
896+
validationError.updateMessage();
897+
898+
/*
899+
Returns
900+
PROBLEM: The problem has no solution. FIX: There is no solution to the described problem.
901+
*/
902+
validationError.message;
903+
904+
// Throw.
905+
validationError.throw();
906+
907+
// or throw
908+
throw validationError;
909+
```
910+
911+
<br>
912+
799913
### Complete usage of `ValidationError`
800914

801915
```typescript
@@ -806,8 +920,6 @@ validationError.throw();
806920

807921
## Interface
808922

809-
### Common
810-
811923
#### `ErrorMessage`
812924

813925
The shape of an [`object`][js-object] for an [`error`][js-error] message that contains a possible solution to the described problem.
@@ -833,6 +945,18 @@ An optional message template of a [`string`][js-string] type.
833945

834946
<br>
835947

948+
## Interface
949+
950+
#### `VEAllowedCallback`
951+
952+
Allowed callback function names available for the [`ValidationError`](#validationerror).
953+
954+
```typescript
955+
type VEAllowedCallback = 'setFix' | 'setMessage' | 'setProblem' | 'setTemplate';
956+
```
957+
958+
<br>
959+
836960
## Experimental
837961

838962
![experimental]

0 commit comments

Comments
 (0)