Skip to content

Commit 069d111

Browse files
refactor(ValidationError): general update.
Add: - static private property `#template` of a `string` type. - private instance `#callback` property of `Callback` instance. - private instance `#fix`, `#problem`, `#tpl` property. - pubic methods `setFix()`, `setMessage()`, `setProblem()`, `setTemplate()`, `throw()`, `updateMessage() of an instance. - static private methods `#guardMessage()`, `#guardTemplate`. Update: - static public `template` property to use static private `#template` property that is guarded by the private static `#guardTemplate()` method. - instance `fix` property to use private `#fix`. - instance `problem` property to use private `#problem`. - public static `defineMessage()` method uses private static `#guardMessage()` to guards the provided `message`. - constructor uses public `setMessage()` method and got new `callback` parameter to handle private instance of `Callback`.
1 parent 4040750 commit 069d111

1 file changed

Lines changed: 271 additions & 31 deletions

File tree

src/lib/validation-error.class.ts

Lines changed: 271 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,314 @@
1-
// Object.
2-
import { is, ResultCallback } from '@angular-package/type';
1+
// @angular-package/type.
2+
import { is, guard } from '@angular-package/type';
3+
4+
// @angular-package/callback.
5+
import {
6+
Callback,
7+
CallbackPayload,
8+
ResultCallback,
9+
} from '@angular-package/callback';
10+
311
// Interface.
412
import { ErrorMessage } from '../interface/error-message.interface';
13+
14+
// Type.
15+
import { VEAllowedCallback } from '../type/allowed-callback.type';
16+
517
/**
6-
* Manages an `Error` of the validation.
18+
* Manages an `Error` of validation.
719
*/
820
export class ValidationError extends Error {
21+
/* #region static private properties */
22+
/**
23+
* A static, privately stored template of the error message.
24+
*/
25+
static #template = `Problem: [problem] => Fix: [fix]`;
26+
/* #endregion static private properties */
27+
28+
//#region instance private properties.
29+
/**
30+
* An instance of `Callback` with specified allowed names of callback functions for the `ValidationError`.
31+
*/
32+
#callback = new Callback('setFix', 'setMessage', 'setProblem', 'setTemplate');
33+
34+
/**
35+
* A privately stored possible solution to the described problem of a `string` type.
36+
* By default, it's an empty `string`.
37+
*/
38+
#fix = '';
39+
40+
/**
41+
* A privately stored validation problem of a `string` type.
42+
* By default, it's an empty `string`.
43+
*/
44+
#problem = '';
45+
946
/**
10-
* Template of the error message with the replaceable [problem] and [fix].
47+
* A string-type privately stored template of the error message that contains replaceable `[fix]` and `[problem]` words.
48+
*/
49+
#tpl = ValidationError.template;
50+
//#endregion instance private properties.
51+
52+
/**
53+
* A template of the error message guarded by a `string` type with the replaceable `[problem]` and `[fix]` words.
54+
* The value is being checked against the existence of `[problem]` and `[fix]` words.
1155
* By default, it's set to `Problem: [problem] => Fix: [fix]`.
1256
*/
13-
static template = `Problem: [problem] => Fix: [fix]`;
57+
static get template(): string {
58+
return ValidationError.#template;
59+
}
60+
static set template(value: string) {
61+
ValidationError.#template = ValidationError.#guardTemplate(value)
62+
? value
63+
: ValidationError.#template;
64+
}
1465

66+
//#region instance public properties.
1567
/**
16-
* A possible solution to the described problem of a `string` type. By default, it's an empty `string`.
68+
* A possible solution to the described `problem` of validation that is guarded by a `string` type.
69+
* By default, it's an empty `string`.
1770
*/
18-
public fix = '';
71+
public get fix(): string {
72+
return this.#fix;
73+
}
74+
public set fix(value: string) {
75+
this.#fix = guard.string(value) ? value : this.#fix;
76+
}
77+
78+
/**
79+
* A validation error message guarded by a `string` type that can be built with the `problem` and `fix` of `ValidationError` by the
80+
* `throw()` and `setMessage()` method.
81+
*/
82+
public set message(value: string) {
83+
super.message = guard.string(value) ? value : super.message;
84+
}
85+
public get message(): string {
86+
return super.message;
87+
}
1988

2089
/**
21-
* Error name of a `string` type that is being thrown. By default, it's `ValidationError`.
90+
* Error name of a `string` type that is being thrown.
91+
* By default, it's `ValidationError`.
2292
*/
2393
public name = ValidationError.name;
2494

2595
/**
26-
* The validation problem of a `string` type. By default, it's an empty `string`.
96+
* Description of a validation problem guarded by a `string` type.
97+
* By default, it's an empty `string`.
2798
*/
28-
public problem = '';
99+
public get problem(): string {
100+
return this.#problem;
101+
}
102+
public set problem(value: string) {
103+
this.#problem = guard.string(value) ? value : this.#problem;
104+
}
105+
//#endregion instance public properties.
29106

107+
//#region static public methods
30108
/**
31109
* Defines the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface.
32110
* @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against
33111
* 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]`.
36112
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains
37113
* required `problem` and `fix` properties.
38114
* @returns The return value is a message of a `string` type created from the provided `message` of `ErrorMessage` interface or it's an
39115
* empty `string` if the provided message object isn't proper.
40116
* @angularpackage
41117
*/
42-
static defineMessage(
118+
public static defineMessage(
43119
message: ErrorMessage,
44-
template: string = ValidationError.template,
45-
callback?: ResultCallback
120+
callback?: ResultCallback<CallbackPayload & ErrorMessage>
46121
): string {
47-
if (is.objectKey(message, ['fix', 'problem'], callback)) {
48-
if (is.string(template)) {
49-
return template
122+
return ValidationError.#guardMessage(message, callback)
123+
? (message.template || ValidationError.template)
50124
.replace(`[fix]`, message.fix)
51-
.replace(`[problem]`, message.problem);
52-
}
53-
}
54-
return '';
125+
.replace(`[problem]`, message.problem)
126+
: '';
55127
}
128+
//#endregion static public methods
129+
130+
//#region private static methods.
131+
/**
132+
* Guards the provided message to be of `ErrorMessage` shape.
133+
* @param message An object of the `ErrorMessage` interface to build the message of a `string` type. The value is checked against
134+
* the proper `object`.
135+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `message` contains
136+
* required `problem` and `fix` properties.
137+
* @returns The return value is a `boolean` indicating whether the provided `message` is an object of `ErrorMessage`.
138+
* @angularpackage
139+
*/
140+
static #guardMessage<Payload extends object>(
141+
message: ErrorMessage,
142+
callback?: ResultCallback<Payload & CallbackPayload>
143+
): message is ErrorMessage {
144+
return guard.objectKey(message, ['fix', 'problem'], callback)
145+
? is.defined(message.template)
146+
? guard.string(message.problem) &&
147+
guard.string(message.fix) &&
148+
ValidationError.#guardTemplate(message.template, callback)
149+
: guard.string(message.problem) && guard.string(message.fix)
150+
: false;
151+
}
152+
153+
/**
154+
* Guards the provided `template` to be a `string` type that includes `[fix]` and `[problem]` words.
155+
* @param template A string-type value to guard.
156+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains
157+
* @returns The return value is a `boolean` indicating whether the provided `template` is a `string` that includes `[fix]` and `[problem]`
158+
* words.
159+
* @angularpackage
160+
*/
161+
static #guardTemplate<Payload extends object>(
162+
template: string,
163+
callback?: ResultCallback<Payload & CallbackPayload>
164+
): template is string {
165+
return guard.string(template, callback)
166+
? template.includes('[fix]') && template.includes('[problem]')
167+
: false;
168+
}
169+
//#endregion private static methods.
56170

