Skip to content

Commit 8f3e79d

Browse files
refactor(ValidationError): update
- add `template: string`, `callback: ResultCallback` parameters to the static `defineMessage()` method. - checks the `template: string` of the static `defineMessage()` method. - use prettier on the file. - update jsdoc.
1 parent 14412a9 commit 8f3e79d

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/lib/validation-error.class.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Object.
2-
import { is } from '@angular-package/type';
2+
import { is, ResultCallback } from '@angular-package/type';
33
// Interface.
44
import { ErrorMessage } from '../interface/error-message.interface';
55
/**
@@ -8,35 +8,48 @@ import { ErrorMessage } from '../interface/error-message.interface';
88
export class ValidationError extends Error {
99
/**
1010
* Template of the error message with the replaceable [problem] and [fix].
11+
* By default, it's set to `Problem: [problem] => Fix: [fix]`.
1112
*/
1213
static template = `Problem: [problem] => Fix: [fix]`;
1314

1415
/**
15-
* Possible solution to the described problem of a `string` type.
16+
* A possible solution to the described problem of a `string` type. By default, it's an empty `string`.
1617
*/
1718
public fix = '';
1819

1920
/**
20-
* Error name of a string type that is being thrown. By default, it's `ValidationError`.
21+
* Error name of a `string` type that is being thrown. By default, it's `ValidationError`.
2122
*/
2223
public name = ValidationError.name;
2324

2425
/**
25-
* The validation problem of a `string` type. By default, it's an empty string.
26+
* The validation problem of a `string` type. By default, it's an empty `string`.
2627
*/
2728
public problem = '';
2829

2930
/**
30-
* Defines the validation error message of a `string` type from the provided `message` of `ErrorMessage` interface.
31+
* Defines the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface.
3132
* @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against
32-
* the proper object.
33-
* @returns The return value is a message of a `string` type created from the provided `message` of `ErrorMessage` interface.
33+
* the proper `object`.
34+
* @param template A message template of a `string` type with replaceable `[problem]` and `[fix]` from the given `message`. The value is
35+
* checked against a `string`. By default, it's set to `Problem: [problem] => Fix: [fix]`.
36+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains
37+
* required `problem` and `fix` properties.
38+
* @returns The return value is a message of a `string` type created from the provided `message` of `ErrorMessage` interface or it's an
39+
* empty `string` if the provided message object isn't proper.
40+
* @angularpackage
3441
*/
35-
static defineMessage(message: ErrorMessage): string {
36-
if (is.objectKey(message, ['fix', 'problem'])) {
37-
return ValidationError.template
38-
.replace(`[fix]`, message.fix)
39-
.replace(`[problem]`, message.problem);
42+
static defineMessage(
43+
message: ErrorMessage,
44+
template: string = ValidationError.template,
45+
callback?: ResultCallback
46+
): string {
47+
if (is.objectKey(message, ['fix', 'problem'], callback)) {
48+
if (is.string(template)) {
49+
return template
50+
.replace(`[fix]`, message.fix)
51+
.replace(`[problem]`, message.problem);
52+
}
4053
}
4154
return '';
4255
}
@@ -45,9 +58,12 @@ export class ValidationError extends Error {
4558
* Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned
4659
* to the instance.
4760
* @param message The message of a `string` type or of an `ErrorMessage` interface that is used to throw with an `error`.
61+
* @angularpackage
4862
*/
4963
constructor(message: string | ErrorMessage) {
50-
super(is.string(message) ? message : ValidationError.defineMessage(message));
64+
super(
65+
is.string(message) ? message : ValidationError.defineMessage(message)
66+
);
5167
if (is.object(message)) {
5268
Object.assign(this, {
5369
problem: message.problem,

0 commit comments

Comments
 (0)