|
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 | + |
3 | 11 | // Interface. |
4 | 12 | import { ErrorMessage } from '../interface/error-message.interface'; |
| 13 | + |
| 14 | +// Type. |
| 15 | +import { VEAllowedCallback } from '../type/allowed-callback.type'; |
| 16 | + |
5 | 17 | /** |
6 | | - * Manages an `Error` of the validation. |
| 18 | + * Manages an `Error` of validation. |
7 | 19 | */ |
8 | 20 | 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 | + |
9 | 46 | /** |
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. |
11 | 55 | * By default, it's set to `Problem: [problem] => Fix: [fix]`. |
12 | 56 | */ |
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 | + } |
14 | 65 |
|
| 66 | + //#region instance public properties. |
15 | 67 | /** |
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`. |
17 | 70 | */ |
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 | + } |
19 | 88 |
|
20 | 89 | /** |
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`. |
22 | 92 | */ |
23 | 93 | public name = ValidationError.name; |
24 | 94 |
|
25 | 95 | /** |
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`. |
27 | 98 | */ |
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. |
29 | 106 |
|
| 107 | + //#region static public methods |
30 | 108 | /** |
31 | 109 | * Defines the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface. |
32 | 110 | * @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against |
33 | 111 | * 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 | 112 | * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains |
37 | 113 | * required `problem` and `fix` properties. |
38 | 114 | * @returns The return value is a message of a `string` type created from the provided `message` of `ErrorMessage` interface or it's an |
39 | 115 | * empty `string` if the provided message object isn't proper. |
40 | 116 | * @angularpackage |
41 | 117 | */ |
42 | | - static defineMessage( |
| 118 | + public static defineMessage( |
43 | 119 | message: ErrorMessage, |
44 | | - template: string = ValidationError.template, |
45 | | - callback?: ResultCallback |
| 120 | + callback?: ResultCallback<CallbackPayload & ErrorMessage> |
46 | 121 | ): 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) |
50 | 124 | .replace(`[fix]`, message.fix) |
51 | | - .replace(`[problem]`, message.problem); |
52 | | - } |
53 | | - } |
54 | | - return ''; |
| 125 | + .replace(`[problem]`, message.problem) |
| 126 | + : ''; |
55 | 127 | } |
| 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. |
56 | 170 |
|
57 | 171 | /** |
58 | 172 | * Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned |
59 | 173 | * 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`. |
61 | 201 | * @angularpackage |
62 | 202 | */ |
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`. |
67 | 239 | 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 | + } |
72 | 244 | } |
| 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 | + }); |
73 | 312 | } |
| 313 | + //#endregion instance public methods. |
74 | 314 | } |
0 commit comments