57171
/**
58172
* Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned
59173
* to the instance.
60-
* @param message The message of a `string` type or of an `ErrorMessage` interface that is used to throw with an `error`.
174+
* @param message The message of a `string` type or of an `ErrorMessage` interface to throw with an `Error`.
175+
* @param callback An optional function to handle the internal instance of `Callback`.
176+
* @angularpackage
177+
*/
178+
constructor(
179+
message: string | ErrorMessage = '',
180+
callback?: (callback: Callback<VEAllowedCallback>) => void
181+
) {
182+
super();
183+
184+
// Sets the callback for an instance methods.
185+
if (is.function(callback)) {
186+
callback(this.#callback);
187+
}
188+
189+
// Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance.
190+
this.setMessage(message);
191+
}
192+
193+
//#region instance public methods.
194+
/**
195+
* Sets the fix a possible solution to the described problem.
196+
* @param fix A possible solution to the described problem guarded by a `string` type.
197+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `fix` is a `string`.
198+
* By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter
199+
* that gives access to the internal instance of `Callback`.
200+
* @returns The return value is an instance of an `ValidationError`.
61201
* @angularpackage
62202
*/
63-
constructor(message: string | ErrorMessage) {
64-
super(
65-
is.string(message) ? message : ValidationError.defineMessage(message)
66-
);
203+
public setFix(
204+
fix: string,
205+
callback: ResultCallback<CallbackPayload> = this.#callback.getCallback(
206+
'setFix'
207+
)
208+
): this {
209+
if (guard.string(fix, callback)) {
210+
this.#fix = fix;
211+
}
212+
return this;
213+
}
214+
215+
/**
216+
* Sets the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface.
217+
* @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against
218+
* the proper `object`.
219+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message is a string
220+
* type or whether it's an object that contains required `problem` and `fix` properties.
221+
* By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter
222+
* that gives access to the internal instance of `Callback`.
223+
* @returns The return value is an instance of an `ValidationError`.
224+
* @angularpackage
225+
*/
226+
public setMessage(
227+
message: string | ErrorMessage,
228+
callback: ResultCallback<
229+
CallbackPayload & ErrorMessage
230+
> = this.#callback.getCallback('setMessage')
231+
): this {
232+
this.message = is.string(message, callback)
233+
? // Sets a message of a string type from the provided message of `string`.
234+
message
235+
: // Sets a message of a string type from the provided message of `ErrorMessage`.
236+
ValidationError.defineMessage(message, callback);
237+
238+
// Sets `fix`, `problem` and `template` from the provided `message`.
67239
if (is.object(message)) {
68-
Object.assign(this, {
69-
problem: message.problem,
70-
fix: message.fix,
71-
});
240+
this.setFix(message.fix).setProblem(message.problem);
241+
if (is.defined(message.template)) {
242+
this.setTemplate(message.template);
243+
}
72244
}
245+
return this;
246+
}
247+
248+
/**
249+
* Sets description problem of a validation error.
250+
* @param problem Description of a problem of validation error guarded by a `string` type.
251+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `problem` is a
252+
* `string`. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback`
253+
* parameter that gives access to the internal instance of `Callback`.
254+
* @returns The return value is an instance of an `ValidationError`.
255+
* @angularpackage
256+
*/
257+
public setProblem(
258+
problem: string,
259+
callback: ResultCallback<CallbackPayload> = this.#callback.getCallback(
260+
'setProblem'
261+
)
262+
): this {
263+
this.#problem = guard.string(problem, callback) ? problem : this.#problem;
264+
return this;
265+
}
266+
267+
/**
268+
* Sets the template of validation error message.
269+
* @param template A message template guarded by a `string` type with replaceable `[problem]` and `[fix]` words.
270+
* @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a
271+
* `string` that contains `[fix]` and `[problem]` words. By default, it uses an internal callback under the `'setTemplate'` name, which
272+
* can be initially set by the optional `callback` parameter that gives access to the internal instance of `Callback`.
273+
* @angularpackage
274+
*/
275+
public setTemplate(
276+
template: string,
277+
callback: ResultCallback<CallbackPayload> = this.#callback.getCallback(
278+
'setTemplate'
279+
)
280+
): this {
281+
this.#tpl = ValidationError.#guardTemplate(template, callback)
282+
? template
283+
: this.#tpl;
284+
return this;
285+
}
286+
287+
/**
288+
* Throws an error of `ValidationError` with the message built from the stored `fix`, `problem` and `template` or optionally from
289+
* the provided `message`.
290+
* @param message An optional
291+
* @angularpackage
292+
*/
293+
public throw(message?: string | ErrorMessage): void {
294+
if (is.defined(message)) {
295+
this.setMessage(message);
296+
} else {
297+
this.updateMessage();
298+
}
299+
throw this;
300+
}
301+
302+
/**
303+
* Updates the message with a stored `fix`, `problem`, and `template`.
304+
* @angularpackage
305+
*/
306+
public updateMessage(): void {
307+
this.message = ValidationError.defineMessage({
308+
fix: this.#fix,
309+
problem: this.#problem,
310+
template: this.#tpl,
311+
});
73312
}
313+
//#endregion instance public methods.
74314
}

0 commit comments

Comments
 (0)