From bc8e9653bd5e5546f2a3df2d6d6f18bcefea192b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 03:07:10 +0200 Subject: [PATCH 001/179] docs(README.md): fix. --- README.md | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index efb4919..7fac344 100644 --- a/README.md +++ b/README.md @@ -146,9 +146,9 @@ Manages an [`Error`][js-error] of validation. | ValidationError.prototype. | Description | | :------------------------------------------ | :---------- | | [`fix: string`][error-property-fix] | A possible solution to the described [`problem`][error-property-problem] of validation that is guarded by a [`string`][js-string] type. | -| [`message: string`][error-property-message] | A validation error message guarded by a [`string`][js-string] type that can be built with the [`problem`][error-property-problem] and [`fix`][error-property-fix] of [`ValidationError`](#validationerror) by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. | +| [`message: string`][error-property-message] | A validation error message guarded by a [`string`][js-string] type that can be built with the [`problem`][error-property-problem] and [`fix`][error-property-fix] of [`ValidationError`](#validationerror) on the [`template`][error-property-template] by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. | | [`name: string`][error-property-name] | Error name of a [`string`][js-string] type that is being thrown. | -| [`problem: string`][error-property-problem] | Description of a validation problem guarded by a [`string`][js-string] type. | +| [`problem: string`][error-property-problem] | Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | [error-property-fix]: #validationerrorprototypefix [error-property-message]: #validationerrorprototypemessage @@ -173,7 +173,7 @@ Manages an [`Error`][js-error] of validation. | :---------------------------------------------- | :---------- | | [`setFix()`][error-method-setfix] | Sets the fix a possible solution to the described [`problem`][error-property-problem]. | | [`setMessage()`][error-method-setmessage] | Sets the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | -| [`setProblem()`][error-method-setproblem] | Sets description problem of a [`ValidationError`](#validationerror). | +| [`setProblem()`][error-method-setproblem] | Sets description problem of a validation. | | [`setTemplate()`][error-method-settemplate] | Sets the template of validation error message. | | [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with actual settings. | | [`updateMessage()`][error-method-updatemessage] | Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. | @@ -195,7 +195,7 @@ Manages an [`Error`][js-error] of validation. ![update] -**`2.0.0`:** Uses static private property `#template` and guards the value with private static method `#guardTemplate()` to be `string` type that contains `[fix]` and `[problem]` words. +**`2.0.0`:** Uses static private property `#template` and guards the value with private static method `#guardTemplate()` to be [`string`][js-string] type that contains `[fix]` and `[problem]` words. A template of the error message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of `[problem]` and `[fix]` words. @@ -239,7 +239,7 @@ public set fix(value: string) { ![update] -**`2.0.0`:** Uses inherited from `Error` property and guards the value to be a [`string`][js-string] type. +**`2.0.0`:** Uses inherited from [`Error`][js-error] property and guards the value to be a [`string`][js-string] type. A validation error message guarded by a [`string`][js-string] type that can be build from the [`problem`][error-property-problem] and [`fix`][error-property-fix] of [`ValidationError`](#validationerror) on the [`template`][error-property-template]. It can be set directly or by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. @@ -672,7 +672,7 @@ console.log(validationError.message); ![new] -Sets description [`problem`][error-property-problem] of a validation error. +Sets description [`problem`][error-property-problem] of a validation. ```typescript public setProblem( @@ -690,8 +690,8 @@ public setProblem( | Name: type | Description | | :------------------------------------------- | :---------- | -| `fix: string` | A possible solution to the described [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `fix` is a [`string`][js-string]. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -856,6 +856,26 @@ const validationError = new ValidationError({ fix, problem, template }); validationError.throw(); ``` +```typescript +// Example usage. +import { ValidationError } from '@angular-package/error'; + +// Define a fix. +const fix = 'There is no solution to the described problem.'; + +// Define a problem. +const problem = 'The problem has no solution.'; + +// Define a template. +const template = 'PROBLEM: [problem] FIX: [fix]'; + +// Initialize an instance. +const validationError = new ValidationError(); + +// Throw an error with message. +validationError.throw({ fix, problem, template }); +``` +
#### `ValidationError.prototype.updateMessage()` @@ -990,14 +1010,14 @@ export interface ErrorMessage { **Properties:** **`fix: string`** -Possible solution to the described problem of a [`string`][js-string] type. +A possible solution to the described problem of a [`string`][js-string] type. **`problem: string`** Description of validation problem of a [`string`][js-string] type. **`template?: string`** ![new] -An optional message template of a [`string`][js-string] type. +An optional error message template of a [`string`][js-string] type.
From c77f3cfc8f7958dbfa29022d2e564d6095c2dc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 03:08:14 +0200 Subject: [PATCH 002/179] docs(ValidationError): fix. --- src/lib/validation-error.class.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index f3b0b11..192a368 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -51,8 +51,8 @@ export class ValidationError extends Error { /** * A template of the error message guarded by a `string` type with the replaceable `[problem]` and `[fix]` words. + * By default, it's set to `Problem: [problem] => Fix: [fix]`. It can be set directly or by the `setTemplate()` and `setMessage()` method. * The value is being checked against the existence of `[problem]` and `[fix]` words. - * By default, it's set to `Problem: [problem] => Fix: [fix]`. */ static get template(): string { return ValidationError.#template; @@ -66,7 +66,7 @@ export class ValidationError extends Error { //#region instance public properties. /** * A possible solution to the described `problem` of validation that is guarded by a `string` type. - * By default, it's an empty `string`. + * By default, it's an empty `string`. It can be set directly or by the `setTemplate()` and `setMessage()` method */ public get fix(): string { return this.#fix; @@ -76,8 +76,8 @@ export class ValidationError extends Error { } /** - * A validation error message guarded by a `string` type that can be built with the `problem` and `fix` of `ValidationError` by the - * `throw()` and `setMessage()` method. + * A validation error message guarded by a `string` type that can be built from the `problem` and `fix` of `ValidationError` on the + * `template`. It can be set directly or by the `throw()` or `setMessage()` method. */ public set message(value: string) { super.message = guard.string(value) ? value : super.message; @@ -87,14 +87,13 @@ export class ValidationError extends Error { } /** - * Error name of a `string` type that is being thrown. - * By default, it's `ValidationError`. + * Error name of a `string` type that is being thrown. By default, it's `ValidationError`. */ public name = ValidationError.name; /** - * Description of a validation problem guarded by a `string` type. - * By default, it's an empty `string`. + * Description of a validation problem guarded by a `string` type. By default, it's an empty `string`. + * It can be set directly or by the `setProblem()` and `setMessage()` method. */ public get problem(): string { return this.#problem; @@ -107,7 +106,7 @@ export class ValidationError extends Error { //#region static public methods /** * Defines the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface. - * @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against + * @param message An object of an `ErrorMessage` interface to build a message of a `string` type. The value is checked against * the proper `object`. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains * required `problem` and `fix` properties. @@ -171,7 +170,7 @@ export class ValidationError extends Error { /** * Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned * to the instance. - * @param message The message of a `string` type or of an `ErrorMessage` interface to throw with an `Error`. + * @param message The message of a `string` type or of an `ErrorMessage` interface that is used to throw with an `Error`. * @param callback An optional function to handle the internal instance of `Callback`. * @angularpackage */ @@ -246,8 +245,8 @@ export class ValidationError extends Error { } /** - * Sets description problem of a validation error. - * @param problem Description of a problem of validation error guarded by a `string` type. + * Sets description problem of a validation. + * @param problem Description of validation problem guarded by a `string` type. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `problem` is a * `string`. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` * parameter that gives access to the internal instance of `Callback`. @@ -287,7 +286,8 @@ export class ValidationError extends Error { /** * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem` and `template` or optionally from * the provided `message`. - * @param message An optional + * @param message An optional object of an `ErrorMessage` interface to build the message of a `string` type. + * The value is checked against the proper object. * @angularpackage */ public throw(message?: string | ErrorMessage): void { From 2fdc000c98705819f713e661ad696ce846d6052d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 03:08:33 +0200 Subject: [PATCH 003/179] chore(package): add keywords. --- package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c7f64ff..c112c24 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,13 @@ "@angular", "@angular-package", "@angular-package/error", + "Error", "angular-package", - "Error" + "error fix", + "error message", + "error problem", + "error template", + "validation error" ], "repository": { "type": "git", From 749a6fda3d4f7bb45648317086ed0371e6a1c989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 03:08:45 +0200 Subject: [PATCH 004/179] 2.0.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 91e8312..2ab55b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@angular-package/error", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@angular-package/error", - "version": "2.0.0", + "version": "2.0.1", "license": "MIT", "dependencies": { "tslib": "^2.2.0" diff --git a/package.json b/package.json index c112c24..16339de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@angular-package/error", - "version": "2.0.0", + "version": "2.0.1", "description": "Manages an error.", "author": "Angular Package (https://wvvw.dev)", "homepage": "https://github.com/angular-package/error#readme", From 49fdc9c34959dee2f75de2fb9da16c4494572d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 03:12:26 +0200 Subject: [PATCH 005/179] docs(CHANGELOG.md): fix 2.0.1. --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33e6c49..db7d0c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [2.0.1] - 2021-08-12 + +### 2.0.1 Fix + +- [`c77f3cf`][c77f3cf] + Fix JS documentation of `ValidationError`. + +- [`bc8e965`][bc8e965] + Fix documentation of `README.md`. + +[c77f3cf]: https://github.com/angular-package/error/commit/c77f3cfc8f7958dbfa29022d2e564d6095c2dc65 +[bc8e965]: https://github.com/angular-package/error/commit/bc8e9653bd5e5546f2a3df2d6d6f18bcefea192b + ## [2.0.0] - 2021-08-12 ### 2.0.0 Added From ff06f3ae1b5c922c7605a7fb6301dd238b9e1b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 20:09:39 +0200 Subject: [PATCH 006/179] chore(package): fix. --- package-lock.json | 44 -------------------------------------------- package.json | 2 +- 2 files changed, 1 insertion(+), 45 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2ab55b8..ef0c222 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,32 +14,6 @@ "devDependencies": { "@angular-package/testing": "^1.1.0" }, - "peerDependencies": { - "@angular-package/callback": "^1.0.1", - "@angular-package/type": "^4.2.0" - } - }, - "node_modules/@angular-package/callback": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@angular-package/callback/-/callback-1.0.1.tgz", - "integrity": "sha512-WIsZ+fEO1BUlBDTJYAstd2u+MA+07Ky+mHz51yChXFdsBW8MKk5V4PD26VYjBak98AXpA7GDOvpj/6iJAlUqOA==", - "peer": true, - "dependencies": { - "tslib": "^2.2.0" - }, - "peerDependencies": { - "@angular-package/error": "^1.0.1", - "@angular-package/type": "^4.2.0" - } - }, - "node_modules/@angular-package/error": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@angular-package/error/-/error-1.0.3.tgz", - "integrity": "sha512-u5aYxX8ifnBdXrVtsDPcvlvQlLmzXo4BJkYup4jELlCdfvyzo5e6ze+dDekCzhBuSbHCcEgJi/6zYVufjXK4qQ==", - "peer": true, - "dependencies": { - "tslib": "^2.2.0" - }, "peerDependencies": { "@angular-package/type": "^4.2.0" } @@ -205,24 +179,6 @@ } }, "dependencies": { - "@angular-package/callback": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@angular-package/callback/-/callback-1.0.1.tgz", - "integrity": "sha512-WIsZ+fEO1BUlBDTJYAstd2u+MA+07Ky+mHz51yChXFdsBW8MKk5V4PD26VYjBak98AXpA7GDOvpj/6iJAlUqOA==", - "peer": true, - "requires": { - "tslib": "^2.2.0" - } - }, - "@angular-package/error": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@angular-package/error/-/error-1.0.3.tgz", - "integrity": "sha512-u5aYxX8ifnBdXrVtsDPcvlvQlLmzXo4BJkYup4jELlCdfvyzo5e6ze+dDekCzhBuSbHCcEgJi/6zYVufjXK4qQ==", - "peer": true, - "requires": { - "tslib": "^2.2.0" - } - }, "@angular-package/testing": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@angular-package/testing/-/testing-1.1.0.tgz", diff --git a/package.json b/package.json index 16339de..dcff67b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Angular Package (https://wvvw.dev)", "homepage": "https://github.com/angular-package/error#readme", "peerDependencies": { - "@angular-package/callback": "^1.0.1", + "@angular-package/callback": "^2.0.0", "@angular-package/type": "^4.2.0" }, "dependencies": { From ab4334088ad99c77185ef4915d31120fcce75876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 20:09:45 +0200 Subject: [PATCH 007/179] 2.0.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index ef0c222..f938e59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@angular-package/error", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@angular-package/error", - "version": "2.0.1", + "version": "2.0.2", "license": "MIT", "dependencies": { "tslib": "^2.2.0" diff --git a/package.json b/package.json index dcff67b..c802d4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@angular-package/error", - "version": "2.0.1", + "version": "2.0.2", "description": "Manages an error.", "author": "Angular Package (https://wvvw.dev)", "homepage": "https://github.com/angular-package/error#readme", From 77d5090e9a8ebaf34061aaab25a736dd58b98ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 20:26:30 +0200 Subject: [PATCH 008/179] chore(package-lock): update. --- package-lock.json | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/package-lock.json b/package-lock.json index f938e59..84fb7ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,33 @@ "@angular-package/testing": "^1.1.0" }, "peerDependencies": { + "@angular-package/callback": "^2.0.0", + "@angular-package/type": "^4.2.0" + } + }, + "node_modules/@angular-package/callback": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@angular-package/callback/-/callback-2.0.0.tgz", + "integrity": "sha512-4c2aG3s04cOOzIPpgI1LckaImS5wJXf5tnr/cFsyVbG9MMB+Agt3hP+OTGHdERTgLD38ZUkbURI2fcFmLRMOQw==", + "peer": true, + "dependencies": { + "tslib": "^2.2.0" + }, + "peerDependencies": { + "@angular-package/error": "^2.0.2", + "@angular-package/type": "^4.2.0" + } + }, + "node_modules/@angular-package/error": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@angular-package/error/-/error-2.0.2.tgz", + "integrity": "sha512-kXigjTEKTZVDurGFXQSAQpN5EmTe70AiPW+oCcjKaqmaN1XJlkFFdgS+kTsVVGefd3Mh3h6Y5DF95y/RtPkF4w==", + "peer": true, + "dependencies": { + "tslib": "^2.2.0" + }, + "peerDependencies": { + "@angular-package/callback": "^2.0.0", "@angular-package/type": "^4.2.0" } }, @@ -179,6 +206,24 @@ } }, "dependencies": { + "@angular-package/callback": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@angular-package/callback/-/callback-2.0.0.tgz", + "integrity": "sha512-4c2aG3s04cOOzIPpgI1LckaImS5wJXf5tnr/cFsyVbG9MMB+Agt3hP+OTGHdERTgLD38ZUkbURI2fcFmLRMOQw==", + "peer": true, + "requires": { + "tslib": "^2.2.0" + } + }, + "@angular-package/error": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@angular-package/error/-/error-2.0.2.tgz", + "integrity": "sha512-kXigjTEKTZVDurGFXQSAQpN5EmTe70AiPW+oCcjKaqmaN1XJlkFFdgS+kTsVVGefd3Mh3h6Y5DF95y/RtPkF4w==", + "peer": true, + "requires": { + "tslib": "^2.2.0" + } + }, "@angular-package/testing": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@angular-package/testing/-/testing-1.1.0.tgz", From 019326ca3e7585dcaae7ae7836f674cd7bca8208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 12 Aug 2021 20:33:26 +0200 Subject: [PATCH 009/179] docs(CHANGELOG.md): update. --- CHANGELOG.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db7d0c4..ef4c56d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [2.0.2] - 2021-08-12 + +### 2.0.2 Fixed + +- [`ff06f3a`][ff06f3a] + Fixed `package.json` peer dependencies cause of `@angular-package/callback`. + +[ff06f3a]: https://github.com/angular-package/error/commit/ff06f3ae1b5c922c7605a7fb6301dd238b9e1b7a + ## [2.0.1] - 2021-08-12 -### 2.0.1 Fix +### 2.0.1 Fixed - [`c77f3cf`][c77f3cf] Fix JS documentation of `ValidationError`. From 01cd8c3bff627e1e2502655861ef234a3f15c3ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Tue, 14 Sep 2021 20:39:04 +0200 Subject: [PATCH 010/179] chore(package): update. --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c802d4b..e6eaf72 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,18 @@ { "name": "@angular-package/error", - "version": "2.0.2", + "version": "3.0.0", "description": "Manages an error.", "author": "Angular Package (https://wvvw.dev)", "homepage": "https://github.com/angular-package/error#readme", "peerDependencies": { - "@angular-package/callback": "^2.0.0", - "@angular-package/type": "^4.2.0" + "@angular-package/callback": "^3.0.0", + "@angular-package/type": "^5.0.0" }, "dependencies": { "tslib": "^2.2.0" }, "devDependencies": { - "@angular-package/testing": "^1.1.0" + "@angular-package/testing": "^2.0.0" }, "publishConfig": { "access": "public", From 5a11a51e1082c34ec98357262ec635d51ff0cf90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 30 Sep 2021 19:49:51 +0200 Subject: [PATCH 011/179] docs(README.md): update. --- README.md | 293 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 177 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index 7fac344..9fcff8e 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Sets ## Skeleton -This package was built by the [library skeleton][skeleton] which was generated with [Angular CLI](https://github.com/angular/angular-cli) version 12.1.1. +This package was built by the [library skeleton][skeleton] which was generated with [Angular CLI](https://github.com/angular/angular-cli) version 12.2.5. Copy this package to the `packages/error` folder of the [library skeleton][skeleton] then run the commands below. @@ -143,39 +143,41 @@ Manages an [`Error`][js-error] of validation. **Instance properties:** -| ValidationError.prototype. | Description | -| :------------------------------------------ | :---------- | -| [`fix: string`][error-property-fix] | A possible solution to the described [`problem`][error-property-problem] of validation that is guarded by a [`string`][js-string] type. | -| [`message: string`][error-property-message] | A validation error message guarded by a [`string`][js-string] type that can be built with the [`problem`][error-property-problem] and [`fix`][error-property-fix] of [`ValidationError`](#validationerror) on the [`template`][error-property-template] by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. | -| [`name: string`][error-property-name] | Error name of a [`string`][js-string] type that is being thrown. | -| [`problem: string`][error-property-problem] | Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | +| ValidationError.prototype. | Description | +| :-------------------------------------------- | :---------- | +| [`fix: string`][error-property-fix] | A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] type. | +| [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | +| [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | +| [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. | [error-property-fix]: #validationerrorprototypefix [error-property-message]: #validationerrorprototypemessage [error-property-name]: #validationerrorprototypename [error-property-problem]: #validationerrorprototypeproblem +[error-property-template]: #validationerrorprototypetemplate **Static methods:** | ValidationError. | Description | | :------------------------------------------------- | :---------- | -| [`defineMessage()`](#validationerrordefinemessage) | Defines the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | +| [`defineMessage()`](#validationerrordefinemessage) | Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | **Constructor:** | Constructor | Description | | :-------------------------------------------------- | :---------- | -| [`ValidationError()`](#validationerror-constructor) | Creates a new instance with the message. If the provided `message` is an [`object`][js-object], then its properties are assigned to the instance. | +| [`ValidationError()`](#validationerror-constructor) | Creates a new instance with the [`message`][error-property-message]. If the provided `message` is an [`object`][js-object], then its properties are assigned to the instance. | **Instance methods:** | ValidationError.prototype. | Description | | :---------------------------------------------- | :---------- | -| [`setFix()`][error-method-setfix] | Sets the fix a possible solution to the described [`problem`][error-property-problem]. | -| [`setMessage()`][error-method-setmessage] | Sets the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | -| [`setProblem()`][error-method-setproblem] | Sets description problem of a validation. | -| [`setTemplate()`][error-method-settemplate] | Sets the template of validation error message. | -| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with actual settings. | +| [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | +| [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. | +| [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | +| [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | +| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. | | [`updateMessage()`][error-method-updatemessage] | Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. | [error-method-setfix]: #validationerrorprototypesetfix @@ -189,15 +191,9 @@ Manages an [`Error`][js-error] of validation. ### `ValidationError` static properties ----- - #### `ValidationError.template` -![update] - -**`2.0.0`:** Uses static private property `#template` and guards the value with private static method `#guardTemplate()` to be [`string`][js-string] type that contains `[fix]` and `[problem]` words. - -A template of the error message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of `[problem]` and `[fix]` words. +A template of the error message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. The value is being checked against the existence of `[problem]` and `[fix]` words. ```typescript static get template(): string { @@ -214,22 +210,16 @@ static set template(value: string) { ### `ValidationError` instance public properties ----- - #### `ValidationError.prototype.fix` -![update] - -**`2.0.0`:** Uses static private property `#fix` and guards the value to be a [`string`][js-string] type. - -A possible solution to the described [`problem`][error-property-problem] of validation that is guarded by a [`string`][js-string] type. By default, it's an empty [`string`][js-string]. It can be set directly or by the [`setFix()`][error-method-setfix] and [`setMessage()`][error-method-setmessage] method. +A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] type. It can be set directly or by the [`setFix()`][error-method-setfix] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. ```typescript public get fix(): string { return this.#fix; } public set fix(value: string) { - this.#fix = guard.string(value) ? value : this.#fix; + this.#fix = guardString(value) ? value : this.#fix; } ``` @@ -237,11 +227,7 @@ public set fix(value: string) { #### `ValidationError.prototype.message` -![update] - -**`2.0.0`:** Uses inherited from [`Error`][js-error] property and guards the value to be a [`string`][js-string] type. - -A validation error message guarded by a [`string`][js-string] type that can be build from the [`problem`][error-property-problem] and [`fix`][error-property-fix] of [`ValidationError`](#validationerror) on the [`template`][error-property-template]. It can be set directly or by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. +A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. ```typescript public set message(value: string) { @@ -256,7 +242,7 @@ public get message(): string { #### `ValidationError.prototype.name` -Error name of a [`string`][js-string] type that is being thrown. By default, it's ['ValidationError'](#validationerror). +[`Error`][js-error] name of [`string`][js-string] type that is being thrown. By default, it's ['ValidationError'](#validationerror). ```typescript public name = ValidationError.name; @@ -266,9 +252,7 @@ public name = ValidationError.name; #### `ValidationError.prototype.problem` -![update] - -Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. By default, it's an empty [`string`][js-string]. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. +Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. By default, it's an empty [`string`][js-string]. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. ```typescript public get problem(): string { @@ -281,23 +265,34 @@ public set problem(value: string) {
-### `ValidationError` static methods +#### `ValidationError.prototype.template` ----- +![new] -#### `ValidationError.defineMessage()` +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of `[problem]` and `[fix]` words. -![update] +```typescript +public get template(): string { + return this.#tpl; +} +public set template(value: string) { + this.#tpl = ValidationError.#guardTemplate(value) ? value : this.#tpl; +} +``` -**`2.0.0`:** Adds template to the provided `message` instead of separate parameter and guards it with a static `#guardMessage()` method. +
+ +### `ValidationError` static methods + +#### `ValidationError.defineMessage()` -Defines the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. +Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. ```typescript // Syntax. public static defineMessage( message: ErrorMessage, - callback?: ResultCallback + callback?: ResultCallback ): string { return ValidationError.#guardMessage(message, callback) ? (message.template || ValidationError.template) @@ -311,12 +306,12 @@ public static defineMessage( | Name: type | Description | | :-------------------------- | :---------- | -| `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] | -| `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 | +| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided message is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` words. | **Returns:** -The **return value** is a message of a [`string`][js-string] type created from the provided `message` of [`ErrorMessage`](#errormessage) interface, or it's an empty [`string`][js-string] if the provided message [`object`][js-object] isn't proper. +The **return value** is a message of [`string`][js-string] type created from the provided `message` of the [`ErrorMessage`](#errormessage) interface, or it's an empty [`string`][js-string] if the provided message [`object`][js-object] isn't proper. **Usage:** @@ -403,15 +398,9 @@ const errorMessage = ValidationError.defineMessage( ### `ValidationError` constructor ----- - #### `ValidationError()` -![update] - -**`2.0.0`:** Adds template to the provided `message` instead of separate parameter and uses a new method [`setMessage()`][error-method-setmessage] to set message. Handle the callback for all instance methods with the callback parameter. - -Creates a new instance with the message. If the provided `message` is an [`object`][js-object], then its properties are assigned to the instance. +Creates a new instance with the [`message`][error-property-message]. If the provided [`message`][error-property-message] is an [`object`][js-object], then its properties are assigned to the instance. ```typescript // Syntax. @@ -435,8 +424,8 @@ constructor( | Name: type | Description | | :----------------------------------------------------------- | :---------- | -| `message: string \| ErrorMessage` | The message of a [`string`][js-string] type or of an [`ErrorMessage`](#errormessage) interface that is used to throw with an [`Error`][js-error]. | -| `callback?: (callback: Callback) => void` | An optional function to handle the internal instance of [`Callback`][callback-github-readme]. | +| `message: string \| ErrorMessage` | The message of [`string`][js-string] type or of an [`ErrorMessage`](#errormessage) interface that is used to throw with an [`Error`][js-error]. | +| `callback?: (callback: Callback) => void` | An optional [`function`][js-function] to handle the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -513,11 +502,9 @@ const validationError = new ValidationError( ### `ValidationError` instance public methods ----- - #### `ValidationError.prototype.setFix()` -![new] +![update] Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. @@ -525,23 +512,21 @@ Sets the [`fix`][error-property-fix] a possible solution to the described [`prob // Syntax. public setFix( fix: string, - callback: ResultCallback = this.#callback.getCallback( + callback: ResultCallback = this.#callback.getResultCallback( 'setFix' ) ): this { - if (guard.string(fix, callback)) { - this.#fix = fix; - } + this.#fix = guardString(fix, callback) ? fix : this.#fix; return this; } ``` **Parameters:** -| Name: type | Description | -| :------------------------------------------- | :---------- | -| `fix: string` | A possible solution to the described problem guarded by a [`string`][js-string] type. | -| `callback?: ResultCallback` | 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]. By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :--------------------------------- | :---------- | +| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| `callback: ResultCallback` | 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]. By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -587,30 +572,34 @@ validationError.setFix(fix, (result, payload) => { #### `ValidationError.prototype.setMessage()` -![new] +![update] -Sets the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. +Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. ```typescript // Syntax. public setMessage( message: string | ErrorMessage, - callback: ResultCallback< - CallbackPayload & ErrorMessage - > = this.#callback.getCallback('setMessage') + callback: ResultCallback = this.#callback.getResultCallback( + 'setMessage' + ) ): this { - this.message = is.string(message, callback) - ? // Sets a message of a string type from the provided message of `string`. - message - : // Sets a message of a string type from the provided message of `ErrorMessage`. - ValidationError.defineMessage(message, callback); - - // Sets `fix`, `problem` and optionally `template` from the provided `message`. - if (is.object(message)) { + super.message = isString(message, callback) + ? message + : ValidationError.defineMessage( + { ...message, ...{ template: message.template || this.#tpl } }, + callback + ); + + // Sets `fix`, `problem` and `template` from the provided `message`. + if (isObject(message)) { this.setFix(message.fix).setProblem(message.problem); - if (is.defined(message.template)) { + if (isDefined(message.template)) { this.setTemplate(message.template); } + } else { + this.#problem = ''; + this.#fix = ''; } return this; } @@ -618,10 +607,10 @@ public setMessage( **Parameters:** -| Name: type | Description | -| :------------------------------------------- | :---------- | -| `message: string \| ErrorMessage` | An object of an [`ErrorMessage`](#errormessage) interface to build the message of a [`string`][js-string] type. The value is checked against the proper `object`. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is a [`string`][js-string] type or whether it's an object that contains required [`problem`][error-property-problem] and [`fix`][error-property-fix] properties. By default, it uses an internal callback under the `'setMessage'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :----------------------------------------- | :---------- | +| `message: string \| ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `callback: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` words. By default, it uses an internal callback under the `'setMessage'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -670,28 +659,28 @@ console.log(validationError.message); #### `ValidationError.prototype.setProblem()` -![new] +![update] -Sets description [`problem`][error-property-problem] of a validation. +Sets description of the validation [`problem`][error-property-problem]. ```typescript public setProblem( problem: string, - callback: ResultCallback = this.#callback.getCallback( + callback: ResultCallback = this.#callback.getResultCallback( 'setProblem' ) ): this { - this.#problem = guard.string(problem, callback) ? problem : this.#problem; + this.#problem = guardString(problem, callback) ? problem : this.#problem; return this; } ``` **Parameters:** -| Name: type | Description | -| :------------------------------------------- | :---------- | -| `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :---------------------------------- | :---------- | +| `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -737,14 +726,14 @@ validationError.setProblem(problem, (result, payload) => { #### `ValidationError.prototype.setTemplate()` -![new] +![update] -Sets the [`template`][error-property-template] of validation error message. +Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. ```typescript public setTemplate( template: string, - callback: ResultCallback = this.#callback.getCallback( + callback: ResultCallback = this.#callback.getResultCallback( 'setTemplate' ) ): this { @@ -757,10 +746,10 @@ public setTemplate( **Parameters:** -| Name: type | Description | -| :------------------------------------------- | :---------- | -| `template: string` | A message [`template`][error-property-template] guarded by a [`string`][js-string] type with replaceable `[problem]` and `[fix]` words. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` words. By default, it uses an internal callback under the `'setTemplate'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :--------------------------------- | :---------- | +| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable `[problem]` and `[fix]` words. | +| `callback: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` words. By default, it uses an internal callback under the `'setTemplate'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -809,8 +798,6 @@ validationError.setTemplate(template, (result, payload) => { #### `ValidationError.prototype.throw()` -![new] - Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. ```typescript @@ -828,7 +815,7 @@ public throw(message?: string | ErrorMessage): void { | Name: type | Description | | :--------------------------------- | :---------- | -| `message?: string \| ErrorMessage` | An optional object of an [`ErrorMessage`](#errormessage) interface to build the message of a [`string`][js-string] type. The value is checked against the proper `object`. | +| `message?: string \| ErrorMessage` | An optional parameter of [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | **Returns:** @@ -880,17 +867,20 @@ validationError.throw({ fix, problem, template }); #### `ValidationError.prototype.updateMessage()` -![new] +![update] Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. ```typescript -public updateMessage(): void { - this.message = ValidationError.defineMessage({ - fix: this.#fix, - problem: this.#problem, - template: this.#tpl, - }); +public updateMessage(): this { + super.message = guardStringLength(this.#problem, { min: 1 }) + ? ValidationError.defineMessage({ + fix: this.#fix, + problem: this.#problem, + template: this.#tpl, + }) + : this.message; + return this; } ``` @@ -987,16 +977,88 @@ addPerson({ ``` +```typescript +// Example usage. +import { ValidationError } from '@angular-package/error'; + +const validationError = new ValidationError(); + +try { + validationError + .setMessage('message of validation error') + .throw(); +} catch (e) { + // message of validation error + console.log(e.message); + // Empty string. + console.log(e.problem); + // Empty string. + console.log(e.fix); +} + +try { + validationError + .setMessage('message of validation error') + .setProblem('my problem') + .setFix('my fix') + .throw(); +} catch (e) { + // Problem: my problem => Fix: my fix + console.log(e.message); + // my problem + console.log(e.problem); + // my fix + console.log(e.fix); +} + +try { + validationError + .setMessage('message of validation error') + .setProblem('my problem') + .setFix('my fix') + .setMessage('message of validation error') + .throw(); +} catch (e) { + // message of validation error + console.log(e.message); + // Empty string + console.log(e.problem); + // Empty string + console.log(e.fix); +} + +try { + validationError + .setMessage({ problem: 'my problem', fix: 'my fix'}) + .throw(); +} catch (e) { + // Problem: my problem => Fix: my fix + console.log(e.message); + // my problem + console.log(e.problem); + // my fix + console.log(e.fix); +} + +try { + validationError + .throw({ problem: 'my problem', fix: 'my fix'}); +} catch (e) { + // Problem: my problem => Fix: my fix + console.log(e.message); + // my problem + console.log(e.problem); + // my fix + console.log(e.fix); +} +``` +
## Interface #### `ErrorMessage` -![update] - -**`2.0.0`:** Adds an optional `template` property. - The shape of an [`object`][js-object] for an [`error`][js-error] message that contains a possible solution to the described problem. ```typescript @@ -1016,7 +1078,6 @@ A possible solution to the described problem of a [`string`][js-string] type. Description of validation problem of a [`string`][js-string] type. **`template?: string`** -![new] An optional error message template of a [`string`][js-string] type.
From b61683a3faba0f9a7164139b82fa2702847c005a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 30 Sep 2021 19:55:58 +0200 Subject: [PATCH 012/179] docs(README.md): update. --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9fcff8e..c15ef67 100644 --- a/README.md +++ b/README.md @@ -135,11 +135,9 @@ Manages an [`Error`][js-error] of validation. **Static properties:** -| ValidationError. | Description | -| :-------------------------------------------- | :---------- | -| [`template: string`][error-property-template] | A template of the error message guarded by a [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | - -[error-property-template]: #validationerrortemplate +| ValidationError. | Description | +| :---------------------------------------------- | :---------- | +| [`template: string`](##validationerrortemplate) | A template of the error message guarded by a [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | **Instance properties:** From fda3b8d1974ebd50334a3ebae0a998015852509b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 30 Sep 2021 21:53:10 +0200 Subject: [PATCH 013/179] docs(README.md): update. --- README.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c15ef67..b52a499 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ Manages an [`Error`][js-error] of validation. | ValidationError. | Description | | :---------------------------------------------- | :---------- | -| [`template: string`](##validationerrortemplate) | A template of the error message guarded by a [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | +| [`template: string`](##validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | **Instance properties:** @@ -191,7 +191,7 @@ Manages an [`Error`][js-error] of validation. #### `ValidationError.template` -A template of the error message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. The value is being checked against the existence of `[problem]` and `[fix]` words. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. The value is being checked against the existence of `[problem]` and `[fix]` words. ```typescript static get template(): string { @@ -250,7 +250,7 @@ public name = ValidationError.name; #### `ValidationError.prototype.problem` -Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. By default, it's an empty [`string`][js-string]. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. +Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. By default, it's an empty [`string`][js-string]. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. ```typescript public get problem(): string { @@ -284,6 +284,8 @@ public set template(value: string) { #### `ValidationError.defineMessage()` +![update] + Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. ```typescript @@ -475,22 +477,22 @@ const validationError = new ValidationError( } */ .setResultCallback('setFix', (result, payload) => - console.log(`setFix`, result, payload); + console.log(`setFix`, result, payload) ) // Console: 'setFix true There is no solution to the described problem.' .setResultCallback('setMessage', (result, payload) => - console.log(`setMessage`, result, payload); + console.log(`setMessage`, result, payload) ) // Console: 'setProblem true The problem has no solution.' .setResultCallback('setProblem', (result, payload) => - console.log(`setProblem`, result, payload); + console.log(`setProblem`, result, payload) ) // Console: 'setTemplate true PROBLEM: [problem] FIX: [fix]' .setResultCallback('setTemplate', (result, payload) => - console.log(`setTemplate`, result, payload); + console.log(`setTemplate`, result, payload) ); } ); @@ -677,7 +679,7 @@ public setProblem( | Name: type | Description | | :---------------------------------- | :---------- | -| `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by a [`string`][js-string] type. | +| `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -815,10 +817,6 @@ public throw(message?: string | ErrorMessage): void { | :--------------------------------- | :---------- | | `message?: string \| ErrorMessage` | An optional parameter of [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -**Returns:** - -The **return value** is an instance of an [`ValidationError`](#validationerror). - **Usage:** ```typescript @@ -1057,7 +1055,7 @@ try { #### `ErrorMessage` -The shape of an [`object`][js-object] for an [`error`][js-error] message that contains a possible solution to the described problem. +The shape of an [`object`][js-object] for an [`error`][js-error] message that contains a possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem]. ```typescript export interface ErrorMessage { From 84a7a5c399989fddd9227ef5d29ad8a21e99f63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 1 Oct 2021 02:21:51 +0200 Subject: [PATCH 014/179] docs(README.md): update. --- README.md | 81 +++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index b52a499..f2f47bf 100644 --- a/README.md +++ b/README.md @@ -198,9 +198,8 @@ static get template(): string { return ValidationError.#template; } static set template(value: string) { - ValidationError.#template = ValidationError.#guardTemplate(value) - ? value - : ValidationError.#template; + ValidationError.#guardTemplate(value) && + (ValidationError.#template = value); } ``` @@ -217,7 +216,7 @@ public get fix(): string { return this.#fix; } public set fix(value: string) { - this.#fix = guardString(value) ? value : this.#fix; + guardString(value) && (this.#fix = value); } ``` @@ -229,7 +228,7 @@ A validation [`error`][js-error] message guarded by [`string`][js-string] type t ```typescript public set message(value: string) { - super.message = guard.string(value) ? value : super.message; + guardString(value) && (super.message = value); } public get message(): string { return super.message; @@ -257,7 +256,7 @@ public get problem(): string { return this.#problem; } public set problem(value: string) { - this.#problem = guard.string(value) ? value : this.#problem; + guardString(value) && (this.#problem = value); } ``` @@ -274,7 +273,7 @@ public get template(): string { return this.#tpl; } public set template(value: string) { - this.#tpl = ValidationError.#guardTemplate(value) ? value : this.#tpl; + ValidationError.#guardTemplate(value) && (this.#tpl = value); } ``` @@ -386,9 +385,9 @@ const problem = 'The problem has no solution.'; const errorMessage = ValidationError.defineMessage( { fix, problem }, - (result, payload) => { + (result, value) => { // Do something with the `result` of the `message` check - // and `payload`. + // and `value`. return result; } ); @@ -411,9 +410,7 @@ constructor( super(); // Sets the callback for an instance methods. - if (is.function(callback)) { - callback(this.#callback); - } + isFunction(callback) && callback(this.#callback); // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. this.setMessage(message); @@ -476,23 +473,23 @@ const validationError = new ValidationError( "template": "PROBLEM: [problem] FIX: [fix]" } */ - .setResultCallback('setFix', (result, payload) => - console.log(`setFix`, result, payload) + .setResultCallback('setFix', (result, value) => + console.log(`setFix`, result, value) ) // Console: 'setFix true There is no solution to the described problem.' - .setResultCallback('setMessage', (result, payload) => - console.log(`setMessage`, result, payload) + .setResultCallback('setMessage', (result, value) => + console.log(`setMessage`, result, value) ) // Console: 'setProblem true The problem has no solution.' - .setResultCallback('setProblem', (result, payload) => - console.log(`setProblem`, result, payload) + .setResultCallback('setProblem', (result, value) => + console.log(`setProblem`, result, value) ) // Console: 'setTemplate true PROBLEM: [problem] FIX: [fix]' - .setResultCallback('setTemplate', (result, payload) => - console.log(`setTemplate`, result, payload) + .setResultCallback('setTemplate', (result, value) => + console.log(`setTemplate`, result, value) ); } ); @@ -516,7 +513,7 @@ public setFix( 'setFix' ) ): this { - this.#fix = guardString(fix, callback) ? fix : this.#fix; + guardString(fix, callback) && (this.#fix = fix); return this; } ``` @@ -559,11 +556,11 @@ const validationError = new ValidationError(); const fix = 'There is no solution to the described problem.'; // Set the fix and handle the check of it with a callback. -validationError.setFix(fix, (result, payload) => { +validationError.setFix(fix, (result, value) => { // Returns `true`. result; // Returns `There is no solution to the described problem.`. - payload; + value; return result; }); ``` @@ -635,7 +632,7 @@ const problem = 'The problem has no solution.'; const template = 'PROBLEM: [problem], FIX: [fix]'; // Set the message and handle the check of it with a callback. -validationError.setMessage({ fix, problem }, (result, payload) => { +validationError.setMessage({ fix, problem }, (result, value) => { // Returns `false` then `true`. result; /* @@ -645,7 +642,7 @@ validationError.setMessage({ fix, problem }, (result, payload) => { "template": "PROBLEM: [problem] FIX: [fix]" } */ - payload; + value; return result; }); /* @@ -670,7 +667,7 @@ public setProblem( 'setProblem' ) ): this { - this.#problem = guardString(problem, callback) ? problem : this.#problem; + guardString(problem, callback) && (this.#problem = problem); return this; } ``` @@ -713,11 +710,11 @@ const validationError = new ValidationError(); const problem = 'The problem has no solution.'; // Set the problem and handle the check of it with a callback. -validationError.setProblem(problem, (result, payload) => { +validationError.setProblem(problem, (result, value) => { // Returns `true`. result; // Returns 'The problem has no solution.' - payload; + value; return result; }); ``` @@ -737,9 +734,8 @@ public setTemplate( 'setTemplate' ) ): this { - this.#tpl = ValidationError.#guardTemplate(template, callback) - ? template - : this.#tpl; + ValidationError.#guardTemplate(template, callback) && + (this.#tpl = template); return this; } ``` @@ -785,11 +781,11 @@ const validationError = new ValidationError(); const template = 'PROBLEM: [problem], FIX: [fix]'; // Set the template and handle the check of it with a callback. -validationError.setTemplate(template, (result, payload) => { +validationError.setTemplate(template, (result, value) => { // Returns `true`. result; // Returns 'PROBLEM: [problem], FIX: [fix]' - payload; + value; return result; }); ``` @@ -802,11 +798,7 @@ Throws an error of [`ValidationError`](#validationerror) with the message built ```typescript public throw(message?: string | ErrorMessage): void { - if (is.defined(message)) { - this.setMessage(message); - } else { - this.updateMessage(); - } + isDefined(message) ? this.setMessage(message) : this.updateMessage(); throw this; } ``` @@ -869,13 +861,12 @@ Updates the message with a stored [`fix`][error-property-fix], [`problem`][error ```typescript public updateMessage(): this { - super.message = guardStringLength(this.#problem, { min: 1 }) - ? ValidationError.defineMessage({ - fix: this.#fix, - problem: this.#problem, - template: this.#tpl, - }) - : this.message; + guardStringLength(this.#problem, { min: 1 }) && + (super.message = ValidationError.defineMessage({ + fix: this.#fix, + problem: this.#problem, + template: this.#tpl, + })); return this; } ``` From ecb0a864f2c586db1c028773949c1381b104a4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 1 Oct 2021 02:48:12 +0200 Subject: [PATCH 015/179] docs(README.md): fix. --- README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f2f47bf..21734ae 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ Manages an [`Error`][js-error] of validation. | ValidationError. | Description | | :---------------------------------------------- | :---------- | -| [`template: string`](##validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | +| [`template: string`](#validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | **Instance properties:** @@ -227,12 +227,12 @@ public set fix(value: string) { A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. ```typescript -public set message(value: string) { - guardString(value) && (super.message = value); -} public get message(): string { return super.message; } +public set message(value: string) { + guardString(value) && (super.message = value); +} ```
@@ -249,7 +249,7 @@ public name = ValidationError.name; #### `ValidationError.prototype.problem` -Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. By default, it's an empty [`string`][js-string]. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. +Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. ```typescript public get problem(): string { @@ -571,6 +571,8 @@ validationError.setFix(fix, (result, value) => { ![update] +TODO: Add detailed description about clear #problem, #fix and inject #tpl. + Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. ```typescript @@ -794,6 +796,10 @@ validationError.setTemplate(template, (result, value) => { #### `ValidationError.prototype.throw()` +![update] + +TODO: Add detailed description about setting or updating the message. + Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. ```typescript From 209fa2eacbb043faf60967ce414a4f3871cb7e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 1 Oct 2021 19:07:11 +0200 Subject: [PATCH 016/179] docs(README.md): update. --- README.md | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 21734ae..15413c0 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,8 @@ Manages an [`Error`][js-error] of validation. **Static properties:** -| ValidationError. | Description | -| :---------------------------------------------- | :---------- | +| ValidationError. | Description | +| :--------------------------------------------- | :---------- | | [`template: string`](#validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | **Instance properties:** @@ -191,6 +191,8 @@ Manages an [`Error`][js-error] of validation. #### `ValidationError.template` +[![update]][error-github-changelog] + A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. The value is being checked against the existence of `[problem]` and `[fix]` words. ```typescript @@ -209,6 +211,8 @@ static set template(value: string) { #### `ValidationError.prototype.fix` +[![update]][error-github-changelog] + A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] type. It can be set directly or by the [`setFix()`][error-method-setfix] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. ```typescript @@ -224,6 +228,8 @@ public set fix(value: string) { #### `ValidationError.prototype.message` +[![update]][error-github-changelog] + A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. ```typescript @@ -249,6 +255,8 @@ public name = ValidationError.name; #### `ValidationError.prototype.problem` +[![update]][error-github-changelog] + Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. ```typescript @@ -283,7 +291,7 @@ public set template(value: string) { #### `ValidationError.defineMessage()` -![update] +[![update]][error-github-changelog] Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. @@ -399,6 +407,8 @@ const errorMessage = ValidationError.defineMessage( #### `ValidationError()` +[![update]][error-github-changelog] + Creates a new instance with the [`message`][error-property-message]. If the provided [`message`][error-property-message] is an [`object`][js-object], then its properties are assigned to the instance. ```typescript @@ -501,7 +511,7 @@ const validationError = new ValidationError( #### `ValidationError.prototype.setFix()` -![update] +[![update]][error-github-changelog] Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. @@ -569,12 +579,12 @@ validationError.setFix(fix, (result, value) => { #### `ValidationError.prototype.setMessage()` -![update] - -TODO: Add detailed description about clear #problem, #fix and inject #tpl. +[![update]][error-github-changelog] Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. +> If the provided `message` is a [`string`][js-string] type, then properties [`problem`][error-property-problem], and [`fix`][error-property-fix] are set to empty [`string`][js-string] to not confuse their relation to the [`message`][error-property-message] and have a possibility throw an error by the [`throw()`](#validationerrorprototypethrow) method with just only a [`string`][js-string] type. + ```typescript // Syntax. public setMessage( @@ -658,7 +668,7 @@ console.log(validationError.message); #### `ValidationError.prototype.setProblem()` -![update] +[![update]][error-github-changelog] Sets description of the validation [`problem`][error-property-problem]. @@ -725,7 +735,7 @@ validationError.setProblem(problem, (result, value) => { #### `ValidationError.prototype.setTemplate()` -![update] +[![update]][error-github-changelog] Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. @@ -796,12 +806,13 @@ validationError.setTemplate(template, (result, value) => { #### `ValidationError.prototype.throw()` -![update] - -TODO: Add detailed description about setting or updating the message. +[![update]][error-github-changelog] Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. +> The provided `message` of the [`ErrorMessage`](#errormessage) is set to the instance to have the possibility to catch `problem` and `fix` properties to know that the message was built from it (it's not just a string message). +> If the `message` parameter is not provided, then if the `problem` of an instance of `ValidationError` is not an empty string it builds the message and overwrites the `message`. + ```typescript public throw(message?: string | ErrorMessage): void { isDefined(message) ? this.setMessage(message) : this.updateMessage(); @@ -861,7 +872,7 @@ validationError.throw({ fix, problem, template }); #### `ValidationError.prototype.updateMessage()` -![update] +[![update]][error-github-changelog] Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. @@ -1089,7 +1100,8 @@ type VEAllowedCallback = 'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'; ## Experimental -![experimental] +![experimental] [![update]][error-github-changelog] + ### Message builder @@ -1257,6 +1269,7 @@ MIT © angular-package ([license][error-license]) [error-license]: https://github.com/angular-package/error/blob/master/LICENSE [error-stars]: https://github.com/angular-package/error/stargazers + [error-github-changelog]: https://github.com/angular-package/error/blob/main/CHANGELOG.md From 7aaa50a778fbdd994c13bd3acb1a5810325a7533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 20:43:37 +0200 Subject: [PATCH 017/179] docs(README.md): update. --- README.md | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 15413c0..7e0b5cf 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ Manages an [`Error`][js-error] of validation. | ValidationError. | Description | | :--------------------------------------------- | :---------- | -| [`template: string`](#validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | +| [`template: string`](#validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | **Instance properties:** @@ -147,7 +147,7 @@ Manages an [`Error`][js-error] of validation. | [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | | [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. | [error-property-fix]: #validationerrorprototypefix [error-property-message]: #validationerrorprototypemessage @@ -193,15 +193,14 @@ Manages an [`Error`][js-error] of validation. [![update]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. By default, it's set to `Problem: [problem] => Fix: [fix]`. The value is being checked against the existence of `[problem]` and `[fix]` words. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. The value is being checked against the existence of `[problem]` and `[fix]` tags. By default, it's set to `Problem: [problem] => Fix: [fix]`. ```typescript static get template(): string { - return ValidationError.#template; + return this.#template; } static set template(value: string) { - ValidationError.#guardTemplate(value) && - (ValidationError.#template = value); + this.#guardTemplate(value) && (this.#template = value); } ``` @@ -274,7 +273,7 @@ public set problem(value: string) { ![new] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` words. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of `[problem]` and `[fix]` words. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of `[problem]` and `[fix]` tags. ```typescript public get template(): string { @@ -293,7 +292,7 @@ public set template(value: string) { [![update]][error-github-changelog] -Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. +Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`template`](#validationerrortemplate). ```typescript // Syntax. @@ -301,10 +300,8 @@ public static defineMessage( message: ErrorMessage, callback?: ResultCallback ): string { - return ValidationError.#guardMessage(message, callback) - ? (message.template || ValidationError.template) - .replace(`[fix]`, message.fix) - .replace(`[problem]`, message.problem) + return this.#guardMessage(message, callback) + ? this.#buildMessage(message) : ''; } ``` @@ -314,7 +311,7 @@ public static defineMessage( | Name: type | Description | | :-------------------------- | :---------- | | `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided message is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` words. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided message is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | **Returns:** @@ -619,7 +616,7 @@ public setMessage( | Name: type | Description | | :----------------------------------------- | :---------- | | `message: string \| ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -| `callback: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` words. By default, it uses an internal callback under the `'setMessage'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| `callback: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. By default, it uses an internal callback under the `'setMessage'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** @@ -756,8 +753,8 @@ public setTemplate( | Name: type | Description | | :--------------------------------- | :---------- | -| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable `[problem]` and `[fix]` words. | -| `callback: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` words. By default, it uses an internal callback under the `'setTemplate'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable `[problem]` and `[fix]` tags. | +| `callback: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` tags. By default, it uses an internal callback under the `'setTemplate'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | **Returns:** From fff1efb898b93bc4928b6deb5d05d9694f406eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 22:09:46 +0200 Subject: [PATCH 018/179] docs(README.md): update. --- README.md | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7e0b5cf..6fa6e86 100644 --- a/README.md +++ b/README.md @@ -1099,7 +1099,6 @@ type VEAllowedCallback = 'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'; ![experimental] [![update]][error-github-changelog] - ### Message builder #### `MessageBuilder` @@ -1116,9 +1115,9 @@ import { MessageBuilder } from '@angular-package/error'; const messageFunctionBuilder = new MessageBuilder('function'); messageFunctionBuilder - .setFunctionName('guardString') - .setParam('value', 'string') - .setReturn('boolean'); + .replaceFunctionName('guardString') + .replaceParam('value', 'string') + .replaceReturn('boolean'); // Console returns `guardString(value: string): boolean` console.log(messageFunctionBuilder.get); @@ -1133,11 +1132,11 @@ import { MessageBuilder } from '@angular-package/error'; */ const messageMethodBuilder = new MessageBuilder('method'); -// Build the method of any class. + // Build the method of any class. messageMethodBuilder - .setMethodName('setPerson') - .setParam('value', 'string') - .setReturn('this'); + .replaceMethodName('setPerson') + .replaceParam('value', 'string') + .replaceReturn('this'); // Console returns `setPerson(value: string): this` console.log(messageMethodBuilder.get); @@ -1152,12 +1151,12 @@ import { MessageBuilder } from '@angular-package/error'; */ const messageClassBuilder = new MessageBuilder('class'); -// Build the method of a specified class. + // Build the method of a specified class. messageClassBuilder - .setClassName('Person.prototype.') - .setMethodName('setPerson') - .setParam('value?', 'object') - .setReturn('object'); + .replaceClassName('Person.prototype.') + .replaceMethodName('setPerson') + .replaceParam('value?', 'object') + .replaceReturn('object'); // Console returns `Person.prototype.setPerson(value?: object): object` console.log(messageClassBuilder.get); @@ -1167,7 +1166,7 @@ console.log(messageClassBuilder.get); #### `MessageFunctionBuilder` -Message function builder for error message of a [`string`][js-string] type. +Message builder of function for the error message of a [`string`][js-string] type. ```typescript // Example usage of building a function. @@ -1181,8 +1180,7 @@ const messageFunctionBuilder = new MessageFunctionBuilder(); messageFunctionBuilder .setName('guardString') .setParam('value', 'string') - .setReturn('boolean') - .build(); + .setReturn('boolean'); // Console returns `guardString(value: string): boolean` console.log(messageFunctionBuilder.get); From 234f946a91efd80767ecd16f67e9e691e7d9fc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 22:12:42 +0200 Subject: [PATCH 019/179] docs(README.md): update. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6fa6e86..34624f9 100644 --- a/README.md +++ b/README.md @@ -1103,7 +1103,7 @@ type VEAllowedCallback = 'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'; #### `MessageBuilder` -Message builder for error message of a [`string`][js-string] type. +Message builder to build [`class`][js-classes], [`function`][js-function] and `method` of a [`string`][js-string] type. ```typescript // Example usage of building a function. @@ -1166,7 +1166,7 @@ console.log(messageClassBuilder.get); #### `MessageFunctionBuilder` -Message builder of function for the error message of a [`string`][js-string] type. +Message builder to build a [`function`][js-function] of a [`string`][js-string] type. ```typescript // Example usage of building a function. From 9d75e3761a44f2533fe9f60462355888137ceccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 22:15:08 +0200 Subject: [PATCH 020/179] docs(README.md): fix. --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 34624f9..ec388c6 100644 --- a/README.md +++ b/README.md @@ -1182,7 +1182,13 @@ messageFunctionBuilder .setParam('value', 'string') .setReturn('boolean'); -// Console returns `guardString(value: string): boolean` +// Console returns [function]([param.name][param.type])[return] +console.log(messageFunctionBuilder.get); + +// Build. +messageFunctionBuilder.build(); + +// Console returns guardString(value: string): boolean console.log(messageFunctionBuilder.get); ``` From 58c2010cc5fd9a9c17f04d128bb94120444680c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 22:16:09 +0200 Subject: [PATCH 021/179] refactor(MessageBuilderTemplate): instead of `is` object use `isString()` function, use short if as expression. --- src/message-builder/src/message-builder-template.class.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/message-builder/src/message-builder-template.class.ts b/src/message-builder/src/message-builder-template.class.ts index 3ea7a3d..9d46858 100644 --- a/src/message-builder/src/message-builder-template.class.ts +++ b/src/message-builder/src/message-builder-template.class.ts @@ -1,9 +1,9 @@ // @angular-package/type -import { is } from '@angular-package/type'; +import { isString } from '@angular-package/type'; // Interface. import { MessageTemplate } from '../interface/message-template.interface'; /** - * MessageBuilderTemplate. + * `MessageBuilderTemplate`. */ export class MessageBuilderTemplate { // argument value is [value.type] type, must be [param.type] type @@ -24,8 +24,6 @@ export class MessageBuilderTemplate { } constructor(type: 'class' | 'function' | 'method') { - if (is.string(type)) { - this.#type = type; - } + isString(type) && (this.#type = type); } } From 997e0e42b398431b101578f7dea9bb822e284fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 23:36:52 +0200 Subject: [PATCH 022/179] refactor(MessageBuilder): instead of `is` object use functions, use short if as expression, change `set` prefix to `replace` of methods, update jsdoc. Still experimental. --- .../src/message-builder.class.ts | 111 +++++++++++++----- 1 file changed, 80 insertions(+), 31 deletions(-) diff --git a/src/message-builder/src/message-builder.class.ts b/src/message-builder/src/message-builder.class.ts index 0facfdb..4b7c471 100644 --- a/src/message-builder/src/message-builder.class.ts +++ b/src/message-builder/src/message-builder.class.ts @@ -1,12 +1,20 @@ // @angular-package/type -import { is, guard, ResultCallback } from '@angular-package/type'; +import { + ResultCallback, + guardString, + isDefined, + isString, +} from '@angular-package/type'; // Class. import { MessageBuilderTemplate } from './message-builder-template.class'; /** - * Message builder for error message of a string type. - * @version Experimental This `object` is an experimental version of the message builder. + * Message builder to build `class`, `function` and `method` of a `string` type. + * @version experimental */ export class MessageBuilder { + /** + * An object with regexp to replace. + */ #regExp = { class: /\[class\]/i, function: /\[function\]/i, @@ -15,46 +23,75 @@ export class MessageBuilder { name: /\[param.name\]/i, type: /\[param.type\]/i, }, - return: /\[return\]/i + return: /\[return\]/i, }; + /** + * + */ #template: string; + /** + * + */ get get(): string { return this.#template; } + /** + * + * @param template + */ constructor(template: 'class' | 'function' | 'method') { this.#template = new MessageBuilderTemplate(template).get; } - public setClassName(name: string, callback?: ResultCallback): this { - if (guard.string(name, callback)) { - this.replace(this.#regExp.class, name); - } + /** + * + * @param name + * @param callback + * @returns + */ + public replaceClassName(name: string, callback?: ResultCallback): this { + guardString(name, callback) && this.replace(this.#regExp.class, name); return this; } - public setFunctionName(name: string, callback?: ResultCallback): this { - if (guard.string(name, callback)) { - this.replace(this.#regExp.function, name); - } + /** + * + * @param name + * @param callback + * @returns + */ + public replaceFunctionName(name: string, callback?: ResultCallback): this { + guardString(name, callback) && this.replace(this.#regExp.function, name); return this; } - public setMethodName(name: string, callback?: ResultCallback): this { - if (guard.string(name, callback)) { - this.replace(this.#regExp.method, name); - } + /** + * + * @param name + * @param callback + * @returns + */ + public replaceMethodName(name: string, callback?: ResultCallback): this { + guardString(name, callback) && this.replace(this.#regExp.method, name); return this; } - public setParam(name: string, type: string = ''): this { - if (guard.string(name)) { + /** + * + * @param name + * @param type + * @returns + */ + public replaceParam(name: string, type: string = ''): this { + if (guardString(name)) { const param = `${name}${type}`; - this - .replace(this.#regExp.param.name, name) - .replace(this.#regExp.param.type, type); + this.replace(this.#regExp.param.name, name).replace( + this.#regExp.param.type, + type + ); if (type.length > 0) { this.replace(type, `: ${type}`); @@ -63,20 +100,32 @@ export class MessageBuilder { return this; } - public setReturn(returns: string, callback?: ResultCallback): this { - if (guard.string(returns, callback)) { - this.replace(this.#regExp.return, returns.length > 0 ? `: ${returns}` : returns); - } + /** + * + * @param returns + * @param callback + * @returns + */ + public replaceReturn(returns: string, callback?: ResultCallback): this { + guardString(returns, callback) && + this.replace( + this.#regExp.return, + returns.length > 0 ? `: ${returns}` : returns + ); return this; } + /** + * + * @param searchValue + * @param replaceValue + * @returns + */ private replace(searchValue: string | RegExp, replaceValue: string): this { - if (is.defined(searchValue)) { - this.#template = this.#template.replace( - searchValue, - is.string(replaceValue) ? replaceValue : '' - ); - } + isDefined(searchValue) && (this.#template = this.#template.replace( + searchValue, + isString(replaceValue) ? replaceValue : '' + )); return this; } } From 25ea72b733939264b6546d01ef2945cc1e716fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 23:38:14 +0200 Subject: [PATCH 023/179] refactor(MessageFunctionBuilder): instead of `is`, `guard` objects use functions, use short if as expression, change `set` prefix to `replace` of `#messageBuilder` property, update jsdoc. Still experimental. --- .../src/message-function-builder.class.ts | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/message-builder/src/message-function-builder.class.ts b/src/message-builder/src/message-function-builder.class.ts index 2afad1a..68aaac7 100644 --- a/src/message-builder/src/message-function-builder.class.ts +++ b/src/message-builder/src/message-function-builder.class.ts @@ -1,18 +1,18 @@ -import { guard, is, ResultCallback } from '@angular-package/type'; +import { guardString, isString, ResultCallback } from '@angular-package/type'; // Class. import { MessageBuilder } from './message-builder.class'; // Interface. import { Parameter } from '../interface/parameter.interface'; /** - * Message function builder for error message of a string type. - * @version Experimental This `object` is an experimental version of the message function builder that is using `MessageBuilder`. + * Message builder to build a `function` of a `string` type. + * @version experimental */ export class MessageFunctionBuilder { #messageBuilder: MessageBuilder; #name = ''; #param: Parameter = { name: '', - type: '' + type: '', }; #return = ''; @@ -23,6 +23,18 @@ export class MessageFunctionBuilder { return this.#messageBuilder.get; } + get param(): string { + return this.#param.name; + } + + get name(): string { + return this.#name; + } + + get return(): string { + return this.#return; + } + /** * Creates an instance of `MessageFunctionBuilder`. */ @@ -34,11 +46,10 @@ export class MessageFunctionBuilder { * Builds string-type function from the privately stored `name`, `param`, and `return`. */ public build(): this { - this - .#messageBuilder - .setFunctionName(this.#name) - .setParam(this.#param.name, this.#param.type) - .setReturn(this.#return); + this.#messageBuilder + .replaceFunctionName(this.#name) + .replaceParam(this.#param.name, this.#param.type) + .replaceReturn(this.#return); return this; } @@ -49,10 +60,8 @@ export class MessageFunctionBuilder { * is a `string` type. * @returns The return value is an instance of `MessageFunctionBuilder`. */ - public setName(name: string, callback?: ResultCallback): this { - if (guard.string(name, callback)) { - this.#name = name; - } + public setName(name: string, callback?: ResultCallback): this { + guardString(name, callback) && (this.#name = name); return this; } @@ -64,13 +73,13 @@ export class MessageFunctionBuilder { * is a `string` type. * @returns The return value is an instance of `MessageFunctionBuilder`. */ - public setParam(name: string, type?: string, callback?: ResultCallback): this { - if (guard.string(name, callback)) { - this.#param.name = name; - if (is.string(type)) { - this.#param.type = type; - } - } + public setParam( + name: string, + type?: string, + callback?: ResultCallback + ): this { + guardString(name, callback) && + ((this.#param.name = name), isString(type) && (this.#param.type = type)); return this; } @@ -81,10 +90,8 @@ export class MessageFunctionBuilder { * is a `string` type. * @returns The return value is an instance of `MessageFunctionBuilder`. */ - public setReturn(returns: string, callback?: ResultCallback): this { - if (guard.string(returns, callback)) { - this.#return = returns; - } + public setReturn(returns: string, callback?: ResultCallback): this { + guardString(returns, callback) && (this.#return = returns); return this; } } From bec2b5b96650998b1c0e6dd7437098461603d1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 23:42:20 +0200 Subject: [PATCH 024/179] test(MessageBuilder): change `set` prefix to `replace` of `MessageBuilder`, change the primary describe description, remove `console.log()`. --- .../test/message-builder.spec.ts | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/message-builder/test/message-builder.spec.ts b/src/message-builder/test/message-builder.spec.ts index a643d7d..4be39d8 100644 --- a/src/message-builder/test/message-builder.spec.ts +++ b/src/message-builder/test/message-builder.spec.ts @@ -10,7 +10,7 @@ const toBe = new TestingToBeMatchers(); /** * Tests. */ -testing.describe(MessageBuilder.name, () => { +testing.describe(`MessageBuilder`, () => { let messageClassBuilder = new MessageBuilder('class'); let messageFunctionBuilder = new MessageBuilder('function'); let messageMethodBuilder = new MessageBuilder('method'); @@ -35,21 +35,20 @@ testing.describe(MessageBuilder.name, () => { testing.it(`build function`, () => { messageFunctionBuilder - .setFunctionName('guardString') - .setParam('value', 'string') - .setReturn('boolean'); + .replaceFunctionName('guardString') + .replaceParam('value', 'string') + .replaceReturn('boolean'); toBe.string(messageFunctionBuilder.get); - console.log(messageFunctionBuilder.get); expect(messageFunctionBuilder.get).toEqual('guardString(value: string): boolean'); }); testing.it(`build class`, () => { messageClassBuilder - .setClassName('Person.prototype.') - .setMethodName('setPerson') - .setParam('value?', 'object') - .setReturn('object'); + .replaceClassName('Person.prototype.') + .replaceMethodName('setPerson') + .replaceParam('value?', 'object') + .replaceReturn('object'); toBe.string(messageClassBuilder.get); expect(messageClassBuilder.get).toEqual('Person.prototype.setPerson(value?: object): object'); @@ -57,11 +56,10 @@ testing.describe(MessageBuilder.name, () => { testing.it(`build method`, () => { messageMethodBuilder - .setMethodName('setPerson') - .setParam('value', 'string') - .setReturn('this'); + .replaceMethodName('setPerson') + .replaceParam('value', 'string') + .replaceReturn('this'); - console.log(messageMethodBuilder.get); toBe.string(messageMethodBuilder.get); expect(messageMethodBuilder.get).toEqual('setPerson(value: string): this'); }); From 4ce6278b24280fde42b534ac06ccfdefb7aa2833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 23:42:57 +0200 Subject: [PATCH 025/179] test(MessageFunctionBuilder): change the primary describe description. --- src/message-builder/test/message-function-builder.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/message-builder/test/message-function-builder.spec.ts b/src/message-builder/test/message-function-builder.spec.ts index 74c2222..6e0c2db 100644 --- a/src/message-builder/test/message-function-builder.spec.ts +++ b/src/message-builder/test/message-function-builder.spec.ts @@ -10,7 +10,7 @@ const toBe = new TestingToBeMatchers(); /** * Tests. */ -testing.describe(MessageFunctionBuilder.name, () => { +testing.describe(`MessageFunctionBuilder`, () => { let messageFunctionBuilder = new MessageFunctionBuilder(); beforeEach(() => (messageFunctionBuilder = new MessageFunctionBuilder())); From 1c3f12de04e0846db11aaf185dd19d1498ce95a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 2 Oct 2021 23:44:02 +0200 Subject: [PATCH 026/179] test(ValidationError): add all known possibilities --- src/test/validation-error.spec.ts | 194 ++++++++++++++++++++++++++---- 1 file changed, 170 insertions(+), 24 deletions(-) diff --git a/src/test/validation-error.spec.ts b/src/test/validation-error.spec.ts index 6edc5f9..1876016 100644 --- a/src/test/validation-error.spec.ts +++ b/src/test/validation-error.spec.ts @@ -11,9 +11,19 @@ const toBe = new TestingToBeMatchers(); * Tests. */ testing.describe('ValidationError', () => { + let fix: any; + let problem: any; + let template: any; + let errorMessage: any; + let validationError: ValidationError; + + beforeEach(() => { + fix = 'There is no solution to the described problem.'; + problem = 'The problem has no solution.'; + validationError = new ValidationError(); + }); + testing.describe('throw', () => { - const problem = 'My callback problem'; - const fix = 'Possible fix does not exist'; try { throw new ValidationError({ problem, fix }); } catch (e: any) { @@ -27,18 +37,10 @@ testing.describe('ValidationError', () => { ) .toBeStringType(e.fix, undefined, '`e.fix` must be of a `string` type'); } - }); + }) - testing.describe('instantiate', () => { - let fix: any; - let problem: any; - let validationError: any; - - beforeEach(() => { - fix = 'There is no solution to the described problem.'; - problem = 'The problem has no solution.'; - validationError = new ValidationError({ fix, problem }); - }); + .describe('instantiate', () => { + beforeEach(() => validationError = new ValidationError({ fix, problem })); testing.it(`with the message of a \`string\` type`, () => { const message = 'Validation error message'; @@ -65,18 +67,10 @@ testing.describe('ValidationError', () => { expect(validationError.message).toContain(fix); expect(validationError.message).toContain(problem); }); - }); + }) - testing.describe('static defineMessage()', () => { - let fix: any; - let problem: any; - let errorMessage: any; - - beforeEach(() => { - fix = 'There is no solution to the described problem.'; - problem = 'The problem has no solution.'; - errorMessage = ValidationError.defineMessage({ fix, problem }); - }); + .describe('.defineMessage()', () => { + beforeEach(() => errorMessage = ValidationError.defineMessage({ fix, problem })); testing.it(`with the message of the \`ErrorMessage\` interface`, () => { toBe.string(errorMessage); @@ -89,5 +83,157 @@ testing.describe('ValidationError', () => { expect(errorMessage).toContain(fix); expect(errorMessage).toContain(problem); }); + }) + + .describe('.prototype.setFix()', () => { + testing.it(`set ${fix}`, () => { + validationError.setFix(fix, (result, value, payload) => { + expect(value).toEqual(fix); + toBe + .boolean(result) + .string(value) + .undefined(payload); + return result; + }); + expect(validationError.fix).toEqual(fix); + }); + }) + + .describe('.prototype.setMessage()', () => { + testing.it(`set ${problem}`, () => { + validationError.setMessage(problem, (result, value, payload) => { + expect(value).toEqual(problem); + toBe + .boolean(result) + .string(value) + .undefined(payload); + return result; + }); + expect(validationError.message).toEqual(problem); + }); + }) + + .describe('[counter] .prototype.setMessage()', () => { + testing.it(`[counter] set ${problem}`, () => { + validationError.setMessage(problem, (result, value, payload) => { + expect(value).toEqual(problem); + toBe + .boolean(result) + .string(value) + .undefined(payload); + return result; + }); + expect(validationError.message).toEqual(problem); + }) + .it(`[counter] set object`, () => { + errorMessage = { fix, problem }; + validationError.setMessage(errorMessage, (result, value, payload) => { + expect((value as any).fix).toEqual(errorMessage.fix); + expect((value as any).problem).toEqual(errorMessage.problem); + toBe + .boolean(result) + .object(value) + .undefined(payload); + return result; + }); + expect(validationError.fix).toEqual(fix); + expect(validationError.problem).toEqual(problem); + }); + }) + + .describe('[counter] .prototype.setProblem()', () => { + testing.it(`[counter] set ${problem}`, () => { + validationError.setProblem(problem, (result, value, payload) => { + expect(value).toEqual(problem); + toBe + .boolean(result) + .string(value) + .undefined(payload); + return result; + }); + expect(validationError.problem).toEqual(problem); + }); + }) + + .describe('[counter] .prototype.setTemplate()', () => { + template = `[problem] must be fixed by using [fix]`; + testing.it(`[counter] set ${template}`, () => { + validationError.setTemplate(template, (result, value, payload) => { + expect(value).toEqual(template); + toBe + .boolean(result) + .string(value) + .object(payload); + return result; + }); + expect(validationError.template).toEqual(template); + }); + }) + + .describe('[counter] .prototype.updateMessage()', () => { + testing.it(`[counter] works properly`, () => { + validationError + .setFix(fix) + .setProblem(problem); + expect(validationError.fix).toEqual(fix); + expect(validationError.problem).toEqual(problem); + expect(validationError.message).toEqual(''); + validationError.updateMessage(); + expect(validationError.message) + .toEqual('Problem: The problem has no solution. => Fix: There is no solution to the described problem.'); + }); + }) + + .describe('[counter] .prototype.throw()', () => { + testing + .it(`[counter] with string`, () => { + try { + validationError.setFix(fix).setProblem(problem); + validationError.throw('Throws a string'); + } catch (e) { + expect(e.problem).toEqual(''); + expect(e.fix).toEqual(''); + toBe + .stringOfLength(e.problem, { max: 0 }) + .stringOfLength(e.fix, { max: 0 }); + } + }) + .it(`[counter] with object of ErrorMessage`, () => { + try { + validationError.setFix(fix).setProblem(problem).setTemplate(`[problem], [fix]`); + validationError.throw({ + problem: 'new problem', + fix: 'new fix' + }); + } catch (e) { + expect(e.message).toEqual(`new problem, new fix`); + expect(e.problem).toEqual('new problem'); + expect(e.fix).toEqual('new fix'); + } + }) + .it(`[counter] with set string type message`, () => { + try { + validationError.setMessage(problem); + validationError.throw(); + } catch (e) { + expect(e.message).toEqual(problem); + expect(e.problem).toEqual(''); + expect(e.fix).toEqual(''); + toBe + .stringOfLength(e.problem, { max: 0 }) + .stringOfLength(e.fix, { max: 0 }); + } + }) + .it(`[counter] with the actual \`problem\` and \`fix\``, () => { + try { + validationError.setMessage('my message'); + validationError.setProblem(problem).setFix(fix).setTemplate(`[problem], [fix]`); + validationError.throw(); + } catch (e) { + expect(e.message).toEqual(`${problem}, ${fix}`); + expect(e.problem).toEqual(problem); + expect(e.fix).toEqual(fix); + } + }); }); }); From 285ef51805a3f1528b62d389214c106c3f213dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 00:06:12 +0200 Subject: [PATCH 027/179] refactor(ValidationError): general 3.0 update. Add: [x] Add `template` property of an instance. [x] Add private property `#tags` of array to replace the template `[problem]` and `[fix]` tags. [x] New property `template` of an instance that refers to private property `#tpl`. [x] Add more examples. Update: [x] Use short if expression. [x] Use `this` for static instead of `ValidationError`. [x] Use `ResultCallback` with generic type variable `Value`. [x] Instead of `is` and `guard` objects use functions of `@angular-package/type`. [x] The `setMessage()` method sets the `problem` and `fix` properties to empty string if the provided message is a `string` type. [x] The `setMessage()` method to set the message uses as the first, template from the given `message` parameter, and if it's empty the private property `#tpl`. [x] The `defineMessage()` static method uses private property `#tags` to replace them in the template. --- src/lib/validation-error.class.ts | 289 +++++++++++++++++------------- 1 file changed, 168 insertions(+), 121 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 192a368..238e0f6 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,29 +1,37 @@ // @angular-package/type. -import { is, guard } from '@angular-package/type'; - -// @angular-package/callback. import { - Callback, - CallbackPayload, ResultCallback, -} from '@angular-package/callback'; - + areString, + guardObjectKeys, + guardString, + guardStringIncludes, + guardStringLength, + isDefined, + isFunction, + isObject, + isString, +} from '@angular-package/type'; +// @angular-package/callback. +import { Callback } from '@angular-package/callback'; // Interface. import { ErrorMessage } from '../interface/error-message.interface'; - // Type. import { VEAllowedCallback } from '../type/allowed-callback.type'; - /** * Manages an `Error` of validation. */ export class ValidationError extends Error { - /* #region static private properties */ + //#region static private properties + /** + * Tags to be replaced. + */ + static #tags = ['fix', 'problem']; + /** * A static, privately stored template of the error message. */ static #template = `Problem: [problem] => Fix: [fix]`; - /* #endregion static private properties */ + //#endregion static private properties //#region instance private properties. /** @@ -32,146 +40,186 @@ export class ValidationError extends Error { #callback = new Callback('setFix', 'setMessage', 'setProblem', 'setTemplate'); /** - * A privately stored possible solution to the described problem of a `string` type. - * By default, it's an empty `string`. + * A privately stored possible solution to the described problem of `string` type. + * * By default, it's an empty `string`. */ #fix = ''; /** - * A privately stored validation problem of a `string` type. - * By default, it's an empty `string`. + * A privately stored validation problem of `string` type. + * * By default, it's an empty `string`. */ #problem = ''; /** - * A string-type privately stored template of the error message that contains replaceable `[fix]` and `[problem]` words. + * A string-type privately stored template of the error message that contains replaceable `[fix]` and `[problem]` tags. + * * By default, it's set to `Problem: [problem] => Fix: [fix]`. */ #tpl = ValidationError.template; //#endregion instance private properties. + //#region static public properties /** - * A template of the error message guarded by a `string` type with the replaceable `[problem]` and `[fix]` words. - * By default, it's set to `Problem: [problem] => Fix: [fix]`. It can be set directly or by the `setTemplate()` and `setMessage()` method. - * The value is being checked against the existence of `[problem]` and `[fix]` words. + * A template of the error message guarded by `string` type with the replaceable `[problem]` and `[fix]` tags. + * * By default, it's set to `Problem: [problem] => Fix: [fix]`. + * * The value is being checked against the existence of `[problem]` and `[fix]` tags. */ static get template(): string { - return ValidationError.#template; + return this.#template; } static set template(value: string) { - ValidationError.#template = ValidationError.#guardTemplate(value) - ? value - : ValidationError.#template; + this.#guardTemplate(value) && (this.#template = value); } + //#endregion static public properties //#region instance public properties. /** - * A possible solution to the described `problem` of validation that is guarded by a `string` type. - * By default, it's an empty `string`. It can be set directly or by the `setTemplate()` and `setMessage()` method + * A possible solution to the described `problem` of validation that is guarded by `string` type. + * * It can be set directly or by the `setFix()` and `setMessage()` method. + * * By default, it's an empty `string`. */ public get fix(): string { return this.#fix; } public set fix(value: string) { - this.#fix = guard.string(value) ? value : this.#fix; + guardString(value) && (this.#fix = value); } /** - * A validation error message guarded by a `string` type that can be built from the `problem` and `fix` of `ValidationError` on the - * `template`. It can be set directly or by the `throw()` or `setMessage()` method. + * A validation error message guarded by `string` type that can be built from the `problem` and `fix` on the `template` + * of an instance of `ValidationError`. + * * It can be set directly or by the `throw()` and `setMessage()` method. */ - public set message(value: string) { - super.message = guard.string(value) ? value : super.message; - } public get message(): string { return super.message; } + public set message(value: string) { + guardString(value) && (super.message = value); + } /** - * Error name of a `string` type that is being thrown. By default, it's `ValidationError`. + * Error name of `string` type that is being thrown. + * * By default, it's `ValidationError`. */ public name = ValidationError.name; /** - * Description of a validation problem guarded by a `string` type. By default, it's an empty `string`. - * It can be set directly or by the `setProblem()` and `setMessage()` method. + * Description of the validation problem guarded by `string` type. + * * It can be set directly or by the `setProblem()` and `setMessage()` method. + * * By default, it's an empty `string`. */ public get problem(): string { return this.#problem; } public set problem(value: string) { - this.#problem = guard.string(value) ? value : this.#problem; + guardString(value) && (this.#problem = value); + } + + /** + * A template of the error message guarded by `string` type with the replaceable `[problem]` and `[fix]` tags. The value is being + * checked against the existence of `[problem]` and `[fix]` tags. + * * It can be set directly or by the `setTemplate()` and `setMessage()` method. + * * By default, it's set to `Problem: [problem] => Fix: [fix]`. + */ + public get template(): string { + return this.#tpl; + } + public set template(value: string) { + // TODO: Add error on [problem] and [fix] missing. + ValidationError.#guardTemplate(value) && (this.#tpl = value); } //#endregion instance public properties. //#region static public methods /** - * Defines the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface. - * @param message An object of an `ErrorMessage` interface to build a message of a `string` type. The value is checked against - * the proper `object`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains - * required `problem` and `fix` properties. - * @returns The return value is a message of a `string` type created from the provided `message` of `ErrorMessage` interface or it's an + * Defines the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface. The message is built + * on the provided `template` or the template from the static property `template`. + * @param message An `object` of an `ErrorMessage` interface to build a message of `string` type. The value is checked against the proper + * `object` of `ErrorMessage`. + * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message is the proper + * `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the optional `template` property has + * `[problem]` and `[fix]` tags. + * @returns The return value is a message of `string` type created from the provided `message` of the `ErrorMessage` interface or it's an * empty `string` if the provided message object isn't proper. * @angularpackage */ public static defineMessage( message: ErrorMessage, - callback?: ResultCallback + callback?: ResultCallback ): string { - return ValidationError.#guardMessage(message, callback) - ? (message.template || ValidationError.template) - .replace(`[fix]`, message.fix) - .replace(`[problem]`, message.problem) + return this.#guardMessage(message, callback) + ? this.#buildMessage(message) : ''; } //#endregion static public methods - //#region private static methods. + //#region static private methods. + /** + * Builds the validation error message of `string` type based on the provided `message` of the `ErrorMessage` interface. + * @param message An `object` of an `ErrorMessage` interface to build a message of `string` type. The value is not checked against the + * proper `object`. + * @returns The return value is a message of `string` type created from the provided `message` parameter. + * @angularpackage + */ + static #buildMessage(message: ErrorMessage): string { + let m = message.template || this.template; + return ( + this.#tags.forEach( + (tag) => + (m = m.replace( + `[${tag}]`, + message[tag as keyof Omit] + )) + ), + m + ); + } + /** * Guards the provided message to be of `ErrorMessage` shape. - * @param message An object of the `ErrorMessage` interface to build the message of a `string` type. The value is checked against - * the proper `object`. + * @param message An object of the `ErrorMessage` interface to build the message of `string` type. The value is checked against the proper + * `object`. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `message` contains - * required `problem` and `fix` properties. + * required `problem`, `fix` properties and the optional `template` has `[problem]` and `[fix]` tags. * @returns The return value is a `boolean` indicating whether the provided `message` is an object of `ErrorMessage`. * @angularpackage */ - static #guardMessage( + static #guardMessage( message: ErrorMessage, - callback?: ResultCallback + callback: ResultCallback = (result) => result ): message is ErrorMessage { - return guard.objectKey(message, ['fix', 'problem'], callback) - ? is.defined(message.template) - ? guard.string(message.problem) && - guard.string(message.fix) && - ValidationError.#guardTemplate(message.template, callback) - : guard.string(message.problem) && guard.string(message.fix) - : false; + return callback( + guardObjectKeys(message, ['fix', 'problem']) && + areString(message.fix, message.problem).every() + ? isDefined(message.template) + ? this.#guardTemplate(message.template) + : true + : false, + message + ); } /** - * Guards the provided `template` to be a `string` type that includes `[fix]` and `[problem]` words. + * Guards the provided `template` to be `string` type that includes `[fix]` and `[problem]` tags. * @param template A string-type value to guard. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message contains + * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message includes + * `[fix]` and `[problem]` tags. * @returns The return value is a `boolean` indicating whether the provided `template` is a `string` that includes `[fix]` and `[problem]` - * words. + * tags. * @angularpackage */ - static #guardTemplate( + static #guardTemplate( template: string, - callback?: ResultCallback + callback?: ResultCallback ): template is string { - return guard.string(template, callback) - ? template.includes('[fix]') && template.includes('[problem]') - : false; + return guardStringIncludes(template, ['[fix]', '[problem]'], callback); } - //#endregion private static methods. + //#endregion static private methods. /** - * Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned - * to the instance. - * @param message The message of a `string` type or of an `ErrorMessage` interface that is used to throw with an `Error`. - * @param callback An optional function to handle the internal instance of `Callback`. + * Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned to the instance. + * @param message The message of `string` type or of an `ErrorMessage` interface that is used to throw with an `Error`. + * @param callback An optional `function` to handle the internal instance of `Callback`. * @angularpackage */ constructor( @@ -181,9 +229,7 @@ export class ValidationError extends Error { super(); // Sets the callback for an instance methods. - if (is.function(callback)) { - callback(this.#callback); - } + isFunction(callback) && callback(this.#callback); // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. this.setMessage(message); @@ -191,8 +237,8 @@ export class ValidationError extends Error { //#region instance public methods. /** - * Sets the fix a possible solution to the described problem. - * @param fix A possible solution to the described problem guarded by a `string` type. + * Sets the `fix` a possible solution to the described `problem`. + * @param fix A possible solution to the described `problem` guarded by `string` type. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `fix` is a `string`. * By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter * that gives access to the internal instance of `Callback`. @@ -201,22 +247,21 @@ export class ValidationError extends Error { */ public setFix( fix: string, - callback: ResultCallback = this.#callback.getCallback( + callback: ResultCallback = this.#callback.getResultCallback( 'setFix' ) ): this { - if (guard.string(fix, callback)) { - this.#fix = fix; - } + guardString(fix, callback) && (this.#fix = fix); return this; } /** - * Sets the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface. - * @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against - * the proper `object`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message is a string - * type or whether it's an object that contains required `problem` and `fix` properties. + * Sets the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface or `string` type. + * @param message A `string` type or an `object` of an `ErrorMessage` interface to build the message of `string` type. The value is + * checked against the proper `object` of `ErrorMessage`. + * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether the provided `message` is `string` + * type or whether it's the proper `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the + * optional `template` has `[problem]` and `[fix]` tags. * By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter * that gives access to the internal instance of `Callback`. * @returns The return value is an instance of an `ValidationError`. @@ -224,29 +269,33 @@ export class ValidationError extends Error { */ public setMessage( message: string | ErrorMessage, - callback: ResultCallback< - CallbackPayload & ErrorMessage - > = this.#callback.getCallback('setMessage') + callback: ResultCallback = this.#callback.getResultCallback( + 'setMessage' + ) ): this { - this.message = is.string(message, callback) - ? // Sets a message of a string type from the provided message of `string`. - message - : // Sets a message of a string type from the provided message of `ErrorMessage`. - ValidationError.defineMessage(message, callback); + super.message = isString(message, callback) + ? message + : ValidationError.defineMessage( + { ...message, ...{ template: message.template || this.#tpl } }, + callback + ); // Sets `fix`, `problem` and `template` from the provided `message`. - if (is.object(message)) { + if (isObject(message)) { this.setFix(message.fix).setProblem(message.problem); - if (is.defined(message.template)) { + if (isDefined(message.template)) { this.setTemplate(message.template); } + } else { + this.#problem = ''; + this.#fix = ''; } return this; } /** - * Sets description problem of a validation. - * @param problem Description of validation problem guarded by a `string` type. + * Sets description of the validation problem. + * @param problem Description of validation problem guarded by `string` type. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `problem` is a * `string`. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` * parameter that gives access to the internal instance of `Callback`. @@ -255,60 +304,58 @@ export class ValidationError extends Error { */ public setProblem( problem: string, - callback: ResultCallback = this.#callback.getCallback( + callback: ResultCallback = this.#callback.getResultCallback( 'setProblem' ) ): this { - this.#problem = guard.string(problem, callback) ? problem : this.#problem; + guardString(problem, callback) && (this.#problem = problem); return this; } /** - * Sets the template of validation error message. - * @param template A message template guarded by a `string` type with replaceable `[problem]` and `[fix]` words. + * Sets the template of validation error `message`. + * @param template A `message` template guarded by `string` type with replaceable `[problem]` and `[fix]` tags. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a - * `string` that contains `[fix]` and `[problem]` words. By default, it uses an internal callback under the `'setTemplate'` name, which + * `string` that contains `[fix]` and `[problem]` tags. By default, it uses an internal callback under the `'setTemplate'` name, which * can be initially set by the optional `callback` parameter that gives access to the internal instance of `Callback`. * @angularpackage */ public setTemplate( template: string, - callback: ResultCallback = this.#callback.getCallback( + callback: ResultCallback = this.#callback.getResultCallback( 'setTemplate' ) ): this { - this.#tpl = ValidationError.#guardTemplate(template, callback) - ? template - : this.#tpl; + ValidationError.#guardTemplate(template, callback) && + (this.#tpl = template); return this; } /** * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem` and `template` or optionally from * the provided `message`. - * @param message An optional object of an `ErrorMessage` interface to build the message of a `string` type. - * The value is checked against the proper object. + * @param message An optional parameter of `string` type or an `object` of an `ErrorMessage` interface to build the message of `string` + * type. The value is checked against the proper `object` of `ErrorMessage`. * @angularpackage */ public throw(message?: string | ErrorMessage): void { - if (is.defined(message)) { - this.setMessage(message); - } else { - this.updateMessage(); - } + isDefined(message) ? this.setMessage(message) : this.updateMessage(); throw this; } /** - * Updates the message with a stored `fix`, `problem`, and `template`. + * Updates the message with stored `fix`, `problem`, and `template` properties. + * @returns The return value is an instance of an `ValidationError`. * @angularpackage */ - public updateMessage(): void { - this.message = ValidationError.defineMessage({ - fix: this.#fix, - problem: this.#problem, - template: this.#tpl, - }); + public updateMessage(): this { + guardStringLength(this.#problem, { min: 1 }) && + (super.message = ValidationError.defineMessage({ + fix: this.#fix, + problem: this.#problem, + template: this.#tpl, + })); + return this; } //#endregion instance public methods. } From 4af703afa7c2c432a902faad96fa535640bc5671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 15:01:58 +0200 Subject: [PATCH 028/179] refactor(AllowedCallback): remove. --- src/type/allowed-callback.type.ts | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/type/allowed-callback.type.ts diff --git a/src/type/allowed-callback.type.ts b/src/type/allowed-callback.type.ts deleted file mode 100644 index 9e8a189..0000000 --- a/src/type/allowed-callback.type.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Allowed callback function names available for the `ValidationError`. - */ -export type VEAllowedCallback = 'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'; From 96c5563285f86eb2ce5d1ecff39aa2110507f220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 15:02:48 +0200 Subject: [PATCH 029/179] refactor(ValidationError): remove the default usage of `callback`. --- src/lib/validation-error.class.ts | 52 +++++-------------------------- 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 238e0f6..8308ceb 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -7,16 +7,11 @@ import { guardStringIncludes, guardStringLength, isDefined, - isFunction, isObject, isString, } from '@angular-package/type'; -// @angular-package/callback. -import { Callback } from '@angular-package/callback'; // Interface. import { ErrorMessage } from '../interface/error-message.interface'; -// Type. -import { VEAllowedCallback } from '../type/allowed-callback.type'; /** * Manages an `Error` of validation. */ @@ -34,11 +29,6 @@ export class ValidationError extends Error { //#endregion static private properties //#region instance private properties. - /** - * An instance of `Callback` with specified allowed names of callback functions for the `ValidationError`. - */ - #callback = new Callback('setFix', 'setMessage', 'setProblem', 'setTemplate'); - /** * A privately stored possible solution to the described problem of `string` type. * * By default, it's an empty `string`. @@ -125,7 +115,6 @@ export class ValidationError extends Error { return this.#tpl; } public set template(value: string) { - // TODO: Add error on [problem] and [fix] missing. ValidationError.#guardTemplate(value) && (this.#tpl = value); } //#endregion instance public properties. @@ -219,18 +208,11 @@ export class ValidationError extends Error { /** * Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned to the instance. * @param message The message of `string` type or of an `ErrorMessage` interface that is used to throw with an `Error`. - * @param callback An optional `function` to handle the internal instance of `Callback`. * @angularpackage */ - constructor( - message: string | ErrorMessage = '', - callback?: (callback: Callback) => void - ) { + constructor(message: string | ErrorMessage = '') { super(); - // Sets the callback for an instance methods. - isFunction(callback) && callback(this.#callback); - // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. this.setMessage(message); } @@ -240,17 +222,10 @@ export class ValidationError extends Error { * Sets the `fix` a possible solution to the described `problem`. * @param fix A possible solution to the described `problem` guarded by `string` type. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `fix` is a `string`. - * By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter - * that gives access to the internal instance of `Callback`. * @returns The return value is an instance of an `ValidationError`. * @angularpackage */ - public setFix( - fix: string, - callback: ResultCallback = this.#callback.getResultCallback( - 'setFix' - ) - ): this { + public setFix(fix: string, callback?: ResultCallback): this { guardString(fix, callback) && (this.#fix = fix); return this; } @@ -262,16 +237,12 @@ export class ValidationError extends Error { * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether the provided `message` is `string` * type or whether it's the proper `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the * optional `template` has `[problem]` and `[fix]` tags. - * By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter - * that gives access to the internal instance of `Callback`. * @returns The return value is an instance of an `ValidationError`. * @angularpackage */ public setMessage( message: string | ErrorMessage, - callback: ResultCallback = this.#callback.getResultCallback( - 'setMessage' - ) + callback?: ResultCallback ): this { super.message = isString(message, callback) ? message @@ -297,17 +268,11 @@ export class ValidationError extends Error { * Sets description of the validation problem. * @param problem Description of validation problem guarded by `string` type. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `problem` is a - * `string`. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` - * parameter that gives access to the internal instance of `Callback`. + * `string`. * @returns The return value is an instance of an `ValidationError`. * @angularpackage */ - public setProblem( - problem: string, - callback: ResultCallback = this.#callback.getResultCallback( - 'setProblem' - ) - ): this { + public setProblem(problem: string, callback?: ResultCallback): this { guardString(problem, callback) && (this.#problem = problem); return this; } @@ -316,15 +281,12 @@ export class ValidationError extends Error { * Sets the template of validation error `message`. * @param template A `message` template guarded by `string` type with replaceable `[problem]` and `[fix]` tags. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a - * `string` that contains `[fix]` and `[problem]` tags. By default, it uses an internal callback under the `'setTemplate'` name, which - * can be initially set by the optional `callback` parameter that gives access to the internal instance of `Callback`. + * `string` that contains `[fix]` and `[problem]` tags. * @angularpackage */ public setTemplate( template: string, - callback: ResultCallback = this.#callback.getResultCallback( - 'setTemplate' - ) + callback?: ResultCallback ): this { ValidationError.#guardTemplate(template, callback) && (this.#tpl = template); From 8a0f58dae2b8a474848a2eccbe3218a0da0c017e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 15:03:18 +0200 Subject: [PATCH 030/179] chore(tsconfig): lib change to es2020 cause of bigint --- tsconfig.lib.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.lib.json b/tsconfig.lib.json index 1407202..3e0398f 100644 --- a/tsconfig.lib.json +++ b/tsconfig.lib.json @@ -10,7 +10,7 @@ "types": [], "lib": [ "dom", - "es2018" + "es2020" ] }, "exclude": [ From 7ed2b5c90ed9922b8ced3622db4e0aa9b51afc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 15:04:05 +0200 Subject: [PATCH 031/179] docs(README.md): remove the default usage of callback. --- README.md | 76 ++++++++++++++++--------------------------------------- 1 file changed, 22 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index ec388c6..26dc817 100644 --- a/README.md +++ b/README.md @@ -410,15 +410,9 @@ Creates a new instance with the [`message`][error-property-message]. If the prov ```typescript // Syntax. -constructor( - message: string | ErrorMessage = '', - callback?: (callback: Callback) => void -) { +constructor(message: string | ErrorMessage = '') { super(); - // Sets the callback for an instance methods. - isFunction(callback) && callback(this.#callback); - // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. this.setMessage(message); } @@ -426,10 +420,9 @@ constructor( **Parameters:** -| Name: type | Description | -| :----------------------------------------------------------- | :---------- | -| `message: string \| ErrorMessage` | The message of [`string`][js-string] type or of an [`ErrorMessage`](#errormessage) interface that is used to throw with an [`Error`][js-error]. | -| `callback?: (callback: Callback) => void` | An optional [`function`][js-function] to handle the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :-------------------------------- | :---------- | +| `message: string \| ErrorMessage` | The message of [`string`][js-string] type or of an [`ErrorMessage`](#errormessage) interface that is used to throw with an [`Error`][js-error]. | **Returns:** @@ -461,6 +454,7 @@ const problem = 'The problem has no solution.'; const template = 'PROBLEM: [problem] FIX: [fix]'; // Initialize an instance. +TODO: Example with callback. const validationError = new ValidationError( { fix, problem, template }, (callback) => { @@ -514,12 +508,7 @@ Sets the [`fix`][error-property-fix] a possible solution to the described [`prob ```typescript // Syntax. -public setFix( - fix: string, - callback: ResultCallback = this.#callback.getResultCallback( - 'setFix' - ) -): this { +public setFix(fix: string, callback?: ResultCallback): this { guardString(fix, callback) && (this.#fix = fix); return this; } @@ -527,10 +516,10 @@ public setFix( **Parameters:** -| Name: type | Description | -| :--------------------------------- | :---------- | -| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type. | -| `callback: ResultCallback` | 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]. By default, it uses an internal callback under the `'setFix'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :---------------------------------- | :---------- | +| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| `callback?: ResultCallback` | 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]. | **Returns:** @@ -586,9 +575,7 @@ Sets the validation [`error`][js-error] [`message`][error-property-message] of [ // Syntax. public setMessage( message: string | ErrorMessage, - callback: ResultCallback = this.#callback.getResultCallback( - 'setMessage' - ) + callback?: ResultCallback ): this { super.message = isString(message, callback) ? message @@ -613,10 +600,10 @@ public setMessage( **Parameters:** -| Name: type | Description | -| :----------------------------------------- | :---------- | -| `message: string \| ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -| `callback: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. By default, it uses an internal callback under the `'setMessage'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :------------------------------------------ | :---------- | +| `message: string \| ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | **Returns:** @@ -670,12 +657,7 @@ console.log(validationError.message); Sets description of the validation [`problem`][error-property-problem]. ```typescript -public setProblem( - problem: string, - callback: ResultCallback = this.#callback.getResultCallback( - 'setProblem' - ) -): this { +public setProblem(problem: string, callback?: ResultCallback): this { guardString(problem, callback) && (this.#problem = problem); return this; } @@ -686,7 +668,7 @@ public setProblem( | Name: type | Description | | :---------------------------------- | :---------- | | `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. By default, it uses an internal callback under the `'setProblem'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. | **Returns:** @@ -739,9 +721,7 @@ Sets the [`template`][error-property-template] of validation error [`message`][e ```typescript public setTemplate( template: string, - callback: ResultCallback = this.#callback.getResultCallback( - 'setTemplate' - ) + callback?: ResultCallback ): this { ValidationError.#guardTemplate(template, callback) && (this.#tpl = template); @@ -751,10 +731,10 @@ public setTemplate( **Parameters:** -| Name: type | Description | -| :--------------------------------- | :---------- | -| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable `[problem]` and `[fix]` tags. | -| `callback: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` tags. By default, it uses an internal callback under the `'setTemplate'` name, which can be initially set by the optional `callback` parameter that gives access to the internal instance of [`Callback`][callback-github-readme]. | +| Name: type | Description | +| :---------------------------------- | :---------- | +| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable `[problem]` and `[fix]` tags. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` tags. | **Returns:** @@ -1083,18 +1063,6 @@ An optional error message template of a [`string`][js-string] type.
-## Type - -#### `VEAllowedCallback` - -Allowed callback function names available for the [`ValidationError`](#validationerror). - -```typescript -type VEAllowedCallback = 'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'; -``` - -
- ## Experimental ![experimental] [![update]][error-github-changelog] From 551ecd59f9a94a69ec71236822352eb8655bf60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 18:31:35 +0200 Subject: [PATCH 032/179] docs(README.md): update callback example. --- README.md | 109 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 26dc817..f778fb9 100644 --- a/README.md +++ b/README.md @@ -443,6 +443,8 @@ const validationError = new ValidationError({ fix, problem }); ```typescript // Example usage with callback. import { ValidationError } from '@angular-package/error'; +import { Callback, ErrorMessage } from '@angular-package/callback'; +import { ResultCallback } from '@angular-package/type'; // Define a fix. const fix = 'There is no solution to the described problem.'; @@ -453,47 +455,76 @@ const problem = 'The problem has no solution.'; // Define a template. const template = 'PROBLEM: [problem] FIX: [fix]'; -// Initialize an instance. -TODO: Example with callback. -const validationError = new ValidationError( - { fix, problem, template }, - (callback) => { - callback - /* - Console: false, - { - "fix": "There is no solution to the described problem.", - "problem": "The problem has no solution.", - "template": "PROBLEM: [problem] FIX: [fix]" - } - - Console: true, - { - "fix": "There is no solution to the described problem.", - "problem": "The problem has no solution.", - "template": "PROBLEM: [problem] FIX: [fix]" - } - */ - .setResultCallback('setFix', (result, value) => - console.log(`setFix`, result, value) - ) - - // Console: 'setFix true There is no solution to the described problem.' - .setResultCallback('setMessage', (result, value) => - console.log(`setMessage`, result, value) - ) - - // Console: 'setProblem true The problem has no solution.' - .setResultCallback('setProblem', (result, value) => - console.log(`setProblem`, result, value) - ) - - // Console: 'setTemplate true PROBLEM: [problem] FIX: [fix]' - .setResultCallback('setTemplate', (result, value) => - console.log(`setTemplate`, result, value) - ); +class CustomError { + #callback = new Callback('setFix', 'setMessage', 'setProblem', 'setTemplate'); + #validationError = new ValidationError(); + + constructor( + message: string | ErrorMessage, + callback?: ( + callback: Callback<'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'> + ) => void + ) { + if (callback) { + callback(this.#callback); + } + + this.#validationError.setMessage(message); } + + public setMessage( + message: string | ErrorMessage, + callback: ResultCallback = this.#callback.getResultCallback( + 'setMessage' + ) + ) { + this.#validationError.setMessage(message, callback); + } + + public throw(): void { + this.#validationError.throw(); + } +} + +const customError = new CustomError('', (callback) => + callback + .setResultCallback('setFix', (result, value) => + console.log(`setFix`, result, value) + ) + + // Console: 'setFix true There is no solution to the described problem.' + .setResultCallback('setMessage', (result, value) => + console.log(`setMessage`, result, value) + ) + + // Console: 'setProblem true The problem has no solution.' + .setResultCallback('setProblem', (result, value) => + console.log(`setProblem`, result, value) + ) + + // Console: 'setTemplate true PROBLEM: [problem] FIX: [fix]' + .setResultCallback('setTemplate', (result, value) => + console.log(`setTemplate`, result, value) + ) ); + +/** + * ! Console returns + * ? Checks message if string. + * setMessage false + * { + * fix: 'There is no solution to the described problem.', + * problem: 'The problem has no solution.', template: 'PROBLEM: [problem] FIX: [fix]' + * } + * + * ? Checks message is an object. + * setMessage false + * { + * fix: 'There is no solution to the described problem.', + * problem: 'The problem has no solution.', template: 'PROBLEM: [problem] FIX: [fix]' + * } + */ +customError.setMessage({ fix, problem, template }); ```
From c1d62cd678d73fd834826a0d29005c9fe39fb0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 3 Oct 2021 22:33:23 +0200 Subject: [PATCH 033/179] docs(README.md): add gitter chat. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index f778fb9..25ef64f 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Useful and simple to use packages based on the [angular.io][angulario]. Manages an [`Error`][js-error]. +[![Gitter][gitter-badge]][gitter-chat] [![npm version][error-npm-badge-svg]][error-npm-badge] @@ -1249,6 +1250,10 @@ MIT © angular-package ([license][error-license]) [new]: https://img.shields.io/badge/-new-green [update]: https://img.shields.io/badge/-update-red + +[gitter-badge]: https://badges.gitter.im/angularpackage/Lobby.svg +[gitter-chat]: https://gitter.im/angularpackage/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge + [git-semver]: http://semver.org/ From ccd2505b3e2b2dc680748a5c6020585c04dea04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 14:07:12 +0200 Subject: [PATCH 034/179] docs(CHANGELOG.md): update 3.0.0. --- CHANGELOG.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef4c56d..50511e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,45 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [3.0.0] - 2021-10-05 + +### 3.0.0 Added + +- [285ef51] + Added property `template` of an instance that refers to private property `#tpl`. + Added private property `#tags` of `string[]` to replace the template `[problem]` and `[fix]` tags. + +### 3.0.0 Changed + +- [8a0f58d] + Changed library to `es2020`. + +- [285ef51] [25ea72b] + Changed all conditionals to short if expressions. + Changed use of static `ValidationError` to `this`. + Changed to `ResultCallback` with generic type variable `Value`. + Changed usage of `is` and `guard` objects to use functions of `@angular-package/type`. + Changed the `setMessage()` method to set the `problem` and `fix` properties to empty string if the provided message is a `string` type. + Changed `setMessage()` method to use as first, template from the given `message` parameter, and if it's empty the private property `#tpl`. + Changed `defineMessage()` static method to use private property `#tags` to replace them in the template. + +- [25ea72b] + Change `set` prefix to `replace` of methods. + +[25ea72b]: https://github.com/angular-package/error/commit/25ea72b733939264b6546d01ef2945cc1e716fa5 +[8a0f58d]: https://github.com/angular-package/error/commit/8a0f58dae2b8a474848a2eccbe3218a0da0c017e +[285ef51]: https://github.com/angular-package/error/commit/285ef51805a3f1528b62d389214c106c3f213dfa + +### 3.0.0 Removed + +- [7ed2b5c] [96c5563] [4af703a] + Removed the default usage of callback. + Removed `AllowedCallback` type because the removed default usage of callback. + +[7ed2b5c]: https://github.com/angular-package/error/commit/7ed2b5c90ed9922b8ced3622db4e0aa9b51afc38 +[96c5563]: https://github.com/angular-package/error/commit/96c5563285f86eb2ce5d1ecff39aa2110507f220 +[4af703a]: https://github.com/angular-package/error/commit/4af703afa7c2c432a902faad96fa535640bc5671 + ## [2.0.2] - 2021-08-12 ### 2.0.2 Fixed From 7e59245dc77464dddedece37006b496c19b17606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 14:07:43 +0200 Subject: [PATCH 035/179] chore(package): 3.0.0 --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index e6eaf72..f25d8b6 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,13 @@ "name": "@angular-package/error", "version": "3.0.0", "description": "Manages an error.", - "author": "Angular Package (https://wvvw.dev)", + "author": "Angular Package (https://angular-package.dev)", "homepage": "https://github.com/angular-package/error#readme", "peerDependencies": { - "@angular-package/callback": "^3.0.0", "@angular-package/type": "^5.0.0" }, "dependencies": { - "tslib": "^2.2.0" + "tslib": "^2.3.0" }, "devDependencies": { "@angular-package/testing": "^2.0.0" From 53c20435df2225cb38e8d45d8f07e9c60db4d95a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:35:06 +0200 Subject: [PATCH 036/179] refactor(ValidationError): add the `value` property to the error message. Add: - `setValue()` method. - `#value` private property by default equal to ''. - tag `[value]`. - `value` instance property that refers to private `#value`. Update: - `updateMessage()` method includes `#value` property. --- src/lib/validation-error.class.ts | 61 +++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 8308ceb..861766c 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -20,7 +20,7 @@ export class ValidationError extends Error { /** * Tags to be replaced. */ - static #tags = ['fix', 'problem']; + static #tags = ['fix', 'problem', 'value']; /** * A static, privately stored template of the error message. @@ -46,6 +46,11 @@ export class ValidationError extends Error { * * By default, it's set to `Problem: [problem] => Fix: [fix]`. */ #tpl = ValidationError.template; + + /** + * The value affected by the validation error after parsing to `string`. + */ + #value = ''; //#endregion instance private properties. //#region static public properties @@ -106,8 +111,8 @@ export class ValidationError extends Error { } /** - * A template of the error message guarded by `string` type with the replaceable `[problem]` and `[fix]` tags. The value is being - * checked against the existence of `[problem]` and `[fix]` tags. + * A template of the error message guarded by `string` type with required replaceable `[problem]` and `[fix]` tags and optionally + * `[value]` tag. The value is being checked against the existence of required `[problem]` and `[fix]` tags. * * It can be set directly or by the `setTemplate()` and `setMessage()` method. * * By default, it's set to `Problem: [problem] => Fix: [fix]`. */ @@ -117,6 +122,16 @@ export class ValidationError extends Error { public set template(value: string) { ValidationError.#guardTemplate(value) && (this.#tpl = value); } + + /** + * The value affected by the validation error, which must be parsed to `string`. + */ + public get value(): string { + return this.#value; + } + public set value(value: string) { + guardString(value) && (this.#value = value); + } //#endregion instance public properties. //#region static public methods @@ -157,7 +172,7 @@ export class ValidationError extends Error { (tag) => (m = m.replace( `[${tag}]`, - message[tag as keyof Omit] + String((message[tag as keyof Omit])) )) ), m @@ -251,16 +266,12 @@ export class ValidationError extends Error { callback ); - // Sets `fix`, `problem` and `template` from the provided `message`. - if (isObject(message)) { - this.setFix(message.fix).setProblem(message.problem); - if (isDefined(message.template)) { - this.setTemplate(message.template); - } - } else { - this.#problem = ''; - this.#fix = ''; - } + isObject(message) + // Sets `fix`, `problem`, `value` and `template` from the provided `message`. + ? (this.setFix(message.fix).setProblem(message.problem).setValue(message.value as string), + isDefined(message.template) && this.setTemplate(message.template)) + // Sets `fix`, `problem` to empty string. + : (this.#fix = '', this.#problem = '', this.#value = ''); return this; } @@ -282,6 +293,7 @@ export class ValidationError extends Error { * @param template A `message` template guarded by `string` type with replaceable `[problem]` and `[fix]` tags. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a * `string` that contains `[fix]` and `[problem]` tags. + * @returns The return value is an instance of an `ValidationError`. * @angularpackage */ public setTemplate( @@ -294,8 +306,24 @@ export class ValidationError extends Error { } /** - * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem` and `template` or optionally from - * the provided `message`. + * Sets the value affected by the validation error (must be parsed to string type). + * @param value The value of `string` type as a replacement to the `[value]` tag of `template` that relates to the given `problem`. + * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `value` is `string` + * type. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setValue( + value: string, + callback?: ResultCallback + ): this { + guardString(value, callback) && (this.#value = value); + return this; + } + + /** + * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem` and `template` or optionally from the + * provided `message`. * @param message An optional parameter of `string` type or an `object` of an `ErrorMessage` interface to build the message of `string` * type. The value is checked against the proper `object` of `ErrorMessage`. * @angularpackage @@ -316,6 +344,7 @@ export class ValidationError extends Error { fix: this.#fix, problem: this.#problem, template: this.#tpl, + value: this.#value, })); return this; } From 8cc7528035bde6b68a1ff80738d7c06fe10f02a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:35:51 +0200 Subject: [PATCH 037/179] refactor(ErrorMessage): add an optional `value` property affected by the validation error, which must be parsed to `string`. --- src/interface/error-message.interface.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/interface/error-message.interface.ts b/src/interface/error-message.interface.ts index ff935e3..e7bc226 100644 --- a/src/interface/error-message.interface.ts +++ b/src/interface/error-message.interface.ts @@ -1,8 +1,9 @@ /** * The shape of an `object` for the error message that contains a possible solution to the described problem. * @param fix A possible solution to the described problem of a `string` type. - * @param problem Description of validation problem of a string type. + * @param problem Description of validation problem of a `string` type. * @param template An optional message template of a `string` type. + * @param value An optional value affected by the validation error, which must be parsed to `string`. */ export interface ErrorMessage { /** @@ -11,12 +12,17 @@ export interface ErrorMessage { fix: string; /** - * Description of validation problem of a `string` type. + * Description of validation problem of string type. */ problem: string; /** - * An optional message template of a `string` type. + * An optional message template of a string type. */ template?: string; + + /** + * An optional value affected by the validation error, which must be parsed to `string`. + */ + value?: string; } From 0f1b105df869c8b3eabeccb708a331bc944bed0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:36:24 +0200 Subject: [PATCH 038/179] test(ValidationError): add tests for the new `value` property. --- src/test/validation-error.spec.ts | 290 ++++++++++++++++++------------ 1 file changed, 171 insertions(+), 119 deletions(-) diff --git a/src/test/validation-error.spec.ts b/src/test/validation-error.spec.ts index 1876016..feca4d1 100644 --- a/src/test/validation-error.spec.ts +++ b/src/test/validation-error.spec.ts @@ -14,6 +14,7 @@ testing.describe('ValidationError', () => { let fix: any; let problem: any; let template: any; + let value: any; let errorMessage: any; let validationError: ValidationError; @@ -72,73 +73,64 @@ testing.describe('ValidationError', () => { .describe('.defineMessage()', () => { beforeEach(() => errorMessage = ValidationError.defineMessage({ fix, problem })); - testing.it(`with the message of the \`ErrorMessage\` interface`, () => { - toBe.string(errorMessage); - expect(errorMessage).toEqual( - ValidationError.template - .replace(`[fix]`, fix) - .replace(`[problem]`, problem) - ); - // toContain. - expect(errorMessage).toContain(fix); - expect(errorMessage).toContain(problem); - }); - }) - - .describe('.prototype.setFix()', () => { - testing.it(`set ${fix}`, () => { - validationError.setFix(fix, (result, value, payload) => { - expect(value).toEqual(fix); - toBe - .boolean(result) - .string(value) - .undefined(payload); - return result; + testing + .it(`with the message of the \`ErrorMessage\` interface`, () => { + toBe.string(errorMessage); + expect(errorMessage).toEqual( + ValidationError.template + .replace(`[fix]`, fix) + .replace(`[problem]`, problem) + ); + // toContain. + expect(errorMessage).toContain(fix); + expect(errorMessage).toContain(problem); }); - expect(validationError.fix).toEqual(fix); - }); }) - .describe('.prototype.setMessage()', () => { - testing.it(`set ${problem}`, () => { - validationError.setMessage(problem, (result, value, payload) => { - expect(value).toEqual(problem); - toBe - .boolean(result) - .string(value) - .undefined(payload); - return result; + .describe('.prototype.setFix()', () => { + testing + .it(`set ${fix}`, () => { + validationError.setFix(fix, (result, value, payload) => { + expect(value).toEqual(fix); + toBe + .boolean(result) + .string(value) + .undefined(payload); + return result; + }); + expect(validationError.fix).toEqual(fix); }); - expect(validationError.message).toEqual(problem); - }); }) .describe('[counter] .prototype.setMessage()', () => { - testing.it(`[counter] set ${problem}`, () => { - validationError.setMessage(problem, (result, value, payload) => { - expect(value).toEqual(problem); - toBe - .boolean(result) - .string(value) - .undefined(payload); - return result; - }); - expect(validationError.message).toEqual(problem); - }) - .it(`[counter] set object`, () => { - errorMessage = { fix, problem }; - validationError.setMessage(errorMessage, (result, value, payload) => { - expect((value as any).fix).toEqual(errorMessage.fix); - expect((value as any).problem).toEqual(errorMessage.problem); - toBe - .boolean(result) - .object(value) - .undefined(payload); - return result; + testing + + .it(`[counter] set ${problem}`, () => { + validationError.setMessage(problem, (result, value, payload) => { + expect(value).toEqual(problem); + toBe + .boolean(result) + .string(value) + .undefined(payload); + return result; + }); + expect(validationError.message).toEqual(problem); + }) + + .it(`[counter] set object`, () => { + errorMessage = { fix, problem }; + validationError.setMessage(errorMessage, (result, value, payload) => { + expect((value as any).fix).toEqual(errorMessage.fix); + expect((value as any).problem).toEqual(errorMessage.problem); + toBe + .boolean(result) + .object(value) + .undefined(payload); + return result; + }); + expect(validationError.fix).toEqual(fix); + expect(validationError.problem).toEqual(problem); }); - expect(validationError.fix).toEqual(fix); - expect(validationError.problem).toEqual(problem); - }); }) .describe('[counter] .prototype.setProblem()', () => { @@ -170,70 +162,130 @@ testing.describe('ValidationError', () => { }); }) + .describe('[counter] .prototype.setValue()', () => { + template = `[problem] [fix] Got [value]`; + testing + + .it(`[counter] set ${template} with the string value`, () => { + value = 'string value'; + validationError.setTemplate(template).setValue(value, (result, value, payload) => { + expect(value).toEqual('string value'); + toBe.boolean(result).string(value); + return result; + }); + expect(validationError.template).toEqual(template); + }) + + .it(`[counter] set ${template} with the object value`, () => { + value = { firstName: Symbol(123), lastName: '', age: 27 }; + validationError.setTemplate(template).setValue(JSON.stringify(value), (result, value, payload) => { + expect(value).toEqual('{"lastName":"","age":27}'); + toBe.boolean(result).string(value); + return result; + }) + .setProblem(problem).setFix(fix).updateMessage(); + expect(validationError.message) + .toEqual('The problem has no solution. There is no solution to the described problem. Got {"lastName":"","age":27}'); + expect(validationError.template).toEqual(template); + }); + }) + .describe('[counter] .prototype.updateMessage()', () => { - testing.it(`[counter] works properly`, () => { - validationError - .setFix(fix) - .setProblem(problem); - expect(validationError.fix).toEqual(fix); - expect(validationError.problem).toEqual(problem); - expect(validationError.message).toEqual(''); - validationError.updateMessage(); - expect(validationError.message) - .toEqual('Problem: The problem has no solution. => Fix: There is no solution to the described problem.'); - }); + testing + .it(`[counter] works properly`, () => { + validationError + .setFix(fix) + .setProblem(problem); + expect(validationError.fix).toEqual(fix); + expect(validationError.problem).toEqual(problem); + expect(validationError.message).toEqual(''); + validationError.updateMessage(); + expect(validationError.message) + .toEqual('Problem: The problem has no solution. => Fix: There is no solution to the described problem.'); + }); }) .describe('[counter] .prototype.throw()', () => { testing - .it(`[counter] with string`, () => { - try { - validationError.setFix(fix).setProblem(problem); - validationError.throw('Throws a string'); - } catch (e) { - expect(e.problem).toEqual(''); - expect(e.fix).toEqual(''); - toBe - .stringOfLength(e.problem, { max: 0 }) - .stringOfLength(e.fix, { max: 0 }); - } - }) - .it(`[counter] with object of ErrorMessage`, () => { - try { - validationError.setFix(fix).setProblem(problem).setTemplate(`[problem], [fix]`); - validationError.throw({ - problem: 'new problem', - fix: 'new fix' - }); - } catch (e) { - expect(e.message).toEqual(`new problem, new fix`); - expect(e.problem).toEqual('new problem'); - expect(e.fix).toEqual('new fix'); - } - }) - .it(`[counter] with set string type message`, () => { - try { - validationError.setMessage(problem); - validationError.throw(); - } catch (e) { - expect(e.message).toEqual(problem); - expect(e.problem).toEqual(''); - expect(e.fix).toEqual(''); - toBe - .stringOfLength(e.problem, { max: 0 }) - .stringOfLength(e.fix, { max: 0 }); - } - }) - .it(`[counter] with the actual \`problem\` and \`fix\``, () => { - try { - validationError.setMessage('my message'); - validationError.setProblem(problem).setFix(fix).setTemplate(`[problem], [fix]`); - validationError.throw(); - } catch (e) { - expect(e.message).toEqual(`${problem}, ${fix}`); - expect(e.problem).toEqual(problem); - expect(e.fix).toEqual(fix); - } - }); + .it(`[counter] with string`, () => { + try { + validationError.setFix(fix).setProblem(problem); + validationError.throw('Throws a string'); + } catch (e) { + expect(e.problem).toEqual(''); + expect(e.fix).toEqual(''); + toBe + .stringOfLength(e.problem, { max: 0 }) + .stringOfLength(e.fix, { max: 0 }); + } + }) + .it(`[counter] with object of ErrorMessage`, () => { + try { + validationError.setFix(fix).setProblem(problem).setTemplate(`[problem], [fix]`); + validationError.throw({ + problem: 'new problem', + fix: 'new fix' + }); + } catch (e) { + expect(e.message).toEqual(`new problem, new fix`); + expect(e.problem).toEqual('new problem'); + expect(e.fix).toEqual('new fix'); + } + }) + .it(`[counter] with set \`string\` type message`, () => { + try { + validationError + .setMessage(problem) + .throw(); + } catch (e) { + expect(e.message).toEqual(problem); + expect(e.problem).toEqual(''); + expect(e.fix).toEqual(''); + toBe + .stringOfLength(e.problem, { max: 0 }) + .stringOfLength(e.fix, { max: 0 }); + } + }) + + .it(`[counter] with the actual \`problem\`, \`fix\` and \`value\``, () => { + value = '27'; + try { + validationError + .setMessage('my message') + .setProblem(problem).setFix(fix).setValue(value).setTemplate(`[problem], [fix], [value]`) + .throw(); + } catch (e) { + expect(e.message).toEqual(`${problem}, ${fix}, ${value}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + expect(e.value).toEqual(value); + } + + try { + validationError + .setMessage('my message') + .setMessage({ + fix, problem, value, template: `[problem] [fix] [value]` + }).throw(); + } catch (e) { + expect(e.message).toEqual(`${problem} ${fix} ${value}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + expect(e.value).toEqual(value); + } + + try { + validationError + .setMessage('my message') + .setMessage({ + fix, problem, template: `[problem] [fix] [value]` + }).throw(); + } catch (e) { + expect(e.message).toEqual(`${problem} ${fix} `); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + expect(e.value).toEqual(''); + } + }); }); }); From 344dcf62e6379c54d0ccc0ab6987984bf3f726db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:37:42 +0200 Subject: [PATCH 039/179] docs(README.md): update. --- README.md | 110 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 25ef64f..99f9cf5 100644 --- a/README.md +++ b/README.md @@ -148,13 +148,15 @@ Manages an [`Error`][js-error] of validation. | [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | | [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with required replaceable `[problem]` and `[fix]` tags and optionally `[value]` tag. | +| [`value: string`][error-property-value] | The value affected by the validation error guarded by [`string`][js-string] type. | [error-property-fix]: #validationerrorprototypefix [error-property-message]: #validationerrorprototypemessage [error-property-name]: #validationerrorprototypename [error-property-problem]: #validationerrorprototypeproblem [error-property-template]: #validationerrorprototypetemplate +[error-property-value]: #validationerrorprototypevalue **Static methods:** @@ -176,6 +178,7 @@ Manages an [`Error`][js-error] of validation. | [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. | | [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | | [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | +| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). | | [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. | | [`updateMessage()`][error-method-updatemessage] | Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. | @@ -183,6 +186,7 @@ Manages an [`Error`][js-error] of validation. [error-method-setmessage]: #validationerrorprototypesetmessage [error-method-setproblem]: #validationerrorprototypesetproblem [error-method-settemplate]: #validationerrorprototypesettemplate +[error-method-setvalue]: #validationerrorprototypesetvalue [error-method-throw]: #validationerrorprototypethrow [error-method-updatemessage]: #validationerrorprototypeupdatemessage @@ -272,9 +276,9 @@ public set problem(value: string) { #### `ValidationError.prototype.template` -![new] +[![new]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of `[problem]` and `[fix]` tags. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags and optionally `[value]` tag. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of required `[problem]` and `[fix]` tags. ```typescript public get template(): string { @@ -285,6 +289,31 @@ public set template(value: string) { } ``` +**Tags:** + +`[fix]`: **Required**, replace by the [`fix`][error-property-fix] property. + +`[problem]`: **Required**, replace by the [`problem`][error-property-problem] property. + +`[value]`: Optional, replace by the [`value`][error-property-value] property. + +
+ +#### `ValidationError.prototype.value` + +[![new]][error-github-changelog] + +The value affected by the validation error guarded by [`string`][js-string] type. + +```typescript +public get value(): string { + return this.#value; +} +public set value(value: string) { + guardString(value) && (this.#value = value); +} +``` +
### `ValidationError` static methods @@ -616,16 +645,10 @@ public setMessage( callback ); - // Sets `fix`, `problem` and `template` from the provided `message`. - if (isObject(message)) { - this.setFix(message.fix).setProblem(message.problem); - if (isDefined(message.template)) { - this.setTemplate(message.template); - } - } else { - this.#problem = ''; - this.#fix = ''; - } + isObject(message) + ? (this.setFix(message.fix).setProblem(message.problem).setValue(message.value as string), + isDefined(message.template) && this.setTemplate(message.template)) + : (this.#fix = '', this.#problem = '', this.#value = ''); return this; } ``` @@ -813,6 +836,62 @@ validationError.setTemplate(template, (result, value) => {
+#### `ValidationError.prototype.setValue()` + +[![update]][error-github-changelog] + +Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). + +```typescript +public setValue( + value: string, + callback?: ResultCallback +): this { + guardString(value, callback) && (this.#value = value); + return this; +} +``` + +**Parameters:** + +| Name: type | Description | +| :---------------------------------- | :---------- | +| `value: string` | The value of [`string`][js-string] type as a replacement to the `[value]` tag of [`template`][error-property-template] that relates to the given [`problem`][error-property-problem]. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `value` is [`string`][js-string] type. | + +**Returns:** + +The **return value** is an instance of an [`ValidationError`](#validationerror). + +**Usage:** + +```typescript +// Example usage with value of Symbol. +import { ValidationError } from '@angular-package/error'; + +const fix = 'Please, provide the string type value.'; +const problem = 'The provided value should be a string type, got '; + +// The provided value should be a string type, got Symbol(123) +// Please, provide the string type value. +ValidationError.defineMessage({ + fix, + problem, + value: String(Symbol('123')), + template: '\n[problem] [value]\n[fix]' }); + + +// The provided value should be a string type, got {"one":1} +// Please, provide the string type value. +ValidationError.defineMessage({ + fix, + problem, + value: JSON.stringify({one: 1 }), + template: '\n[problem] [value]\n[fix]' }); +``` + +
+ #### `ValidationError.prototype.throw()` [![update]][error-github-changelog] @@ -892,6 +971,7 @@ public updateMessage(): this { fix: this.#fix, problem: this.#problem, template: this.#tpl, + value: this.#value, })); return this; } @@ -1079,6 +1159,7 @@ export interface ErrorMessage { fix: string; problem: string; template?: string; + value?: string; } ``` @@ -1093,6 +1174,9 @@ Description of validation problem of a [`string`][js-string] type. **`template?: string`** An optional error message template of a [`string`][js-string] type. +**`value?: string`** +An optional value affected by the validation error, which must be parsed to [`string`][js-string]. +
## Experimental From b164685c45062e87ad4425be997fabf98897c7c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:39:22 +0200 Subject: [PATCH 040/179] docs(README.md): fix. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 99f9cf5..97c2765 100644 --- a/README.md +++ b/README.md @@ -838,7 +838,7 @@ validationError.setTemplate(template, (result, value) => { #### `ValidationError.prototype.setValue()` -[![update]][error-github-changelog] +[![new]][error-github-changelog] Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). From 9ed20cedf3ea3d3e4e3b70cc5a5315da0584a0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:50:42 +0200 Subject: [PATCH 041/179] docs(README.md): update. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 97c2765..b3b5df4 100644 --- a/README.md +++ b/README.md @@ -630,7 +630,7 @@ validationError.setFix(fix, (result, value) => { Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. -> If the provided `message` is a [`string`][js-string] type, then properties [`problem`][error-property-problem], and [`fix`][error-property-fix] are set to empty [`string`][js-string] to not confuse their relation to the [`message`][error-property-message] and have a possibility throw an error by the [`throw()`](#validationerrorprototypethrow) method with just only a [`string`][js-string] type. +> If the provided `message` is a [`string`][js-string] type, then properties [`problem`][error-property-problem], [`fix`][error-property-fix], and [`value`][error-property-value] are set to empty [`string`][js-string] to not confuse their relation to the [`message`][error-property-message] and have a possibility to throw an error by the [`throw()`](#validationerrorprototypethrow) method with just only a [`string`][js-string] type. ```typescript // Syntax. @@ -1175,6 +1175,7 @@ Description of validation problem of a [`string`][js-string] type. An optional error message template of a [`string`][js-string] type. **`value?: string`** +[![new]][error-github-changelog] An optional value affected by the validation error, which must be parsed to [`string`][js-string].
From 8c300c20fb9fe2c465a2730255c886a1f94413cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 19:52:39 +0200 Subject: [PATCH 042/179] docs(README.md): fix. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b3b5df4..e1cf474 100644 --- a/README.md +++ b/README.md @@ -866,7 +866,7 @@ The **return value** is an instance of an [`ValidationError`](#validationerror). **Usage:** ```typescript -// Example usage with value of Symbol. +// Example usage. import { ValidationError } from '@angular-package/error'; const fix = 'Please, provide the string type value.'; From 942bdd90a34380f9d8e2db592463d807792d6a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Mon, 4 Oct 2021 20:20:51 +0200 Subject: [PATCH 043/179] docs(CHANGELOG.md): update. --- CHANGELOG.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50511e2..476c594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,16 +10,25 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### 3.0.0 Added +- [53c2043] + Added `setValue()` method. + Added private property `#value` by default equal to `''`. + Added tag `[value]`. + Added `value` instance property that refers to private `#value`. + - [285ef51] Added property `template` of an instance that refers to private property `#tpl`. Added private property `#tags` of `string[]` to replace the template `[problem]` and `[fix]` tags. ### 3.0.0 Changed +- [53c2043] + Changed `updateMessage()` method to include `#value` property. + - [8a0f58d] Changed library to `es2020`. -- [285ef51] [25ea72b] +- [285ef51] [25ea72b] Changed all conditionals to short if expressions. Changed use of static `ValidationError` to `this`. Changed to `ResultCallback` with generic type variable `Value`. @@ -31,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [25ea72b] Change `set` prefix to `replace` of methods. +[53c2043]: https://github.com/angular-package/error/commit/53c20435df2225cb38e8d45d8f07e9c60db4d95a [25ea72b]: https://github.com/angular-package/error/commit/25ea72b733939264b6546d01ef2945cc1e716fa5 [8a0f58d]: https://github.com/angular-package/error/commit/8a0f58dae2b8a474848a2eccbe3218a0da0c017e [285ef51]: https://github.com/angular-package/error/commit/285ef51805a3f1528b62d389214c106c3f213dfa From 37a8d1841e7d10297f679526c4f05debbded0b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 00:06:24 +0200 Subject: [PATCH 044/179] refactor(interface/ErrorMessage): add `id` an optional identifier of the described problem, change the type of an optional `value` from `string to `any` to preserve original type of the value. --- src/interface/error-message.interface.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/interface/error-message.interface.ts b/src/interface/error-message.interface.ts index e7bc226..e592ab7 100644 --- a/src/interface/error-message.interface.ts +++ b/src/interface/error-message.interface.ts @@ -1,9 +1,10 @@ /** * The shape of an `object` for the error message that contains a possible solution to the described problem. * @param fix A possible solution to the described problem of a `string` type. + * @param id An optional identifier of the described problem. * @param problem Description of validation problem of a `string` type. * @param template An optional message template of a `string` type. - * @param value An optional value affected by the validation error, which must be parsed to `string`. + * @param value An optional value of any type affected by the validation error, which should be converted to `string`. */ export interface ErrorMessage { /** @@ -11,6 +12,11 @@ export interface ErrorMessage { */ fix: string; + /** + * An optional identifier of the described problem. + */ + id?: number; + /** * Description of validation problem of string type. */ @@ -22,7 +28,7 @@ export interface ErrorMessage { template?: string; /** - * An optional value affected by the validation error, which must be parsed to `string`. + * An optional value of any type affected by the validation error, which should be converted to `string`. */ - value?: string; + value?: any; } From 48e6ab752f64095ef317839304d57d660c885dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 00:08:13 +0200 Subject: [PATCH 045/179] feat(type/ValueParser): add type `ValueParser` the function to convert the value of any type to `string`. --- src/type/value-parser.type.ts | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/type/value-parser.type.ts diff --git a/src/type/value-parser.type.ts b/src/type/value-parser.type.ts new file mode 100644 index 0000000..bbbbea3 --- /dev/null +++ b/src/type/value-parser.type.ts @@ -0,0 +1,4 @@ +/** + * The function to convert the value of any type to `string`. + */ +export type ValueParser = (value: any) => string; From 0e5bf686a0405919e482d4bc882538f932379ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 00:13:54 +0200 Subject: [PATCH 046/179] docs(README.md): add new features. --- README.md | 365 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 262 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index e1cf474..d6c56d5 100644 --- a/README.md +++ b/README.md @@ -138,20 +138,22 @@ Manages an [`Error`][js-error] of validation. | ValidationError. | Description | | :--------------------------------------------- | :---------- | -| [`template: string`](#validationerrortemplate) | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. By default, it's set to `'Problem: [problem] => Fix: [fix]'`. | +| [`template: string`](#validationerrortemplate) | A template of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]`. | **Instance properties:** | ValidationError.prototype. | Description | | :-------------------------------------------- | :---------- | | [`fix: string`][error-property-fix] | A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] type. | -| [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | +| [`id: number`][error-property-id] | The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type. | +| [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | | [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with required replaceable `[problem]` and `[fix]` tags and optionally `[value]` tag. | -| [`value: string`][error-property-value] | The value affected by the validation error guarded by [`string`][js-string] type. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with required replaceable `[problem]` and `[fix]` tags and optionally `[id]` and`[value]` tag. | +| [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. | [error-property-fix]: #validationerrorprototypefix +[error-property-id]: #validationerrorprototypeid [error-property-message]: #validationerrorprototypemessage [error-property-name]: #validationerrorprototypename [error-property-problem]: #validationerrorprototypeproblem @@ -160,9 +162,11 @@ Manages an [`Error`][js-error] of validation. **Static methods:** -| ValidationError. | Description | -| :------------------------------------------------- | :---------- | -| [`defineMessage()`](#validationerrordefinemessage) | Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | +| ValidationError. | Description | +| :--------------------------------------------------- | :---------- | +| [`defineMessage()`](#validationerrordefinemessage) | Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | +| [`setTemplate()`](#validationerrorsettemplate) | Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the **default** value for [`template`][error-property-template] of an instance. | +| [`setValueParser()`](#validationerrorsetvalueparser) | Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation performed by the static [`defineMessage()`](#validationerrorsetvalueparser) method. | **Constructor:** @@ -172,21 +176,25 @@ Manages an [`Error`][js-error] of validation. **Instance methods:** -| ValidationError.prototype. | Description | -| :---------------------------------------------- | :---------- | -| [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | -| [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. | -| [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | -| [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | -| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). | -| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. | -| [`updateMessage()`][error-method-updatemessage] | Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. | +| ValidationError.prototype. | Description | +| :------------------------------------------------ | :---------- | +| [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | +| [`setId()`][error-method-setid] | Sets the [`id`][error-property-id] an identifier of the described [`problem`][error-property-problem]. | +| [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. | +| [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | +| [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | +| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). | +| [`setValueParser()`][error-method-setvalueparser] | Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] to the [`string`][js-string] during [`message`][error-property-message] creation. | +| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. | +| [`updateMessage()`][error-method-updatemessage] | Updates the [`message`][error-property-message] with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. | [error-method-setfix]: #validationerrorprototypesetfix +[error-method-setid]: #validationerrorprototypesetid [error-method-setmessage]: #validationerrorprototypesetmessage [error-method-setproblem]: #validationerrorprototypesetproblem [error-method-settemplate]: #validationerrorprototypesettemplate [error-method-setvalue]: #validationerrorprototypesetvalue +[error-method-setvalueparser]: #validationerrorprototypesetvalueparser [error-method-throw]: #validationerrorprototypethrow [error-method-updatemessage]: #validationerrorprototypeupdatemessage @@ -198,14 +206,14 @@ Manages an [`Error`][js-error] of validation. [![update]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags. The value is being checked against the existence of `[problem]` and `[fix]` tags. By default, it's set to `Problem: [problem] => Fix: [fix]`. +A template of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]`. The value is being checked against the existence of `[problem]` and `[fix]` tags. It can be set directly or by the static [`setTemplate()`](#validationerrorsettemplate) methods. By default, it's set to `Problem: [problem] => Fix: [fix]`. ```typescript static get template(): string { - return this.#template; + return this.#defaultTemplate; } static set template(value: string) { - this.#guardTemplate(value) && (this.#template = value); + this.#guardTemplate(value) && (this.#defaultTemplate = value); } ``` @@ -217,7 +225,7 @@ static set template(value: string) { [![update]][error-github-changelog] -A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] type. It can be set directly or by the [`setFix()`][error-method-setfix] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. +A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string]. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. ```typescript public get fix(): string { @@ -230,11 +238,28 @@ public set fix(value: string) {
+#### `ValidationError.prototype.id` + +[![new]][error-github-changelog] + +The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. + +```typescript +public get id(): number { + return this.#id; +} +public set id(value: number) { + guardNumber(value) && (this.#id = value); +} +``` + +
+ #### `ValidationError.prototype.message` [![update]][error-github-changelog] -A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the [`problem`][error-property-problem] and [`fix`][error-property-fix] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`throw()`][error-method-throw] and [`setMessage()`][error-method-setmessage] method. +A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. ```typescript public get message(): string { @@ -261,7 +286,7 @@ public name = ValidationError.name; [![update]][error-github-changelog] -Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. It can be set directly or by the [`setProblem()`][error-method-setproblem] and [`setMessage()`][error-method-setmessage] method. By default, it's an empty [`string`][js-string]. +Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. It can be set directly or by the [`setProblem()`][error-method-setproblem], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method. By default, it's an empty [`string`][js-string]. ```typescript public get problem(): string { @@ -278,7 +303,7 @@ public set problem(value: string) { [![new]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable `[problem]` and `[fix]` tags and optionally `[value]` tag. It can be set directly or by the [`setTemplate()`][error-method-settemplate] and [`setMessage()`][error-method-setmessage] method. The value is being checked against the existence of required `[problem]` and `[fix]` tags. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. The value is being checked against the existence of required `[problem]` and `[fix]` tags. ```typescript public get template(): string { @@ -293,9 +318,11 @@ public set template(value: string) { `[fix]`: **Required**, replace by the [`fix`][error-property-fix] property. +`[id]`: Optional, replace by the [`id`][error-property-id] property. + `[problem]`: **Required**, replace by the [`problem`][error-property-problem] property. -`[value]`: Optional, replace by the [`value`][error-property-value] property. +`[value]`: Optional, replace by the [`value`][error-property-value] property.
@@ -303,14 +330,14 @@ public set template(value: string) { [![new]][error-github-changelog] -The value affected by the validation error guarded by [`string`][js-string] type. +The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. The conversion is performed only during [`message`][error-property-message] creation by providing the parser to the static [`defineMessage()`](#validationerrordefinemessage) or instance [`setMessage()`][error-method-setmessage] method and can be converted automatically by the defined [`function`][js-function] of the `ValueParser` type, set by the [`setValueParser()`][error-method-setvalueparser] method. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. ```typescript -public get value(): string { +public get value(): any { return this.#value; } -public set value(value: string) { - guardString(value) && (this.#value = value); +public set value(value: any) { + this.#value = value; } ``` @@ -328,20 +355,22 @@ Defines the validation [`error`][js-error] message of [`string`][js-string] type // Syntax. public static defineMessage( message: ErrorMessage, - callback?: ResultCallback + callback?: ResultCallback, + parser: ValueParser = this.#defaultValueParser ): string { return this.#guardMessage(message, callback) - ? this.#buildMessage(message) + ? this.#buildMessage(message, parser) : ''; } ``` **Parameters:** -| Name: type | Description | -| :-------------------------- | :---------- | -| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided message is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | +| Name: type | Description | +| :---------------------------------------- | :---------- | +| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | +| `parser: ValueParser` | An optional [`function`][js-function] to convert property `value` from the provided [`ErrorMessage`](#errormessage) to [`string`][js-string] during message creation. It can be set by static [`setValueParser()`](#validationerrorsetvalueparser). | **Returns:** @@ -430,6 +459,80 @@ const errorMessage = ValidationError.defineMessage(
+#### `ValidationError.setTemplate()` + +[![new]][error-github-changelog] + +Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the default value for [`template`][error-property-template] of an instance. + +```typescript +// Syntax. +public static setTemplate( + template: string, + callback?: ResultCallback +): typeof ValidationError { + ValidationError.#guardTemplate(template, callback) && + (this.#defaultTemplate = template); + return ValidationError; +} +``` + +**Parameters:** + +| Name: type | Description | +| :---------------------------------- | :---------- | +| `template: string` | A [template](#validationerrortemplate) for error [`message`][error-property-message] guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]`. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains required `[fix]` and `[problem]` tags. | + +**Returns:** + +The **return value** is static [`ValidationError`](#validationerror). + +**Usage:** + +```typescript +// Example usage. + +``` + +
+ +#### `ValidationError.setValueParser()` + +[![new]][error-github-changelog] + +Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`defineMessage()`](#validationerrorsetvalueparser) method. + +```typescript +// Syntax. +public static setValueParser( + parser: ValueParser, + callback?: ResultCallback +): typeof ValidationError { + guardFunction(parser, callback) && (this.#defaultValueParser = parser); + return ValidationError; +} +``` + +**Parameters:** + +| Name: type | Description | +| :--------------------------------------- | :---------- | +| `parser: ValueParser` | The [`function`][js-function] of the `ValueParser` type to convert the value of [`any`][ts-any] type to [`string`][js-string]. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the result of the check whether the provided `parser` is [`function`][js-function] type. | + +**Returns:** + +The **return value** is static [`ValidationError`](#validationerror). + +**Usage:** + +```typescript +// Example usage. +``` + +
+ ### `ValidationError` constructor #### `ValidationError()` @@ -584,7 +687,7 @@ public setFix(fix: string, callback?: ResultCallback): this { **Returns:** -The **return value** is an instance of an [`ValidationError`](#validationerror). +The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** @@ -624,31 +727,62 @@ validationError.setFix(fix, (result, value) => {
+#### `ValidationError.prototype.setId()` + +[![new]][error-github-changelog] + +Sets the [`id`][error-property-id] an identifier of the described [`problem`][error-property-problem]. + +```typescript +// Syntax. +public setFix(fix: string, callback?: ResultCallback): this { + guardString(fix, callback) && (this.#fix = fix); + return this; +} +``` + +**Parameters:** + +| Name: type | Description | +| :---------------------------------- | :---------- | +| `fix: string` | The identifier, guarded by [`number`][js-number] type. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `id` is [`number`][js-number]. | + +**Returns:** + +The **return value** is an instance of a [`ValidationError`](#validationerror). + +**Usage:** + +```typescript +// Example usage. +import { ValidationError } from '@angular-package/error'; +``` + +
+ #### `ValidationError.prototype.setMessage()` [![update]][error-github-changelog] Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. -> If the provided `message` is a [`string`][js-string] type, then properties [`problem`][error-property-problem], [`fix`][error-property-fix], and [`value`][error-property-value] are set to empty [`string`][js-string] to not confuse their relation to the [`message`][error-property-message] and have a possibility to throw an error by the [`throw()`](#validationerrorprototypethrow) method with just only a [`string`][js-string] type. - ```typescript // Syntax. public setMessage( - message: string | ErrorMessage, - callback?: ResultCallback + message: ErrorMessage, + callback?: ResultCallback, + parser: ValueParser = this.#valueParser ): this { - super.message = isString(message, callback) - ? message - : ValidationError.defineMessage( - { ...message, ...{ template: message.template || this.#tpl } }, - callback - ); - - isObject(message) - ? (this.setFix(message.fix).setProblem(message.problem).setValue(message.value as string), - isDefined(message.template) && this.setTemplate(message.template)) - : (this.#fix = '', this.#problem = '', this.#value = ''); + (super.message = ValidationError.defineMessage( + { ...message, ...{ template: message.template || this.#template } }, + callback, + parser + )) && + this.setFix(message.fix) + .setId(message.id || this.#id) + .setProblem(message.problem) + .setValue(message.value); return this; } ``` @@ -657,12 +791,13 @@ public setMessage( | Name: type | Description | | :------------------------------------------ | :---------- | -| `message: string \| ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `message: ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. All properties are being assigned to the instance. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | +| `parser: ValueParser` | The [`function`][js-function] of the `ValueParser` type, to convert any value to a [`string`][js-string]. | **Returns:** -The **return value** is an instance of an [`ValidationError`](#validationerror). +The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** @@ -709,7 +844,7 @@ console.log(validationError.message); [![update]][error-github-changelog] -Sets description of the validation [`problem`][error-property-problem]. +Sets the description of the validation [`problem`][error-property-problem]. ```typescript public setProblem(problem: string, callback?: ResultCallback): this { @@ -722,12 +857,12 @@ public setProblem(problem: string, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `problem: string` | Description of a validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| `problem: string` | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. | **Returns:** -The **return value** is an instance of an [`ValidationError`](#validationerror). +The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** @@ -788,12 +923,12 @@ public setTemplate( | Name: type | Description | | :---------------------------------- | :---------- | -| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable `[problem]` and `[fix]` tags. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains `[fix]` and `[problem]` tags. | +| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains **required** `[fix]` and `[problem]` tags. | **Returns:** -The **return value** is an instance of an [`ValidationError`](#validationerror). +The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** @@ -840,28 +975,57 @@ validationError.setTemplate(template, (result, value) => { [![new]][error-github-changelog] -Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). +Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). ```typescript -public setValue( - value: string, - callback?: ResultCallback -): this { - guardString(value, callback) && (this.#value = value); +public setValue(value: any): this { + this.#value = value; return this; } ``` **Parameters:** -| Name: type | Description | -| :---------------------------------- | :---------- | -| `value: string` | The value of [`string`][js-string] type as a replacement to the `[value]` tag of [`template`][error-property-template] that relates to the given [`problem`][error-property-problem]. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `value` is [`string`][js-string] type. | +| Name: type | Description | +| :----------- | :---------- | +| `value: any` | The value of [`any`][ts-any] type as a replacement to the `[value]` tag of [`template`][error-property-template] that relates to the given [`problem`][error-property-problem]. | + +**Returns:** + +The **return value** is an instance of a [`ValidationError`](#validationerror). + +**Usage:** + +```typescript +// Example usage. +import { ValidationError } from '@angular-package/error'; + +``` + +
+ +#### `ValidationError.prototype.setValueParser()` + +[![new]][error-github-changelog] + +Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). + +```typescript +public setValueParser(parser: ValueParser): this { + guardFunction(parser) && (this.#valueParser = parser); + return this; +} +``` + +**Parameters:** + +| Name: type | Description | +| :-------------------- | :---------- | +| `parser: ValueParser` | The [`function`][js-function] of the `ValueParser` type, to convert any value to a [`string`][js-string]. | **Returns:** -The **return value** is an instance of an [`ValidationError`](#validationerror). +The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** @@ -869,25 +1033,6 @@ The **return value** is an instance of an [`ValidationError`](#validationerror). // Example usage. import { ValidationError } from '@angular-package/error'; -const fix = 'Please, provide the string type value.'; -const problem = 'The provided value should be a string type, got '; - -// The provided value should be a string type, got Symbol(123) -// Please, provide the string type value. -ValidationError.defineMessage({ - fix, - problem, - value: String(Symbol('123')), - template: '\n[problem] [value]\n[fix]' }); - - -// The provided value should be a string type, got {"one":1} -// Please, provide the string type value. -ValidationError.defineMessage({ - fix, - problem, - value: JSON.stringify({one: 1 }), - template: '\n[problem] [value]\n[fix]' }); ```
@@ -896,13 +1041,10 @@ ValidationError.defineMessage({ [![update]][error-github-changelog] -Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. - -> The provided `message` of the [`ErrorMessage`](#errormessage) is set to the instance to have the possibility to catch `problem` and `fix` properties to know that the message was built from it (it's not just a string message). -> If the `message` parameter is not provided, then if the `problem` of an instance of `ValidationError` is not an empty string it builds the message and overwrites the `message`. +Throws an error of [`ValidationError`](#validationerror) with the message built from the stored required [`fix`][error-property-fix], [`problem`][error-property-problem] and optional [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. The provided `message` of the [`ErrorMessage`](#errormessage) is set to the instance. If the `message` parameter is not provided, then if the `problem` of an instance of `ValidationError` is not an empty [`string`][js-string] it builds and overwrites the `message`. ```typescript -public throw(message?: string | ErrorMessage): void { +public throw(message?: ErrorMessage): void { isDefined(message) ? this.setMessage(message) : this.updateMessage(); throw this; } @@ -910,9 +1052,9 @@ public throw(message?: string | ErrorMessage): void { **Parameters:** -| Name: type | Description | -| :--------------------------------- | :---------- | -| `message?: string \| ErrorMessage` | An optional parameter of [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| Name: type | Description | +| :----------------------- | :---------- | +| `message?: ErrorMessage` | An optional [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | **Usage:** @@ -962,24 +1104,34 @@ validationError.throw({ fix, problem, template }); [![update]][error-github-changelog] -Updates the message with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. +Updates the message with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. ```typescript -public updateMessage(): this { - guardStringLength(this.#problem, { min: 1 }) && - (super.message = ValidationError.defineMessage({ +public updateMessage(callback?: ResultCallback): this { + super.message = ValidationError.defineMessage( + { fix: this.#fix, + id: this.#id, problem: this.#problem, - template: this.#tpl, + template: this.#template, value: this.#value, - })); + }, + callback, + this.#valueParser + ); return this; } ``` +**Parameters:** + +| Name: type | Description | +| :---------------------------------------- | :---------- | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the stored properties used to build a message of [`string`][js-string] type are proper. | + **Returns:** -The **return value** is an instance of an [`ValidationError`](#validationerror). +The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** @@ -1555,6 +1707,13 @@ MIT © angular-package ([license][error-license]) [prism-js]: https://prismjs.com/ +[ts-any]: https://www.typescriptlang.org/docs/handbook/basic-types.html#any +[ts-boolean]: https://www.typescriptlang.org/docs/handbook/basic-types.html#boolean [ts-classes]: https://www.typescriptlang.org/docs/handbook/2/classes.html [ts-function]: https://www.typescriptlang.org/docs/handbook/2/functions.html [ts-interface]: https://www.typescriptlang.org/docs/handbook/interfaces.html#our-first-interface +[ts-never]: https://www.typescriptlang.org/docs/handbook/basic-types.html#never +[ts-number]: https://www.typescriptlang.org/docs/handbook/basic-types.html#number +[ts-object]: https://www.typescriptlang.org/docs/handbook/basic-types.html#object +[ts-string]: https://www.typescriptlang.org/docs/handbook/basic-types.html#string +[ts-unknown]: https://www.typescriptlang.org/docs/handbook/basic-types.html#unknown From 6c5ee359b531c72a6159b3731dc250d6bb34524b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 01:12:35 +0200 Subject: [PATCH 047/179] docs(README.md): fix descriptions. --- README.md | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d6c56d5..62b969e 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Manages an [`Error`][js-error] of validation. | ValidationError. | Description | | :--------------------------------------------- | :---------- | -| [`template: string`](#validationerrortemplate) | A template of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]`. | +| [`template: string`](#validationerrortemplate) | A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** `[fix]` and `[problem]` tags and **optional** `[id]` and `[value]`. | **Instance properties:** @@ -149,7 +149,7 @@ Manages an [`Error`][js-error] of validation. | [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | | [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with required replaceable `[problem]` and `[fix]` tags and optionally `[id]` and`[value]` tag. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and`[value]` tags. | | [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. | [error-property-fix]: #validationerrorprototypefix @@ -206,7 +206,7 @@ Manages an [`Error`][js-error] of validation. [![update]][error-github-changelog] -A template of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]`. The value is being checked against the existence of `[problem]` and `[fix]` tags. It can be set directly or by the static [`setTemplate()`](#validationerrorsettemplate) methods. By default, it's set to `Problem: [problem] => Fix: [fix]`. +A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** `[fix]` and `[problem]` tags and **optional** `[id]` and `[value]`. The value is being checked against the existence of `[fix]` and `[problem]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the static [`ValidationError.setTemplate()`](#validationerrorsettemplate) method. By default, it's set to `Problem: [problem] => Fix: [fix]`. ```typescript static get template(): string { @@ -303,7 +303,7 @@ public set problem(value: string) { [![new]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. The value is being checked against the existence of required `[problem]` and `[fix]` tags. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. The value is being checked against the existence of required `[problem]` and `[fix]` tags. ```typescript public get template(): string { @@ -316,13 +316,13 @@ public set template(value: string) { **Tags:** -`[fix]`: **Required**, replace by the [`fix`][error-property-fix] property. +`[fix]`: **Required**, replaced by the [`fix`][error-property-fix] property. -`[id]`: Optional, replace by the [`id`][error-property-id] property. +`[id]`: Optional, replaced by the [`id`][error-property-id] property. -`[problem]`: **Required**, replace by the [`problem`][error-property-problem] property. +`[problem]`: **Required**, replaced by the [`problem`][error-property-problem] property. -`[value]`: Optional, replace by the [`value`][error-property-value] property. +`[value]`: Optional, replaced by the [`value`][error-property-value] property.
@@ -330,7 +330,13 @@ public set template(value: string) { [![new]][error-github-changelog] -The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. The conversion is performed only during [`message`][error-property-message] creation by providing the parser to the static [`defineMessage()`](#validationerrordefinemessage) or instance [`setMessage()`][error-method-setmessage] method and can be converted automatically by the defined [`function`][js-function] of the `ValueParser` type, set by the [`setValueParser()`][error-method-setvalueparser] method. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. +The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. + +**Conversion:** +The conversion is performed only during [`message`][error-property-message] creation and can be done in **two** ways, **automatic** or **manual**. + +* The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the static [`ValidationError.defineMessage()`](#validationerrordefinemessage) or [`setMessage()`][error-method-setmessage] method of an instance. +* The **automatic** by the defined function of the [`ValueParser`](#valueparser) type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method to automatize static [`ValidationError.defineMessage()`](#validationerrordefinemessage), and by the [`setValueParser()`][error-method-setvalueparser] method of an instance for automatize [`setMessage()`][error-method-setmessage]. ```typescript public get value(): any { @@ -349,7 +355,7 @@ public set value(value: any) { [![update]][error-github-changelog] -Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`template`](#validationerrortemplate). +Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). The **automatic** convert of the value is performed by the defined function of the [`ValueParser`](#valueparser) type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method. ```typescript // Syntax. @@ -370,7 +376,7 @@ public static defineMessage( | :---------------------------------------- | :---------- | | `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | -| `parser: ValueParser` | An optional [`function`][js-function] to convert property `value` from the provided [`ErrorMessage`](#errormessage) to [`string`][js-string] during message creation. It can be set by static [`setValueParser()`](#validationerrorsetvalueparser). | +| `parser: ValueParser` | An optional [`function`][js-function] to convert property `value` from the provided [`ErrorMessage`](#errormessage) to [`string`][js-string] during message creation. It can be set by static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser). | **Returns:** @@ -501,7 +507,7 @@ The **return value** is static [`ValidationError`](#validationerror). [![new]][error-github-changelog] -Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`defineMessage()`](#validationerrorsetvalueparser) method. +Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the default parser for the instance. ```typescript // Syntax. From 4597a69f5085fada414aed916df1e0ceaf155200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 01:31:32 +0200 Subject: [PATCH 048/179] docs(README.md): fix, and update tags. --- README.md | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 62b969e..072a715 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,16 @@ static set template(value: string) { } ``` +**Tags:** + +`[fix]`: **Required**, replaced by the [`fix`][error-property-fix] property. + +`[id]`: Optional, replaced by the [`id`][error-property-id] property. + +`[problem]`: **Required**, replaced by the [`problem`][error-property-problem] property. + +`[value]`: Optional, replaced by the [`value`][error-property-value] property. +
### `ValidationError` instance public properties @@ -225,7 +235,7 @@ static set template(value: string) { [![update]][error-github-changelog] -A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string]. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. +A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string]. It refers to **`[fix]`** tag. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. ```typescript public get fix(): string { @@ -303,7 +313,7 @@ public set problem(value: string) { [![new]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. The value is being checked against the existence of required `[problem]` and `[fix]` tags. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. The value is being checked against the existence of **required** `[problem]` and `[fix]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). ```typescript public get template(): string { @@ -316,13 +326,7 @@ public set template(value: string) { **Tags:** -`[fix]`: **Required**, replaced by the [`fix`][error-property-fix] property. - -`[id]`: Optional, replaced by the [`id`][error-property-id] property. - -`[problem]`: **Required**, replaced by the [`problem`][error-property-problem] property. - -`[value]`: Optional, replaced by the [`value`][error-property-value] property. +Exactly the same as with the static [`ValidationError.template`](#validationerrortemplate).
From 9093709b3c61c1da8ac5859841c1ad8f7692a5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 01:36:35 +0200 Subject: [PATCH 049/179] docs(README.md): fix the `value` property of an instance, update tags. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 072a715..1b1f724 100644 --- a/README.md +++ b/README.md @@ -317,16 +317,16 @@ A template of the [`error`][js-error] message guarded by [`string`][js-string] t ```typescript public get template(): string { - return this.#tpl; + return this.#template; } public set template(value: string) { - ValidationError.#guardTemplate(value) && (this.#tpl = value); + ValidationError.#guardTemplate(value) && (this.#template = value); } ``` **Tags:** -Exactly the same as with the static [`ValidationError.template`](#validationerrortemplate). +Equivalent to the static [`ValidationError.template`](#validationerrortemplate).
@@ -339,8 +339,8 @@ The value of [`any`][ts-any] type affected by the validation error, which must b **Conversion:** The conversion is performed only during [`message`][error-property-message] creation and can be done in **two** ways, **automatic** or **manual**. -* The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the static [`ValidationError.defineMessage()`](#validationerrordefinemessage) or [`setMessage()`][error-method-setmessage] method of an instance. -* The **automatic** by the defined function of the [`ValueParser`](#valueparser) type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method to automatize static [`ValidationError.defineMessage()`](#validationerrordefinemessage), and by the [`setValueParser()`][error-method-setvalueparser] method of an instance for automatize [`setMessage()`][error-method-setmessage]. +* The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the [`setMessage()`][error-method-setmessage] method. +* The **automatic** by the defined function of the [`ValueParser`](#valueparser) type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage]. ```typescript public get value(): any { From da6093fab1ac0bb0aaf55f85a8d9213d3170c571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 01:42:58 +0200 Subject: [PATCH 050/179] docs(README.md): update. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1b1f724..c60f831 100644 --- a/README.md +++ b/README.md @@ -1018,7 +1018,7 @@ import { ValidationError } from '@angular-package/error'; [![new]][error-github-changelog] -Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). +Sets the [`function`][js-function] to automatically convert [`any`][ts-any] [`value`][error-property-value] to the [`string`][js-string] during message creation. The [`function`][js-function] is used implicitly by the [`updateMessage()`][error-method-updatemessage] and with the parameter by [`setMessage()`][error-method-setmessage] method. ```typescript public setValueParser(parser: ValueParser): this { @@ -1031,7 +1031,7 @@ public setValueParser(parser: ValueParser): this { | Name: type | Description | | :-------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the `ValueParser` type, to convert any value to a [`string`][js-string]. | +| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type, to convert [`any`][ts-any] [`value`][error-property-value] to a [`string`][js-string]. | **Returns:** From 12f42945633d1e8172a585670839db63aa47bfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 01:50:46 +0200 Subject: [PATCH 051/179] docs(README.md): update. --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c60f831..9495756 100644 --- a/README.md +++ b/README.md @@ -775,7 +775,7 @@ import { ValidationError } from '@angular-package/error'; [![update]][error-github-changelog] -Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. +Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. ```typescript // Syntax. @@ -792,6 +792,7 @@ public setMessage( this.setFix(message.fix) .setId(message.id || this.#id) .setProblem(message.problem) + .setTemplate(message.template || this.#template) .setValue(message.value); return this; } @@ -801,9 +802,9 @@ public setMessage( | Name: type | Description | | :------------------------------------------ | :---------- | -| `message: ErrorMessage` | A [`string`][js-string] type or an [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. All properties are being assigned to the instance. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `message: ErrorMessage` | An [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. All properties are being assigned to the instance. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | -| `parser: ValueParser` | The [`function`][js-function] of the `ValueParser` type, to convert any value to a [`string`][js-string]. | +| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type, to **convert** [`any`][ts-any] value to a [`string`][js-string]. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | **Returns:** From a0668fd5329fb287e2ed116ce1d0ee5d390c9230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 02:06:54 +0200 Subject: [PATCH 052/179] docs(README.md): update. --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9495756..85230c4 100644 --- a/README.md +++ b/README.md @@ -359,7 +359,9 @@ public set value(value: any) { [![update]][error-github-changelog] -Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). The **automatic** convert of the value is performed by the defined function of the [`ValueParser`](#valueparser) type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method. +Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). + +> The **automatic** convert of the `value` of an [`ErrorMessage`](#errormessage) is performed by the defined function of the [`ValueParser`](#valueparser) type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method. ```typescript // Syntax. @@ -378,7 +380,7 @@ public static defineMessage( | Name: type | Description | | :---------------------------------------- | :---------- | -| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) by the static private `ValidationError.#guarMessage()` method. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | | `parser: ValueParser` | An optional [`function`][js-function] to convert property `value` from the provided [`ErrorMessage`](#errormessage) to [`string`][js-string] during message creation. It can be set by static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser). | @@ -528,7 +530,7 @@ public static setValueParser( | Name: type | Description | | :--------------------------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the `ValueParser` type to convert the value of [`any`][ts-any] type to [`string`][js-string]. | +| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type to convert the value of [`any`][ts-any] type to [`string`][js-string]. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the result of the check whether the provided `parser` is [`function`][js-function] type. | **Returns:** @@ -803,8 +805,8 @@ public setMessage( | Name: type | Description | | :------------------------------------------ | :---------- | | `message: ErrorMessage` | An [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. All properties are being assigned to the instance. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is [`string`][js-string] type or whether it's the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | -| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type, to **convert** [`any`][ts-any] value to a [`string`][js-string]. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | +| `parser: ValueParser` | An optional [`function`][js-function] of the [`ValueParser`](#valueparser) type, to **convert** [`any`][ts-any] value to a [`string`][js-string]. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | **Returns:** From 3d6407299930584a164a0771439be4e875beb709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 02:13:55 +0200 Subject: [PATCH 053/179] docs(README.md): fix. --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 85230c4..dedbd21 100644 --- a/README.md +++ b/README.md @@ -180,13 +180,13 @@ Manages an [`Error`][js-error] of validation. | :------------------------------------------------ | :---------- | | [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | | [`setId()`][error-method-setid] | Sets the [`id`][error-property-id] an identifier of the described [`problem`][error-property-problem]. | -| [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface or [`string`][js-string] type. | +| [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | | [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | | [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | -| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be parsed to [`string`][js-string] type). | -| [`setValueParser()`][error-method-setvalueparser] | Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] to the [`string`][js-string] during [`message`][error-property-message] creation. | -| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored [`fix`][error-property-fix], [`problem`][error-property-problem] and [`template`][error-property-template] or optionally from the provided `message`. | -| [`updateMessage()`][error-method-updatemessage] | Updates the [`message`][error-property-message] with a stored [`fix`][error-property-fix], [`problem`][error-property-problem], and [`template`][error-property-template]. | +| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]) type. | +| [`setValueParser()`][error-method-setvalueparser] | Sets the [`function`][js-function] to automatically **convert** [`any`][ts-any] [`value`][error-property-value] to the [`string`][js-string] during [`message`][error-property-message] creation. | +| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored required [`fix`][error-property-fix], [`problem`][error-property-problem] and optional [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. | +| [`updateMessage()`][error-method-updatemessage] | Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. | [error-method-setfix]: #validationerrorprototypesetfix [error-method-setid]: #validationerrorprototypesetid @@ -988,7 +988,7 @@ validationError.setTemplate(template, (result, value) => { [![new]][error-github-changelog] -Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). +Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]) type. ```typescript public setValue(value: any): this { @@ -1021,7 +1021,7 @@ import { ValidationError } from '@angular-package/error'; [![new]][error-github-changelog] -Sets the [`function`][js-function] to automatically convert [`any`][ts-any] [`value`][error-property-value] to the [`string`][js-string] during message creation. The [`function`][js-function] is used implicitly by the [`updateMessage()`][error-method-updatemessage] and with the parameter by [`setMessage()`][error-method-setmessage] method. +Sets the [`function`][js-function] to automatically **convert** [`any`][ts-any] [`value`][error-property-value] to the [`string`][js-string] during [`message`][error-property-message] creation. ```typescript public setValueParser(parser: ValueParser): this { @@ -1117,7 +1117,7 @@ validationError.throw({ fix, problem, template }); [![update]][error-github-changelog] -Updates the message with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. +Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. ```typescript public updateMessage(callback?: ResultCallback): this { From 6ae77f2ca5865613f573b72fdd85d8ae29ed00e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Thu, 7 Oct 2021 14:08:26 +0200 Subject: [PATCH 054/179] docs(README.md): update. --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dedbd21..38c5f09 100644 --- a/README.md +++ b/README.md @@ -144,11 +144,11 @@ Manages an [`Error`][js-error] of validation. | ValidationError.prototype. | Description | | :-------------------------------------------- | :---------- | -| [`fix: string`][error-property-fix] | A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] type. | -| [`id: number`][error-property-id] | The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type. | +| [`fix: string`][error-property-fix] | A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. | +| [`id: number`][error-property-id] | The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. | | [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | -| [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for a **required** tag **`[problem]`** of the [`template`][error-property-template]. | | [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and`[value]` tags. | | [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. | @@ -235,7 +235,7 @@ static set template(value: string) { [![update]][error-github-changelog] -A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string]. It refers to **`[fix]`** tag. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. +A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. ```typescript public get fix(): string { @@ -252,7 +252,7 @@ public set fix(value: string) { [![new]][error-github-changelog] -The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. +The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. ```typescript public get id(): number { @@ -296,7 +296,7 @@ public name = ValidationError.name; [![update]][error-github-changelog] -Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. It can be set directly or by the [`setProblem()`][error-method-setproblem], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method. By default, it's an empty [`string`][js-string]. +Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. It can be set directly or by the [`setProblem()`][error-method-setproblem], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method. By default, it's an empty [`string`][js-string]. ```typescript public get problem(): string { @@ -334,13 +334,13 @@ Equivalent to the static [`ValidationError.template`](#validationerrortemplate). [![new]][error-github-changelog] -The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. +The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. **Conversion:** The conversion is performed only during [`message`][error-property-message] creation and can be done in **two** ways, **automatic** or **manual**. * The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the [`setMessage()`][error-method-setmessage] method. -* The **automatic** by the defined function of the [`ValueParser`](#valueparser) type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage]. +* The **automatic** by the defined function of the [`ValueParser`](#valueparser) type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage] and [`updateMessage()`][error-method-updatemessage] methods. ```typescript public get value(): any { @@ -988,7 +988,7 @@ validationError.setTemplate(template, (result, value) => { [![new]][error-github-changelog] -Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]) type. +Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). ```typescript public setValue(value: any): this { From 47a88cc14fdca778335304cb15e6b368bbaa8d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 8 Oct 2021 21:44:48 +0200 Subject: [PATCH 055/179] docs(ValueParser): update description. --- src/type/value-parser.type.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type/value-parser.type.ts b/src/type/value-parser.type.ts index bbbbea3..31e13c5 100644 --- a/src/type/value-parser.type.ts +++ b/src/type/value-parser.type.ts @@ -1,4 +1,4 @@ /** - * The function to convert the value of any type to `string`. + * The type of function to convert the value of any type to `string`. */ export type ValueParser = (value: any) => string; From 8e0ef957f439966c4d9dc8bc2010b8c6106306f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 8 Oct 2021 21:45:47 +0200 Subject: [PATCH 056/179] docs(README.md): update. --- README.md | 657 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 381 insertions(+), 276 deletions(-) diff --git a/README.md b/README.md index 38c5f09..99cd8d0 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ Manages an [`Error`][js-error] of validation. | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | | [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for a **required** tag **`[problem]`** of the [`template`][error-property-template]. | | [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and`[value]` tags. | -| [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message]. | +| [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. | [error-property-fix]: #validationerrorprototypefix [error-property-id]: #validationerrorprototypeid @@ -178,7 +178,7 @@ Manages an [`Error`][js-error] of validation. | ValidationError.prototype. | Description | | :------------------------------------------------ | :---------- | -| [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | +| [`setFix(): this`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | | [`setId()`][error-method-setid] | Sets the [`id`][error-property-id] an identifier of the described [`problem`][error-property-problem]. | | [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | | [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | @@ -233,6 +233,8 @@ static set template(value: string) { #### `ValidationError.prototype.fix` +DONE + [![update]][error-github-changelog] A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. @@ -250,9 +252,11 @@ public set fix(value: string) { #### `ValidationError.prototype.id` +DONE + [![new]][error-github-changelog] -The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. +The **identifier** of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. ```typescript public get id(): number { @@ -267,6 +271,8 @@ public set id(value: number) { #### `ValidationError.prototype.message` +DONE + [![update]][error-github-changelog] A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. @@ -284,6 +290,8 @@ public set message(value: string) { #### `ValidationError.prototype.name` +DONE + [`Error`][js-error] name of [`string`][js-string] type that is being thrown. By default, it's ['ValidationError'](#validationerror). ```typescript @@ -294,6 +302,8 @@ public name = ValidationError.name; #### `ValidationError.prototype.problem` +DONE + [![update]][error-github-changelog] Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. It can be set directly or by the [`setProblem()`][error-method-setproblem], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method. By default, it's an empty [`string`][js-string]. @@ -311,9 +321,11 @@ public set problem(value: string) { #### `ValidationError.prototype.template` +DONE + [![new]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. The value is being checked against the existence of **required** `[problem]` and `[fix]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. The value is being checked against the existence of **required** `[problem]` and `[fix]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). By default, it's set to `Problem: [problem] => Fix: [fix]`. ```typescript public get template(): string { @@ -332,6 +344,8 @@ Equivalent to the static [`ValidationError.template`](#validationerrortemplate). #### `ValidationError.prototype.value` +DONE + [![new]][error-github-changelog] The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. @@ -339,8 +353,8 @@ The value of [`any`][ts-any] type affected by the validation error, which must b **Conversion:** The conversion is performed only during [`message`][error-property-message] creation and can be done in **two** ways, **automatic** or **manual**. -* The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the [`setMessage()`][error-method-setmessage] method. -* The **automatic** by the defined function of the [`ValueParser`](#valueparser) type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage] and [`updateMessage()`][error-method-updatemessage] methods. +* The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the [`setMessage()`][error-method-setmessage] method of an instance. +* The **automatic** by the previously defined function of the [`ValueParser`](#valueparser) type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage] method. ```typescript public get value(): any { @@ -357,6 +371,8 @@ public set value(value: any) { #### `ValidationError.defineMessage()` +DONE + [![update]][error-github-changelog] Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). @@ -367,10 +383,14 @@ Defines the validation [`error`][js-error] message of [`string`][js-string] type // Syntax. public static defineMessage( message: ErrorMessage, - callback?: ResultCallback, + callback: ResultCallback = (result) => result, parser: ValueParser = this.#defaultValueParser ): string { - return this.#guardMessage(message, callback) + return callback( + this.#guardMessage(message) && guardFunction(parser), + message, + this + ) ? this.#buildMessage(message, parser) : ''; } @@ -381,8 +401,8 @@ public static defineMessage( | Name: type | Description | | :---------------------------------------- | :---------- | | `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) by the static private `ValidationError.#guarMessage()` method. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | -| `parser: ValueParser` | An optional [`function`][js-function] to convert property `value` from the provided [`ErrorMessage`](#errormessage) to [`string`][js-string] during message creation. It can be set by static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser). | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags and parser is [`function`][js-funciton] type. | +| `parser: ValueParser` | An optional [`function`][js-function] to **convert** property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during [`message`][error-property-message] creation. It can be set by static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) as the default value parser for static and instance [`ValidationError`](#validationerror). | **Returns:** @@ -394,78 +414,69 @@ The **return value** is a message of [`string`][js-string] type created from the // Example usage. import { ValidationError } from '@angular-package/error'; -const fix = 'There is no solution to the described problem.'; -const problem = 'The problem has no solution.'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); /* Returns - -------- - Problem: The problem has no solution. => Fix: There is no solution - to the described problem. + ------- + Problem(VE427): The value must be a string type. + Got: Symbol(123) + Fix: Provide string type value. Read more: https://duckduckgo.com/ */ -const errorMessage = ValidationError.defineMessage({ fix, problem }); -``` +ValidationError.defineMessage({ fix, id, problem, value, template }); -```typescript +// Define with only required tags. /* - Example usage: create an error message of a string type - from the provided object with a different template. + Returns + ------- + Problem: The value must be a string type. => Fix: Provide string type value. Read more: https://duckduckgo.com/ */ -import { ValidationError } from '@angular-package/error'; +ValidationError.defineMessage({ fix, problem }); -const fix = 'There is no solution to the described problem.'; -const problem = 'The problem has no solution.'; -const template = `[problem] ... [fix]`; +// Define the template in static `ValidationError`. +ValidationError.template = template; +// Define the message again without `template`. /* Returns - -------- - The problem has no solution. ... There is no solution to the described problem. -*/ -const errorMessage = ValidationError.defineMessage({ - fix, problem, template -}); -``` - -```typescript -/* - Example usage: create an error message of a string type - from the provided object and the changed template. + ------- + Problem(VE427): The value must be a string type. + Got: Symbol(123) + Fix: Provide string type value. Read more: https://duckduckgo.com/ */ -import { ValidationError } from '@angular-package/error'; - -const fix = 'There is no solution to the described problem.'; -const problem = 'The problem has no solution.'; - -// Change the template by directly assign a new value. -ValidationError.template = `\nPROBLEM: [problem]\nFIX: [fix] `; +ValidationError.defineMessage({ fix, id, problem, value }); +// Define with only required tags. /* Returns ------- - PROBLEM: The problem has no solution. - FIX: There is no solution to the described problem. + Problem(VE): The value must be a string type. + Got: undefined + Fix: Provide string type value. Read more: https://duckduckgo.com/ */ -const errorMessage = ValidationError.defineMessage({ fix, problem }); -``` +ValidationError.defineMessage({ fix, problem }); -```typescript +// Define with the callback and value parser. /* - Example usage: create an error message of a string type - from the provided object and the changed template. + Returns + ------- + Problem(VE427): The value must be a string type. + Got: 12345 + Fix: Provide string type value. Read more: https://duckduckgo.com/ */ -import { ValidationError } from '@angular-package/error'; - -const fix = 'There is no solution to the described problem.'; -const problem = 'The problem has no solution.'; - -const errorMessage = ValidationError.defineMessage( - { fix, problem }, - (result, value) => { - // Do something with the `result` of the `message` check - // and `value`. +ValidationError.defineMessage( + { fix, id, problem, value, template }, + (result, value, payload) => { + result // true + value // {fix ... } the given message. return result; - } + }, + (value) => String(12345) // Should return converted value instead of 12345. ); ``` @@ -473,9 +484,11 @@ const errorMessage = ValidationError.defineMessage( #### `ValidationError.setTemplate()` +DONE + [![new]][error-github-changelog] -Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the default value for [`template`][error-property-template] of an instance. +Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and the default value for [`template`][error-property-template] of an instance. ```typescript // Syntax. @@ -504,13 +517,38 @@ The **return value** is static [`ValidationError`](#validationerror). ```typescript // Example usage. +import { ValidationError } from '@angular-package/error'; + +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); +/* + Returns + ------- + Problem(VE427): The value must be a string type. + Got: Symbol(123) + Fix: Provide string type value. Read more: https://duckduckgo.com/ +*/ +ValidationError + .setTemplate(template, (result) => result) + .defineMessage({ + fix, + id, + problem, + value, + }); ```
#### `ValidationError.setValueParser()` +DONE + [![new]][error-github-changelog] Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the default parser for the instance. @@ -541,6 +579,31 @@ The **return value** is static [`ValidationError`](#validationerror). ```typescript // Example usage. +import { ValidationError } from '@angular-package/error'; + +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); + +/* + Returns + ------- + Problem(VE427): The value must be a string type. + Got: 12345 + Fix: Provide string type value. Read more: https://duckduckgo.com/ +*/ +ValidationError + .setValueParser((value: any) => String(12345), (result) => result) + .defineMessage({ + fix, + id, + problem, + value, + template + }); ```
@@ -549,25 +612,31 @@ The **return value** is static [`ValidationError`](#validationerror). #### `ValidationError()` +DONE + [![update]][error-github-changelog] -Creates a new instance with the [`message`][error-property-message]. If the provided [`message`][error-property-message] is an [`object`][js-object], then its properties are assigned to the instance. +Creates a new instance with an optional [`message`][error-property-message] of [`ErrorMessage`](#errormessage) interface. ```typescript // Syntax. -constructor(message: string | ErrorMessage = '') { +constructor(message?: ErrorMessage, parser?: ValueParser) { super(); + // Sets the parser. + isDefined(parser) && this.setValueParser(parser); + // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. - this.setMessage(message); + isDefined(message) && this.setMessage(message, undefined, parser); } ``` **Parameters:** -| Name: type | Description | -| :-------------------------------- | :---------- | -| `message: string \| ErrorMessage` | The message of [`string`][js-string] type or of an [`ErrorMessage`](#errormessage) interface that is used to throw with an [`Error`][js-error]. | +| Name: type | Description | +| :----------------------- | :---------- | +| `message?: ErrorMessage` | An optional `message` of an [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a [`message`][error-property-message] of [`string`][js-string] and throw with an [`Error`][js-error]. Its properties are assigned to the instance. | +| `parser: ValueParser` | An optional [`function`][js-function] to **convert** the property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during message creation and set as the **default** value parser for the instance. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | **Returns:** @@ -579,10 +648,21 @@ The **return value** is an instance of [`ValidationError`](#validationerror). // Example usage. import { ValidationError } from '@angular-package/error'; -const fix = 'There is no solution to the described problem.'; -const problem = 'The problem has no solution.'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); -const validationError = new ValidationError({ fix, problem }); +/* + Returns + ------- + ValidationError: Problem(VE427): The value must be a string type. + Got: Symbol(123) + Fix: Provide string type value. Read more: https://duckduckgo.com/ +*/ +const validationError = new ValidationError({ fix, id, problem, template, value }); ``` ```typescript @@ -591,39 +671,33 @@ import { ValidationError } from '@angular-package/error'; import { Callback, ErrorMessage } from '@angular-package/callback'; import { ResultCallback } from '@angular-package/type'; -// Define a fix. -const fix = 'There is no solution to the described problem.'; - -// Define a problem. -const problem = 'The problem has no solution.'; - -// Define a template. -const template = 'PROBLEM: [problem] FIX: [fix]'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; class CustomError { #callback = new Callback('setFix', 'setMessage', 'setProblem', 'setTemplate'); #validationError = new ValidationError(); constructor( - message: string | ErrorMessage, + message?: ErrorMessage, callback?: ( callback: Callback<'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'> ) => void ) { - if (callback) { - callback(this.#callback); - } - - this.#validationError.setMessage(message); + callback && callback(this.#callback); + message && this.#validationError.setMessage(message); } public setMessage( - message: string | ErrorMessage, + message: ErrorMessage, callback: ResultCallback = this.#callback.getResultCallback( 'setMessage' ) - ) { + ): this { this.#validationError.setMessage(message, callback); + return this; } public throw(): void { @@ -631,7 +705,7 @@ class CustomError { } } -const customError = new CustomError('', (callback) => +const customError = new CustomError(undefined, (callback) => callback .setResultCallback('setFix', (result, value) => console.log(`setFix`, result, value) @@ -655,18 +729,11 @@ const customError = new CustomError('', (callback) => /** * ! Console returns - * ? Checks message if string. - * setMessage false + * setMessage true * { - * fix: 'There is no solution to the described problem.', - * problem: 'The problem has no solution.', template: 'PROBLEM: [problem] FIX: [fix]' - * } - * - * ? Checks message is an object. - * setMessage false - * { - * fix: 'There is no solution to the described problem.', - * problem: 'The problem has no solution.', template: 'PROBLEM: [problem] FIX: [fix]' + * fix: 'Provide string type value. Read more: https://duckduckgo.com/', + * problem: 'The value must be a string type.', + * template: 'Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]' * } */ customError.setMessage({ fix, problem, template }); @@ -676,6 +743,8 @@ customError.setMessage({ fix, problem, template }); ### `ValidationError` instance public methods +DONE + #### `ValidationError.prototype.setFix()` [![update]][error-github-changelog] @@ -694,7 +763,7 @@ public setFix(fix: string, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the `[fix]` tag of [`template`][error-property-template]. | | `callback?: ResultCallback` | 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]. | **Returns:** @@ -707,13 +776,13 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). // Example usage. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + // Initialize an instance. const validationError = new ValidationError(); -// Define a fix. -const fix = 'There is no solution to the described problem.'; - -// Returns 'There is no solution to the described problem.' +// Returns 'Provide string type value. Read more: https://duckduckgo.com/' validationError.setFix(fix).fix; ``` @@ -721,17 +790,17 @@ validationError.setFix(fix).fix; // Example usage with a callback. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + // Initialize an instance. const validationError = new ValidationError(); -// Define a fix. -const fix = 'There is no solution to the described problem.'; - // Set the fix and handle the check of it with a callback. validationError.setFix(fix, (result, value) => { // Returns `true`. result; - // Returns `There is no solution to the described problem.`. + // Returns 'Provide string type value. Read more: https://duckduckgo.com/' value; return result; }); @@ -741,14 +810,16 @@ validationError.setFix(fix, (result, value) => { #### `ValidationError.prototype.setId()` +DONE + [![new]][error-github-changelog] -Sets the [`id`][error-property-id] an identifier of the described [`problem`][error-property-problem]. +Sets the [`id`][error-property-id] an **identifier** of the described [`problem`][error-property-problem]. ```typescript // Syntax. -public setFix(fix: string, callback?: ResultCallback): this { - guardString(fix, callback) && (this.#fix = fix); +public setId(id: number, callback?: ResultCallback): this { + guardNumber(id, callback) && (this.#id = id); return this; } ``` @@ -757,8 +828,8 @@ public setFix(fix: string, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `fix: string` | The identifier, guarded by [`number`][js-number] type. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `id` is [`number`][js-number]. | +| `id: number` | The identifier, guarded by [`number`][js-number] type and a replacement to the `[id]` tag of [`template`][error-property-template]. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `id` is [`number`][js-number]. | **Returns:** @@ -767,14 +838,31 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** ```typescript -// Example usage. +// Example usage with a callback. import { ValidationError } from '@angular-package/error'; + +// Prepare the values. +const id = 427; + +// Initialize an instance. +const validationError = new ValidationError(); + +// Set the id and handle the check of it with a callback. +validationError.setId(id, (result, value) => { + // Returns `true`. + result; + // Returns 427 + value; + return result; +}).id; // Returns 427 ```
#### `ValidationError.prototype.setMessage()` +DONE + [![update]][error-github-changelog] Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. @@ -786,16 +874,15 @@ public setMessage( callback?: ResultCallback, parser: ValueParser = this.#valueParser ): this { - (super.message = ValidationError.defineMessage( + super.message = ValidationError.defineMessage( { ...message, ...{ template: message.template || this.#template } }, - callback, + (result, value, payload) => ( + isDefined(callback) && callback(result, value, payload), + isTrue(result) && this.assignMessageProperties(message), + result + ), parser - )) && - this.setFix(message.fix) - .setId(message.id || this.#id) - .setProblem(message.problem) - .setTemplate(message.template || this.#template) - .setValue(message.value); + ); return this; } ``` @@ -804,9 +891,9 @@ public setMessage( | Name: type | Description | | :------------------------------------------ | :---------- | -| `message: ErrorMessage` | An [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. All properties are being assigned to the instance. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains required [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the optional [`template`][error-property-template] property has `[problem]` and `[fix]` tags. | -| `parser: ValueParser` | An optional [`function`][js-function] of the [`ValueParser`](#valueparser) type, to **convert** [`any`][ts-any] value to a [`string`][js-string]. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | +| `message: ErrorMessage` | An [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) and if it's a proper object then its properties are being assigned to the instance. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags and `parser` is a function type. | +| `parser: ValueParser` | An optional [`function`][js-function] to **convert** property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during message creation. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | **Returns:** @@ -818,43 +905,45 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). // Example usage with a callback. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); + // Initialize an instance. const validationError = new ValidationError(); -// Define a fix. -const fix = 'There is no solution to the described problem.'; - -// Define a problem. -const problem = 'The problem has no solution.'; - -// Define a template. -const template = 'PROBLEM: [problem], FIX: [fix]'; - // Set the message and handle the check of it with a callback. -validationError.setMessage({ fix, problem }, (result, value) => { - // Returns `false` then `true`. +validationError.setMessage({ fix, id, problem, template, value }, (result, value) => { + // Returns `true`. result; /* - Returns { - "fix": "There is no solution to the described problem.", - "problem": "The problem has no solution.", - "template": "PROBLEM: [problem] FIX: [fix]" - } + { + fix: 'Provide string type value. Read more: https://duckduckgo.com/', id: 427, + problem: 'The value must be a string type.', + template: 'Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]', value: Symbol(123)} */ value; return result; -}); +}, (value) => String(12345)); /* Returns - PROBLEM: The problem has no solution. FIX: There is no solution to the described problem. + ------- + Problem(VE427): The value must be a string type. + Got: 12345 + Fix: Provide string type value. Read more: https://duckduckgo.com/ */ -console.log(validationError.message); +validationError.message; ```
#### `ValidationError.prototype.setProblem()` +DONE + [![update]][error-github-changelog] Sets the description of the validation [`problem`][error-property-problem]. @@ -870,7 +959,7 @@ public setProblem(problem: string, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `problem: string` | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type. | +| `problem: string` | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the `[problem]` tag of [`template`][error-property-template]. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. | **Returns:** @@ -879,44 +968,32 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Initialize an instance. -const validationError = new ValidationError(); - -// Define a problem. -const problem = 'The problem has no solution.'; - -// Returns 'The problem has no solution.' -validationError.setProblem(problem).problem; -``` - ```typescript // Example usage with a callback. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const problem = 'The value must be a string type.'; + // Initialize an instance. const validationError = new ValidationError(); -// Define a problem. -const problem = 'The problem has no solution.'; - -// Set the problem and handle the check of it with a callback. +// Returns 'The value must be a string type.' validationError.setProblem(problem, (result, value) => { // Returns `true`. result; - // Returns 'The problem has no solution.' + // Returns 'The value must be a string type.' value; return result; -}); +}).problem; ```
#### `ValidationError.prototype.setTemplate()` +DONE + [![update]][error-github-changelog] Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. @@ -927,7 +1004,7 @@ public setTemplate( callback?: ResultCallback ): this { ValidationError.#guardTemplate(template, callback) && - (this.#tpl = template); + (this.#template = template); return this; } ``` @@ -945,47 +1022,38 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). **Usage:** -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Initialize an instance. -const validationError = new ValidationError(); - -// Define a template. -const template = 'PROBLEM: [problem], FIX: [fix]'; - -// Set the template. -validationError.setTemplate(template); - -// Returns 'PROBLEM: [problem], FIX: [fix]' -validationError.template; -``` - ```typescript // Example usage with a callback. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; + // Initialize an instance. const validationError = new ValidationError(); -// Define a template. -const template = 'PROBLEM: [problem], FIX: [fix]'; - -// Set the template and handle the check of it with a callback. +/* + Returns + ------- + Problem(VE[id]): [problem] + Got: [value] + Fix: [fix] +*/ validationError.setTemplate(template, (result, value) => { // Returns `true`. result; - // Returns 'PROBLEM: [problem], FIX: [fix]' + // Returns 'Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]' value; return result; -}); +}).template; ```
#### `ValidationError.prototype.setValue()` +DONE + [![new]][error-github-changelog] Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). @@ -1013,28 +1081,42 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). // Example usage. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const value = Symbol(123); + +// Initialize an instance. +const validationError = new ValidationError(); + +// Symbol(123) +validationError.setValue(value).value; ```
#### `ValidationError.prototype.setValueParser()` +DONE + [![new]][error-github-changelog] -Sets the [`function`][js-function] to automatically **convert** [`any`][ts-any] [`value`][error-property-value] to the [`string`][js-string] during [`message`][error-property-message] creation. +Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] of [`any`][ts-any] type to the [`string`][js-string] during [`message`][error-property-message] creation. The defined [`function`][js-function] is being used implicitly by the [`updateMessage()`][error-method-updatemessage] and [`throw()`][error-method-throw] methods and by the [`setMessage()`][error-method-setmessage] method as the default value of parameter `parser`. ```typescript -public setValueParser(parser: ValueParser): this { - guardFunction(parser) && (this.#valueParser = parser); +public setValueParser( + parser: ValueParser, + callback?: ResultCallback +): this { + guardFunction(parser, callback) && (this.#valueParser = parser); return this; } ``` **Parameters:** -| Name: type | Description | -| :-------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type, to convert [`any`][ts-any] [`value`][error-property-value] to a [`string`][js-string]. | +| Name: type | Description | +| :--------------------------------------- | :---------- | +| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type, to **convert** the [`value`][error-property-value] of [`any`][ts-any] type to [`string`][js-string]. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `parser` is [`function`][js-function]. | **Returns:** @@ -1046,20 +1128,53 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). // Example usage. import { ValidationError } from '@angular-package/error'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); + +// Initialize an instance. +const validationError = new ValidationError(); + +// Symbol(123) +validationError.setValueParser(value => String(12345)); + +// Set the message with predefined parser, the value instead of Symbol(123) is 12345 +/* + Returns + ------- + Problem(VE427): The value must be a string type. + Got: 12345 + Fix: Provide string type value. Read more: https://duckduckgo.com/ +*/ +validationError.setMessage({ fix, id, problem, value, template }).message; ```
#### `ValidationError.prototype.throw()` +DONE + [![update]][error-github-changelog] -Throws an error of [`ValidationError`](#validationerror) with the message built from the stored required [`fix`][error-property-fix], [`problem`][error-property-problem] and optional [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. The provided `message` of the [`ErrorMessage`](#errormessage) is set to the instance. If the `message` parameter is not provided, then if the `problem` of an instance of `ValidationError` is not an empty [`string`][js-string] it builds and overwrites the `message`. +Throws an error of new instance [`ValidationError`](#validationerror) with the message built from the stored **required** [`fix`][error-property-fix], [`problem`][error-property-problem] and **optional** [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. ```typescript public throw(message?: ErrorMessage): void { isDefined(message) ? this.setMessage(message) : this.updateMessage(); - throw this; + throw new ValidationError( + { + fix: this.#fix, + id: this.#id, + problem: this.#problem, + template: this.#template, + value: this.#value, + }, + this.#valueParser + ); } ``` @@ -1067,7 +1182,7 @@ public throw(message?: ErrorMessage): void { | Name: type | Description | | :----------------------- | :---------- | -| `message?: ErrorMessage` | An optional [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage). | +| `message?: ErrorMessage` | An optional [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) and if it's a proper [`object`][js-object] then its properties are being assigned to the instance. | **Usage:** @@ -1075,46 +1190,43 @@ public throw(message?: ErrorMessage): void { // Example usage. import { ValidationError } from '@angular-package/error'; -// Define a fix. -const fix = 'There is no solution to the described problem.'; - -// Define a problem. -const problem = 'The problem has no solution.'; - -// Define a template. -const template = 'PROBLEM: [problem] FIX: [fix]'; - -// Initialize an instance. -const validationError = new ValidationError({ fix, problem, template }); - -// Throw an error. -validationError.throw(); -``` - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Define a fix. -const fix = 'There is no solution to the described problem.'; - -// Define a problem. -const problem = 'The problem has no solution.'; - -// Define a template. -const template = 'PROBLEM: [problem] FIX: [fix]'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); // Initialize an instance. -const validationError = new ValidationError(); +const validationError = new ValidationError({ fix, id, problem, value, template }); -// Throw an error with message. -validationError.throw({ fix, problem, template }); +// throw +try { + validationError.throw(); +} catch (e) { + // Provide string type value. Read more: https://duckduckgo.com/ + console.log(e.fix); + // 427 + console.log(e.id); + // The value must be a string type. + console.log(e.problem); + // Symbol(123) + console.log(e.value); + /* + Problem(VE[id]): [problem] + Got: [value] + Fix: [fix] + */ + console.log(e.template); +} ```
#### `ValidationError.prototype.updateMessage()` +DONE + [![update]][error-github-changelog] Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. @@ -1152,20 +1264,23 @@ The **return value** is an instance of a [`ValidationError`](#validationerror). // Example usage. import { ValidationError } from '@angular-package/error'; -// Define a fix. -const fix = 'There is no solution to the described problem.'; - -// Define a problem. -const problem = 'The problem has no solution.'; - -// Define a template. -const template = 'PROBLEM: [problem] FIX: [fix]'; +// Prepare the values. +const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; +const id = 427; +const problem = 'The value must be a string type.'; +const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; +const value = Symbol(123); // Initialize an instance. const validationError = new ValidationError(); // Sets defined above fix, problem, and template. -validationError.setProblem(problem).setFix(fix).setTemplate(template); +validationError + .setFix(fix) + .setId(id) + .setProblem(problem) + .setTemplate(template) + .setValue(value); // Returns empty string. validationError.message; @@ -1175,7 +1290,10 @@ validationError.updateMessage(); /* Returns - PROBLEM: The problem has no solution. FIX: There is no solution to the described problem. + ------- + Problem(VE427): The value must be a string type. + Got: Symbol(123) + Fix: Provide string type value. Read more: https://duckduckgo.com/ */ validationError.message; @@ -1188,7 +1306,7 @@ throw validationError;
-### Another example usage of `ValidationError` +### More example usage of `ValidationError` ```typescript // Example usage. @@ -1239,14 +1357,13 @@ addPerson({ // Example usage. import { ValidationError } from '@angular-package/error'; +// Initialize an instance. const validationError = new ValidationError(); try { - validationError - .setMessage('message of validation error') - .throw(); + validationError.throw(); } catch (e) { - // message of validation error + // Problem: => Fix: console.log(e.message); // Empty string. console.log(e.problem); @@ -1256,7 +1373,6 @@ try { try { validationError - .setMessage('message of validation error') .setProblem('my problem') .setFix('my fix') .throw(); @@ -1269,21 +1385,6 @@ try { console.log(e.fix); } -try { - validationError - .setMessage('message of validation error') - .setProblem('my problem') - .setFix('my fix') - .setMessage('message of validation error') - .throw(); -} catch (e) { - // message of validation error - console.log(e.message); - // Empty string - console.log(e.problem); - // Empty string - console.log(e.fix); -} try { validationError @@ -1297,18 +1398,6 @@ try { // my fix console.log(e.fix); } - -try { - validationError - .throw({ problem: 'my problem', fix: 'my fix'}); -} catch (e) { - // Problem: my problem => Fix: my fix - console.log(e.message); - // my problem - console.log(e.problem); - // my fix - console.log(e.fix); -} ```
@@ -1322,9 +1411,10 @@ The shape of an [`object`][js-object] for an [`error`][js-error] message that co ```typescript export interface ErrorMessage { fix: string; + id?: number; problem: string; template?: string; - value?: string; + value?: any; } ``` @@ -1333,15 +1423,30 @@ export interface ErrorMessage { **`fix: string`** A possible solution to the described problem of a [`string`][js-string] type. +**`id: number`** +An optional **identifier** of the described problem. + **`problem: string`** -Description of validation problem of a [`string`][js-string] type. +Description of validation problem of a [`string`][js-string] type. **`template?: string`** -An optional error message template of a [`string`][js-string] type. +An optional error message template of a [`string`][js-string] type. **`value?: string`** [![new]][error-github-changelog] -An optional value affected by the validation error, which must be parsed to [`string`][js-string]. +An optional value affected by the validation error, which must be converted to [`string`][js-string]. + +
+ +## Type + +#### `ValueParser` + +The type of [`function`][js-function] to convert the value of [`any`][ts-any] type to a [`string`][js-string]. + +```typescript +type ValueParser = (value: any) => string; +```
From 949c392b72502234cc885c0adcde049e90178354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 8 Oct 2021 21:53:44 +0200 Subject: [PATCH 057/179] docs(README.md): update. --- README.md | 62 ++++++++++--------------------------------------------- 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 99cd8d0..d264512 100644 --- a/README.md +++ b/README.md @@ -145,11 +145,11 @@ Manages an [`Error`][js-error] of validation. | ValidationError.prototype. | Description | | :-------------------------------------------- | :---------- | | [`fix: string`][error-property-fix] | A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. | -| [`id: number`][error-property-id] | The identifier of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. | +| [`id: number`][error-property-id] | The **identifier** of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. | | [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | -| [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for a **required** tag **`[problem]`** of the [`template`][error-property-template]. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and`[value]` tags. | +| [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. | | [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. | [error-property-fix]: #validationerrorprototypefix @@ -166,7 +166,7 @@ Manages an [`Error`][js-error] of validation. | :--------------------------------------------------- | :---------- | | [`defineMessage()`](#validationerrordefinemessage) | Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | | [`setTemplate()`](#validationerrorsettemplate) | Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the **default** value for [`template`][error-property-template] of an instance. | -| [`setValueParser()`](#validationerrorsetvalueparser) | Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation performed by the static [`defineMessage()`](#validationerrorsetvalueparser) method. | +| [`setValueParser()`](#validationerrorsetvalueparser) | Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation performed by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the default parser for the instance. | **Constructor:** @@ -178,14 +178,14 @@ Manages an [`Error`][js-error] of validation. | ValidationError.prototype. | Description | | :------------------------------------------------ | :---------- | -| [`setFix(): this`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | -| [`setId()`][error-method-setid] | Sets the [`id`][error-property-id] an identifier of the described [`problem`][error-property-problem]. | +| [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | +| [`setId()`][error-method-setid] | Sets the [`id`][error-property-id] an **identifier** of the described [`problem`][error-property-problem]. | | [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | -| [`setProblem()`][error-method-setproblem] | Sets description [`problem`][error-property-problem] of a validation. | +| [`setProblem()`][error-method-setproblem] | Sets the description of the validation [`problem`][error-property-problem]. | | [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | -| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]) type. | -| [`setValueParser()`][error-method-setvalueparser] | Sets the [`function`][js-function] to automatically **convert** [`any`][ts-any] [`value`][error-property-value] to the [`string`][js-string] during [`message`][error-property-message] creation. | -| [`throw()`][error-method-throw] | Throws an error of [`ValidationError`](#validationerror) with the message built from the stored required [`fix`][error-property-fix], [`problem`][error-property-problem] and optional [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. | +| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). | +| [`setValueParser()`][error-method-setvalueparser] | Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] of [`any`][ts-any] type to the [`string`][js-string] during [`message`][error-property-message] creation. | +| [`throw()`][error-method-throw] | Throws an error of new instance [`ValidationError`](#validationerror) with the message built from the stored **required** [`fix`][error-property-fix], [`problem`][error-property-problem] and **optional** [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. | | [`updateMessage()`][error-method-updatemessage] | Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. | [error-method-setfix]: #validationerrorprototypesetfix @@ -233,8 +233,6 @@ static set template(value: string) { #### `ValidationError.prototype.fix` -DONE - [![update]][error-github-changelog] A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. @@ -252,8 +250,6 @@ public set fix(value: string) { #### `ValidationError.prototype.id` -DONE - [![new]][error-github-changelog] The **identifier** of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. @@ -271,8 +267,6 @@ public set id(value: number) { #### `ValidationError.prototype.message` -DONE - [![update]][error-github-changelog] A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. @@ -290,8 +284,6 @@ public set message(value: string) { #### `ValidationError.prototype.name` -DONE - [`Error`][js-error] name of [`string`][js-string] type that is being thrown. By default, it's ['ValidationError'](#validationerror). ```typescript @@ -302,8 +294,6 @@ public name = ValidationError.name; #### `ValidationError.prototype.problem` -DONE - [![update]][error-github-changelog] Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. It can be set directly or by the [`setProblem()`][error-method-setproblem], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method. By default, it's an empty [`string`][js-string]. @@ -321,8 +311,6 @@ public set problem(value: string) { #### `ValidationError.prototype.template` -DONE - [![new]][error-github-changelog] A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. The value is being checked against the existence of **required** `[problem]` and `[fix]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). By default, it's set to `Problem: [problem] => Fix: [fix]`. @@ -344,8 +332,6 @@ Equivalent to the static [`ValidationError.template`](#validationerrortemplate). #### `ValidationError.prototype.value` -DONE - [![new]][error-github-changelog] The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. @@ -371,8 +357,6 @@ public set value(value: any) { #### `ValidationError.defineMessage()` -DONE - [![update]][error-github-changelog] Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). @@ -484,11 +468,9 @@ ValidationError.defineMessage( #### `ValidationError.setTemplate()` -DONE - [![new]][error-github-changelog] -Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and the default value for [`template`][error-property-template] of an instance. +Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the **default** value for [`template`][error-property-template] of an instance. ```typescript // Syntax. @@ -547,8 +529,6 @@ ValidationError #### `ValidationError.setValueParser()` -DONE - [![new]][error-github-changelog] Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the default parser for the instance. @@ -612,8 +592,6 @@ ValidationError #### `ValidationError()` -DONE - [![update]][error-github-changelog] Creates a new instance with an optional [`message`][error-property-message] of [`ErrorMessage`](#errormessage) interface. @@ -743,8 +721,6 @@ customError.setMessage({ fix, problem, template }); ### `ValidationError` instance public methods -DONE - #### `ValidationError.prototype.setFix()` [![update]][error-github-changelog] @@ -810,8 +786,6 @@ validationError.setFix(fix, (result, value) => { #### `ValidationError.prototype.setId()` -DONE - [![new]][error-github-changelog] Sets the [`id`][error-property-id] an **identifier** of the described [`problem`][error-property-problem]. @@ -861,8 +835,6 @@ validationError.setId(id, (result, value) => { #### `ValidationError.prototype.setMessage()` -DONE - [![update]][error-github-changelog] Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. @@ -942,8 +914,6 @@ validationError.message; #### `ValidationError.prototype.setProblem()` -DONE - [![update]][error-github-changelog] Sets the description of the validation [`problem`][error-property-problem]. @@ -992,8 +962,6 @@ validationError.setProblem(problem, (result, value) => { #### `ValidationError.prototype.setTemplate()` -DONE - [![update]][error-github-changelog] Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. @@ -1052,8 +1020,6 @@ validationError.setTemplate(template, (result, value) => { #### `ValidationError.prototype.setValue()` -DONE - [![new]][error-github-changelog] Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). @@ -1095,8 +1061,6 @@ validationError.setValue(value).value; #### `ValidationError.prototype.setValueParser()` -DONE - [![new]][error-github-changelog] Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] of [`any`][ts-any] type to the [`string`][js-string] during [`message`][error-property-message] creation. The defined [`function`][js-function] is being used implicitly by the [`updateMessage()`][error-method-updatemessage] and [`throw()`][error-method-throw] methods and by the [`setMessage()`][error-method-setmessage] method as the default value of parameter `parser`. @@ -1156,8 +1120,6 @@ validationError.setMessage({ fix, id, problem, value, template }).message; #### `ValidationError.prototype.throw()` -DONE - [![update]][error-github-changelog] Throws an error of new instance [`ValidationError`](#validationerror) with the message built from the stored **required** [`fix`][error-property-fix], [`problem`][error-property-problem] and **optional** [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. @@ -1225,8 +1187,6 @@ try { #### `ValidationError.prototype.updateMessage()` -DONE - [![update]][error-github-changelog] Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. From 0ddcf623a78587f1ae2201174cc8b2060d8ad7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Fri, 8 Oct 2021 22:08:49 +0200 Subject: [PATCH 058/179] docs(README.md): update and fix. --- README.md | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d264512..6885c87 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Manages an [`Error`][js-error] of validation. | ValidationError. | Description | | :--------------------------------------------- | :---------- | -| [`template: string`](#validationerrortemplate) | A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** `[fix]` and `[problem]` tags and **optional** `[id]` and `[value]`. | +| [`template: string`](#validationerrortemplate) | A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** **`[fix]`** and **`[problem]`** tags and **optional** **`[id]`** and **`[value]`**. | **Instance properties:** @@ -149,7 +149,7 @@ Manages an [`Error`][js-error] of validation. | [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | | [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | | [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. | +| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`** tags. | | [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. | [error-property-fix]: #validationerrorprototypefix @@ -206,7 +206,7 @@ Manages an [`Error`][js-error] of validation. [![update]][error-github-changelog] -A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** `[fix]` and `[problem]` tags and **optional** `[id]` and `[value]`. The value is being checked against the existence of `[fix]` and `[problem]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the static [`ValidationError.setTemplate()`](#validationerrorsettemplate) method. By default, it's set to `Problem: [problem] => Fix: [fix]`. +A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** **`[fix]`** and **`[problem]`** tags and **optional** **`[id]`** and **`[value]`**. The value is being checked against the existence of **`[fix]`** and **`[problem]`** tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the static [`ValidationError.setTemplate()`](#validationerrorsettemplate) method. By default, it's set to `Problem: [problem] => Fix: [fix]`. ```typescript static get template(): string { @@ -219,13 +219,13 @@ static set template(value: string) { **Tags:** -`[fix]`: **Required**, replaced by the [`fix`][error-property-fix] property. +**`[fix]`**: **Required**, replaced by the [`fix`][error-property-fix] property. -`[id]`: Optional, replaced by the [`id`][error-property-id] property. +**`[id]`**: Optional, replaced by the [`id`][error-property-id] property. -`[problem]`: **Required**, replaced by the [`problem`][error-property-problem] property. +**`[problem]`**: **Required**, replaced by the [`problem`][error-property-problem] property. -`[value]`: Optional, replaced by the [`value`][error-property-value] property. +**`[value]`**: Optional, replaced by the [`value`][error-property-value] property.
@@ -313,7 +313,7 @@ public set problem(value: string) { [![new]][error-github-changelog] -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. The value is being checked against the existence of **required** `[problem]` and `[fix]` tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). By default, it's set to `Problem: [problem] => Fix: [fix]`. +A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`** tags. The value is being checked against the existence of **required** **`[problem]`** and **`[fix]`** tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). By default, it's set to `Problem: [problem] => Fix: [fix]`. ```typescript public get template(): string { @@ -385,7 +385,7 @@ public static defineMessage( | Name: type | Description | | :---------------------------------------- | :---------- | | `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) by the static private `ValidationError.#guarMessage()` method. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags and parser is [`function`][js-funciton] type. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has **`[problem]`** and **`[fix]`** tags and parser is [`function`][js-function] type. | | `parser: ValueParser` | An optional [`function`][js-function] to **convert** property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during [`message`][error-property-message] creation. It can be set by static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) as the default value parser for static and instance [`ValidationError`](#validationerror). | **Returns:** @@ -488,8 +488,8 @@ public static setTemplate( | Name: type | Description | | :---------------------------------- | :---------- | -| `template: string` | A [template](#validationerrortemplate) for error [`message`][error-property-message] guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]`. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains required `[fix]` and `[problem]` tags. | +| `template: string` | A [template](#validationerrortemplate) for error [`message`][error-property-message] guarded by [`string`][js-string] type with replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`**. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains required **`[fix]`** and **`[problem]`** tags. | **Returns:** @@ -531,7 +531,7 @@ ValidationError [![new]][error-github-changelog] -Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the default parser for the instance. +Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the initial parser for the instance. ```typescript // Syntax. @@ -604,7 +604,7 @@ constructor(message?: ErrorMessage, parser?: ValueParser) { // Sets the parser. isDefined(parser) && this.setValueParser(parser); - // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. + // Initializes the message and assigns its properties to a new instance. isDefined(message) && this.setMessage(message, undefined, parser); } ``` @@ -739,7 +739,7 @@ public setFix(fix: string, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the `[fix]` tag of [`template`][error-property-template]. | +| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the **`[fix]`** tag of [`template`][error-property-template]. | | `callback?: ResultCallback` | 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]. | **Returns:** @@ -802,8 +802,8 @@ public setId(id: number, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `id: number` | The identifier, guarded by [`number`][js-number] type and a replacement to the `[id]` tag of [`template`][error-property-template]. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `id` is [`number`][js-number]. | +| `id: number` | The identifier, guarded by [`number`][js-number] type and a replacement to the **`[id]`** tag of [`template`][error-property-template]. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`id`][error-property-id] is [`number`][js-number]. | **Returns:** @@ -864,7 +864,7 @@ public setMessage( | Name: type | Description | | :------------------------------------------ | :---------- | | `message: ErrorMessage` | An [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) and if it's a proper object then its properties are being assigned to the instance. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, the **optional** [`template`][error-property-template] property has `[problem]` and `[fix]` tags and `parser` is a function type. | +| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, the **optional** [`template`][error-property-template] property has **`[problem]`** and **`[fix]`** tags and `parser` is a function type. | | `parser: ValueParser` | An optional [`function`][js-function] to **convert** property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during message creation. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | **Returns:** @@ -929,7 +929,7 @@ public setProblem(problem: string, callback?: ResultCallback): this { | Name: type | Description | | :---------------------------------- | :---------- | -| `problem: string` | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the `[problem]` tag of [`template`][error-property-template]. | +| `problem: string` | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the **`[problem]`** tag of [`template`][error-property-template]. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. | **Returns:** @@ -981,8 +981,8 @@ public setTemplate( | Name: type | Description | | :---------------------------------- | :---------- | -| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable **required** `[problem]` and `[fix]` tags and **optional** `[id]` and `[value]` tags. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains **required** `[fix]` and `[problem]` tags. | +| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`** tags. | +| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains **required** **`[fix]`** and **`[problem]`** tags. | **Returns:** @@ -1035,7 +1035,7 @@ public setValue(value: any): this { | Name: type | Description | | :----------- | :---------- | -| `value: any` | The value of [`any`][ts-any] type as a replacement to the `[value]` tag of [`template`][error-property-template] that relates to the given [`problem`][error-property-problem]. | +| `value: any` | The value of [`any`][ts-any] type as a replacement to the **`[value]`** tag of [`template`][error-property-template] that relates to the given [`problem`][error-property-problem]. | **Returns:** From 10d7391e194d42908490839ed37fb506a5504cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 01:19:57 +0200 Subject: [PATCH 059/179] test(ValidationError): update and add tests. --- src/test/validation-error.spec.ts | 573 ++++++++++++++++++------------ 1 file changed, 341 insertions(+), 232 deletions(-) diff --git a/src/test/validation-error.spec.ts b/src/test/validation-error.spec.ts index feca4d1..085fc29 100644 --- a/src/test/validation-error.spec.ts +++ b/src/test/validation-error.spec.ts @@ -2,290 +2,399 @@ import { Testing, TestingToBeMatchers } from '@angular-package/testing'; // Class. import { ValidationError } from '../lib/validation-error.class'; +// Interface. +import { ErrorMessage } from '../interface/error-message.interface'; /** * Initialize `Testing`. */ -const testing = new Testing(true, true); +const testing = new Testing(true, true, { + // describe: [1, 8], + // it: [1, 2] +}); const toBe = new TestingToBeMatchers(); /** * Tests. */ -testing.describe('ValidationError', () => { - let fix: any; - let problem: any; - let template: any; +testing.describe('[counter] ValidationError', () => { + let errorMessage: string; + let fix: string; + let id: number; + let message: ErrorMessage; + let problem: string; + let template: string; let value: any; - let errorMessage: any; let validationError: ValidationError; + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = 427; + problem = 'The value must be a string type.'; + template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; + value = Symbol(123); + beforeEach(() => { - fix = 'There is no solution to the described problem.'; - problem = 'The problem has no solution.'; + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = 427; + problem = 'The value must be a string type.'; + template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; + value = Symbol(123); validationError = new ValidationError(); }); - testing.describe('throw', () => { - try { - throw new ValidationError({ problem, fix }); - } catch (e: any) { - testing - .toBeClass(ValidationError) - .toBe('instanceof ValidationError', e instanceof ValidationError, true) - .toBeStringType( - e.problem, - undefined, - '`e.problem` must be of a `string` type' - ) - .toBeStringType(e.fix, undefined, '`e.fix` must be of a `string` type'); - } - }) - - .describe('instantiate', () => { - beforeEach(() => validationError = new ValidationError({ fix, problem })); - - testing.it(`with the message of a \`string\` type`, () => { - const message = 'Validation error message'; - validationError = new ValidationError(message); - toBe.string(validationError.message); - expect(validationError.message).toContain(message); - }); - - testing.it(`with the message of the \`ErrorMessage\` interface`, () => { - toBe - .string(validationError.message) - .string(validationError.problem) - .string(validationError.fix); - - // to Equal. - expect(validationError.problem).toEqual(problem); - expect(validationError.fix).toEqual(fix); - expect(validationError.message).toEqual( - ValidationError.template - .replace(`[fix]`, fix) - .replace(`[problem]`, problem) - ); - // toContain. - expect(validationError.message).toContain(fix); - expect(validationError.message).toContain(problem); - }); - }) - - .describe('.defineMessage()', () => { - beforeEach(() => errorMessage = ValidationError.defineMessage({ fix, problem })); + testing + /** + * defineMessage() + */ + .describe('[counter] .defineMessage()', () => { + testing.it(`[counter] with the message of the \`ErrorMessage\` interface`, () => { + errorMessage = ValidationError.defineMessage({ fix, id, problem, template, value}); + toBe.string(errorMessage); + // toContain. + expect(errorMessage).toContain(fix); + expect(errorMessage).toContain(String(id)); + expect(errorMessage).toContain(problem); + expect(errorMessage).toContain(String(value)); + }) + .it(`[counter] with the message of the \`ErrorMessage\` interface with the provided \`parser\``, () => { + message = { fix, id, problem, template, value }; + errorMessage = ValidationError.defineMessage( + message, + (result, checkedValue, payload) => { + toBe + .true(result) + .object(checkedValue) + .undefined(payload); - testing - .it(`with the message of the \`ErrorMessage\` interface`, () => { + expect(checkedValue).toEqual(message); + return result; + } , valueToConvert => String(12345)); toBe.string(errorMessage); - expect(errorMessage).toEqual( - ValidationError.template - .replace(`[fix]`, fix) - .replace(`[problem]`, problem) - ); // toContain. expect(errorMessage).toContain(fix); + expect(errorMessage).toContain(String(id)); expect(errorMessage).toContain(problem); + expect(errorMessage).toContain(String(12345)); }); - }) + }) - .describe('.prototype.setFix()', () => { - testing - .it(`set ${fix}`, () => { - validationError.setFix(fix, (result, value, payload) => { - expect(value).toEqual(fix); - toBe - .boolean(result) - .string(value) - .undefined(payload); + /** + * setTemplate() + */ + .describe('[counter] .setTemplate()', () => { + testing.it(`[counter] propery set the static \`template\``, () => { + expect(ValidationError.template).toEqual(`Problem[id]: [problem] => Fix: [fix]`); + ValidationError.setTemplate(template, (result, checkedValue) => { + toBe.true(result).string(checkedValue); + expect(checkedValue).toEqual(template); return result; }); - expect(validationError.fix).toEqual(fix); + expect(ValidationError.template).toEqual(template); }); - }) - - .describe('[counter] .prototype.setMessage()', () => { - testing + }) - .it(`[counter] set ${problem}`, () => { - validationError.setMessage(problem, (result, value, payload) => { - expect(value).toEqual(problem); - toBe - .boolean(result) - .string(value) - .undefined(payload); + /** + * setValueParser() + */ + .describe('[counter] .setValueParser()', () => { + testing.it(`[counter] properly set the static private \`#defaultValueParser\``, () => { + ValidationError.setValueParser(valueToConvert => String(12345678910), (result, checkedValue) => { + toBe.true(result).function(checkedValue); return result; }); - expect(validationError.message).toEqual(problem); + expect(ValidationError.defineMessage({ fix, problem, value, template })).toContain(String(12345678910)); + // The default value parser. + ValidationError.setValueParser(valueToConvert => String(valueToConvert)); + }); + }) + + /** + * constructor() + */ + .describe('[counter] instantiate', () => { + beforeEach(() => validationError = new ValidationError({ + fix, id, problem, template, value + })); + + testing.it(`[counter] with the message of the \`ErrorMessage\` interface`, () => { + toBe + .string(validationError.fix) + .number(validationError.id) + .string(validationError.message) + .string(validationError.problem) + .string(validationError.template) + .symbol(validationError.value); + + // to Equal. + expect(validationError.fix).toEqual(fix); + expect(validationError.id).toEqual(id); + expect(validationError.problem).toEqual(problem); + expect(validationError.template).toEqual(template); + expect(validationError.value).toEqual(value); + + // toContain. + expect(validationError.message).toContain(fix); + expect(validationError.message).toContain(String(id)); + expect(validationError.message).toContain(problem); + expect(validationError.message).toContain(String(value)); }) + .it(`[counter] with the provided \`parser\``, () => { + validationError = new ValidationError({ + fix, id, problem, template, value + }, (valueToConvert: any) => String(12345)); + toBe + .string(validationError.fix) + .number(validationError.id) + .string(validationError.message) + .string(validationError.problem) + .string(validationError.template) + .symbol(validationError.value); - .it(`[counter] set object`, () => { - errorMessage = { fix, problem }; - validationError.setMessage(errorMessage, (result, value, payload) => { - expect((value as any).fix).toEqual(errorMessage.fix); - expect((value as any).problem).toEqual(errorMessage.problem); - toBe - .boolean(result) - .object(value) - .undefined(payload); - return result; - }); + // to Equal. expect(validationError.fix).toEqual(fix); + expect(validationError.id).toEqual(id); expect(validationError.problem).toEqual(problem); + expect(validationError.template).toEqual(template); + expect(validationError.value).toEqual(value); + + // toContain. + expect(validationError.message).toContain(fix); + expect(validationError.message).toContain(String(id)); + expect(validationError.message).toContain(problem); + expect(validationError.message).toContain(String(12345)); }); - }) + }) - .describe('[counter] .prototype.setProblem()', () => { - testing.it(`[counter] set ${problem}`, () => { - validationError.setProblem(problem, (result, value, payload) => { - expect(value).toEqual(problem); - toBe - .boolean(result) - .string(value) - .undefined(payload); - return result; + /** + * .prototype.setFix() + */ + .describe('[counter] .prototype.setFix()', () => { + testing.it(`set ${fix}`, () => { + validationError.setFix(fix, (result, checkedValue, payload) => { + expect(checkedValue).toEqual(fix); + toBe.boolean(result).string(checkedValue).undefined(payload); + return result; + }); + expect(validationError.fix).toEqual(fix); }); - expect(validationError.problem).toEqual(problem); - }); - }) + }) - .describe('[counter] .prototype.setTemplate()', () => { - template = `[problem] must be fixed by using [fix]`; - testing.it(`[counter] set ${template}`, () => { - validationError.setTemplate(template, (result, value, payload) => { - expect(value).toEqual(template); - toBe - .boolean(result) - .string(value) - .object(payload); - return result; + /** + * .prototype.setId() + */ + .describe('[counter] .prototype.setId()', () => { + testing.it(`set ${id}`, () => { + validationError.setFix(fix, (result, checkedValue, payload) => { + expect(checkedValue).toEqual(fix); + toBe.boolean(result).string(checkedValue).undefined(payload); + return result; + }); + expect(validationError.fix).toEqual(fix); }); - expect(validationError.template).toEqual(template); - }); - }) + }) - .describe('[counter] .prototype.setValue()', () => { - template = `[problem] [fix] Got [value]`; - testing + /** + * .prototype.setMessage() + */ + .describe('[counter] .prototype.setMessage()', () => { + testing + .it(`[counter] set works properly`, () => { + validationError.setMessage({ fix, id, problem, template, value }, (result, checkedValue) => { + expect(checkedValue.fix).toEqual(fix); + expect(checkedValue.id).toEqual(id); + expect(checkedValue.problem).toEqual(problem); + expect(checkedValue.value).toEqual(value); + toBe.true(result).object(checkedValue); + return result; + }); + expect(validationError.message).toContain(fix); + expect(validationError.message).toContain(String(id)); + expect(validationError.message).toContain(problem); + expect(validationError.message).toContain(String(value)); + }) + .it(`[counter] set works properly with the provided \`parser\``, () => { + validationError.setMessage( + { fix, id, problem, template, value }, + (result, checkedValue) => { + expect(checkedValue.fix).toEqual(fix); + expect(checkedValue.id).toEqual(id); + expect(checkedValue.problem).toEqual(problem); + expect(checkedValue.value).toEqual(value); + toBe.true(result).object(checkedValue); + return result; + }, valueToConvert => String(12345)); + expect(validationError.message).toContain(fix); + expect(validationError.message).toContain(String(id)); + expect(validationError.message).toContain(problem); + expect(validationError.message).toContain(String(12345)); + }); + }) - .it(`[counter] set ${template} with the string value`, () => { - value = 'string value'; - validationError.setTemplate(template).setValue(value, (result, value, payload) => { - expect(value).toEqual('string value'); - toBe.boolean(result).string(value); + /** + * .prototype.setProblem() + */ + .describe('[counter] .prototype.setProblem()', () => { + testing.it(`[counter] set ${problem}`, () => { + validationError.setProblem(problem, (result, checkedValue) => { + expect(checkedValue).toEqual(problem); + toBe.boolean(result).string(checkedValue); return result; }); - expect(validationError.template).toEqual(template); - }) + expect(validationError.problem).toEqual(problem); + }); + }) - .it(`[counter] set ${template} with the object value`, () => { - value = { firstName: Symbol(123), lastName: '', age: 27 }; - validationError.setTemplate(template).setValue(JSON.stringify(value), (result, value, payload) => { - expect(value).toEqual('{"lastName":"","age":27}'); - toBe.boolean(result).string(value); + /** + * .prototype.setTemplate() + */ + .describe('[counter] .prototype.setTemplate()', () => { + template = `[problem] must be fixed by using [fix]`; + testing.it(`[counter] set ${template}`, () => { + validationError.setTemplate(template, (result, checkedValue, payload) => { + expect(checkedValue).toEqual(template); + toBe.boolean(result).string(checkedValue).object(payload); return result; - }) - .setProblem(problem).setFix(fix).updateMessage(); - expect(validationError.message) - .toEqual('The problem has no solution. There is no solution to the described problem. Got {"lastName":"","age":27}'); + }); expect(validationError.template).toEqual(template); }); - }) + }) + + /** + * .prototype.setValue() + */ + .describe('[counter] .prototype.setValue()', () => { + testing.it(`[counter] set ${String(value)}`, () => { + validationError.setValue(value); + expect(validationError.value).toEqual(value); + }); + }) + + /** + * .prototype.updateMessage() + */ + .describe('[counter] .prototype.updateMessage()', () => { + testing.it(`[counter] works properly`, () => { + validationError + .setFix('') + .setId(0) + .setProblem('') + .setTemplate(template) + .setValue('') + .setValueParser(valueToConvert => String(12345678910)) + .updateMessage(); + + expect(validationError.fix).toEqual(''); + expect(validationError.id).toEqual(0); + expect(validationError.problem).toEqual(''); + expect(validationError.value).toEqual(''); + expect(validationError.message).toContain(String(12345678910)); - .describe('[counter] .prototype.updateMessage()', () => { - testing - .it(`[counter] works properly`, () => { validationError .setFix(fix) - .setProblem(problem); + .setId(id) + .setProblem(problem) + .setTemplate(template) + .setValue(value) + .setValueParser(valueToConvert => String(valueToConvert)) + .updateMessage((result, checkedValue) => { + toBe.true(result).object(checkedValue); + expect(checkedValue).toEqual({ + fix, id, problem, template, value + }); + return result; + }); + expect(validationError.fix).toEqual(fix); + expect(validationError.id).toEqual(id); expect(validationError.problem).toEqual(problem); - expect(validationError.message).toEqual(''); - validationError.updateMessage(); - expect(validationError.message) - .toEqual('Problem: The problem has no solution. => Fix: There is no solution to the described problem.'); + expect(validationError.value).toEqual(value); + expect(validationError.message).toContain(String(value)); }); - }) + }) - .describe('[counter] .prototype.throw()', () => { - testing - .it(`[counter] with string`, () => { - try { - validationError.setFix(fix).setProblem(problem); - validationError.throw('Throws a string'); - } catch (e) { - expect(e.problem).toEqual(''); - expect(e.fix).toEqual(''); - toBe - .stringOfLength(e.problem, { max: 0 }) - .stringOfLength(e.fix, { max: 0 }); - } - }) - .it(`[counter] with object of ErrorMessage`, () => { - try { - validationError.setFix(fix).setProblem(problem).setTemplate(`[problem], [fix]`); - validationError.throw({ - problem: 'new problem', - fix: 'new fix' - }); - } catch (e) { - expect(e.message).toEqual(`new problem, new fix`); - expect(e.problem).toEqual('new problem'); - expect(e.fix).toEqual('new fix'); - } - }) - .it(`[counter] with set \`string\` type message`, () => { - try { - validationError - .setMessage(problem) - .throw(); - } catch (e) { - expect(e.message).toEqual(problem); - expect(e.problem).toEqual(''); - expect(e.fix).toEqual(''); - toBe - .stringOfLength(e.problem, { max: 0 }) - .stringOfLength(e.fix, { max: 0 }); - } - }) + /** + * .prototype.throw() + */ + .describe('[counter] throw', () => { + try { + throw new ValidationError({ fix, id, problem, template, value }); + } catch (e: any) { + testing + .toBeClass(ValidationError) + .toBe('instanceof ValidationError', e instanceof ValidationError, true) + .toBeStringType(e.fix, undefined, '`e.fix` must be of a `string` type') + .toBeNumberType(e.id, undefined, '`e.fix` must be of a `string` type') + .toBeStringType(e.problem, undefined, '`e.problem` must be of a `string` type'); + } + }) - .it(`[counter] with the actual \`problem\`, \`fix\` and \`value\``, () => { - value = '27'; - try { - validationError - .setMessage('my message') - .setProblem(problem).setFix(fix).setValue(value).setTemplate(`[problem], [fix], [value]`) - .throw(); - } catch (e) { - expect(e.message).toEqual(`${problem}, ${fix}, ${value}`); - expect(e.fix).toEqual(fix); - expect(e.problem).toEqual(problem); - expect(e.value).toEqual(value); - } + .describe('[counter] .prototype.throw()', () => { + testing + .it(`[counter] with object of ErrorMessage`, () => { + try { + validationError + .setFix(fix) + .setProblem(problem) + .setTemplate(`[problem], [fix]`); + validationError.throw({ + problem: 'new problem', + fix: 'new fix', + }); + } catch (e) { + expect(e.message).toEqual(`new problem, new fix`); + expect(e.problem).toEqual('new problem'); + expect(e.fix).toEqual('new fix'); + } + }) + .it( + `[counter] with the actual \`problem\`, \`fix\` and \`value\``, + () => { + value = '27'; + try { + validationError + .setProblem(problem) + .setFix(fix) + .setValue(value) + .setTemplate(`[problem], [fix], [value]`) + .throw(); + } catch (e) { + expect(e.message).toEqual(`${problem}, ${fix}, ${String(value)}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + expect(e.value).toEqual(value); + } - try { - validationError - .setMessage('my message') - .setMessage({ - fix, problem, value, template: `[problem] [fix] [value]` - }).throw(); - } catch (e) { - expect(e.message).toEqual(`${problem} ${fix} ${value}`); - expect(e.fix).toEqual(fix); - expect(e.problem).toEqual(problem); - expect(e.value).toEqual(value); - } + try { + validationError + .setMessage({ + fix, + problem, + value, + template: `[problem] [fix] [value]`, + }) + .throw(); + } catch (e) { + expect(e.message).toEqual(`${problem} ${fix} ${String(value)}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + expect(e.value).toEqual(value); + } - try { - validationError - .setMessage('my message') - .setMessage({ - fix, problem, template: `[problem] [fix] [value]` - }).throw(); - } catch (e) { - expect(e.message).toEqual(`${problem} ${fix} `); - expect(e.fix).toEqual(fix); - expect(e.problem).toEqual(problem); - expect(e.value).toEqual(''); - } - }); - }); + try { + validationError + .setMessage({ + fix, + problem, + template: `[problem] [fix] [value]`, + }) + .throw(); + } catch (e) { + expect(e.message).toEqual(`${problem} ${fix} ${String(value)}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + expect(e.value).toEqual(value); + } + } + ); + }); }); From c00758a02cd32e61b813719df7ee8b0f7d18cb12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 16:04:25 +0200 Subject: [PATCH 060/179] refactor(ValidationError): general update and jsdoc. Add: // Static general - add tag `[id]` to static private property `#tags` and as string to the static private property `#defaultTemplate`. - add static `#defaultValueParser` property of the default value equal to `(value) => String(value)`. - add static public method `setTemplate()` to set the `template` and `setValueParser()` to set value parser, for the static `ValidationError`. // Instance private properties - add private property `#id` of an instance as an identifier of the described problem. - add private property `#valueParser` function of `ValueParser` type to convert the value of `any` type to `string` with the default value equal to static `ValidationError.#defaultValueParser`. // Public instance properties - add a public property `id` as an identifier of the described `problem`, guarded by `number` type, and as a replacement for an optional tag `[id]`. // Public instance methods - add public `setId()` method of an instance to set the `id` an identifier of the described problem. - add `setValueParser()` method of an instance to convert property `value` of an instance to `string` during message creation. - add private method `assignMessageProperties()` of an instance to assign properties of the provided `message` to the instance. Update: // Static general - static private property `#template` change name to `#defaultTemplate`. - update static public `template` to refers to static private `#defaultTemplate`. - add to static public `defineMessage()` method an optional `parser` parameter of `ValueParser` type to convert property `value` from the provided `message` to `string` during message creation. - change the way of building the message by the static private method `#buildMessage()`. // Instance private properties - private property `#tpl` of an instance change name to `#template`. // Instance public properties - update public property `template` of an instance to refers to private property `#template` of an instance. - public property `value` change type from `string` to `any`. // Instance public methods - remove possibility to add `string` type message in the `constructor`, `throw()`, `setMessage()` methods of an instance. - add to `constructor()` an optional `parser` parameter of `ValueParser` type that initialize the default parser for the instance. - update the `setMessage()` method of an instance by adding an optional parameter `parser` of `ValueParser` type and the way it assigns the message properties to only when they are all proper. - update `setTemplate()` method of an instance to use `#template` private property. - remove `callback` parameter from the `setValue()` method of an instance and change its parameter type to `any`. - `throw()` method throws new instance of `ValidationError` based on actual settings. - add an optional `callback` parameter to the `updateMessage()` method of an instance and add the use of value parser function. --- src/lib/validation-error.class.ts | 344 ++++++++++++++++++++++-------- 1 file changed, 250 insertions(+), 94 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 861766c..a718e0c 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,14 +1,17 @@ // @angular-package/type. import { + // Type. ResultCallback, + ValueParser, + // Function. areString, + guardFunction, + guardNumber, guardObjectKeys, guardString, guardStringIncludes, - guardStringLength, isDefined, - isObject, - isString, + isTrue, } from '@angular-package/type'; // Interface. import { ErrorMessage } from '../interface/error-message.interface'; @@ -20,12 +23,12 @@ export class ValidationError extends Error { /** * Tags to be replaced. */ - static #tags = ['fix', 'problem', 'value']; + static #tags = ['fix', 'id', 'problem', 'value']; /** - * A static, privately stored template of the error message. + * A static, privately stored template of the error `message`. */ - static #template = `Problem: [problem] => Fix: [fix]`; + static #defaultTemplate = `Problem[id]: [problem] => Fix: [fix]`; //#endregion static private properties //#region instance private properties. @@ -35,6 +38,11 @@ export class ValidationError extends Error { */ #fix = ''; + /** + * A privately stored identifier of the described problem. By default, it's `0`. + */ + #id = 0; + /** * A privately stored validation problem of `string` type. * * By default, it's an empty `string`. @@ -42,36 +50,44 @@ export class ValidationError extends Error { #problem = ''; /** - * A string-type privately stored template of the error message that contains replaceable `[fix]` and `[problem]` tags. + * A string-type privately stored template of the error message that contains replaceable required `[fix]` and `[problem]` tags and + * optional `[id]` and `[value]`. * * By default, it's set to `Problem: [problem] => Fix: [fix]`. */ - #tpl = ValidationError.template; + #template = ValidationError.template; /** - * The value affected by the validation error after parsing to `string`. + * The value affected by the validation error. */ #value = ''; + + /** + * Private function to convert the value of any type to `string`. + */ + #valueParser: ValueParser = ValidationError.#defaultValueParser; //#endregion instance private properties. //#region static public properties /** - * A template of the error message guarded by `string` type with the replaceable `[problem]` and `[fix]` tags. + * A template of static `ValidationError` and the default value of `template` of an instance for the error message guarded by `string` + * type with the replaceable required `[problem]` and `[fix]` tags and optional `[id]` and `[value]`. * * By default, it's set to `Problem: [problem] => Fix: [fix]`. * * The value is being checked against the existence of `[problem]` and `[fix]` tags. + * * Possible tags to be replaced `[fix]`, `[id]`, `[problem]`, `[value]`. */ static get template(): string { - return this.#template; + return this.#defaultTemplate; } static set template(value: string) { - this.#guardTemplate(value) && (this.#template = value); + this.#guardTemplate(value) && (this.#defaultTemplate = value); } //#endregion static public properties //#region instance public properties. /** - * A possible solution to the described `problem` of validation that is guarded by `string` type. - * * It can be set directly or by the `setFix()` and `setMessage()` method. - * * By default, it's an empty `string`. + * A possible solution to the described `problem` of validation that is guarded by `string` and it's a replacement for the required tag + * `[fix]` of the `template`. + * * It can be set directly or by the `setFix()`, `setMessage()` and `throw()` methods. By default, it's an empty string. */ public get fix(): string { return this.#fix; @@ -81,9 +97,22 @@ export class ValidationError extends Error { } /** - * A validation error message guarded by `string` type that can be built from the `problem` and `fix` on the `template` - * of an instance of `ValidationError`. - * * It can be set directly or by the `throw()` and `setMessage()` method. + * The identifier of the described `problem`, guarded by `number` type and it's a replacement for an optional tag `[id]` of the + * `template`. + * * It refers to `[id]` tag. + * * It can be set directly or by the `setId()`, `setMessage()` and `throw()` methods of an instance. By default, it's 0. + */ + public get id(): number { + return this.#id; + } + public set id(value: number) { + guardNumber(value) && (this.#id = value); + } + + /** + * A validation error `message` guarded by `string` type that can be built from the required `problem` and `fix` with optional `id` and + * `value` on the `template` of an instance of `ValidationError`. + * * It can be set directly or by the `setMessage()` and `throw()` methods. */ public get message(): string { return super.message; @@ -99,9 +128,10 @@ export class ValidationError extends Error { public name = ValidationError.name; /** - * Description of the validation problem guarded by `string` type. - * * It can be set directly or by the `setProblem()` and `setMessage()` method. - * * By default, it's an empty `string`. + * Description of the validation problem guarded by `string` type and it's a replacement for the required tag `[problem]` of the + * `template`. + * * It refers to `[problem]` tag. + * * It can be set directly or by the `setProblem()`, `setMessage()` and `throw()` methods. By default, it's an empty string. */ public get problem(): string { return this.#problem; @@ -111,50 +141,103 @@ export class ValidationError extends Error { } /** - * A template of the error message guarded by `string` type with required replaceable `[problem]` and `[fix]` tags and optionally - * `[value]` tag. The value is being checked against the existence of required `[problem]` and `[fix]` tags. - * * It can be set directly or by the `setTemplate()` and `setMessage()` method. + * A template of the error `message` guarded by `string` type with the replaceable required `[problem]` and `[fix]` tags and optional + * `[id]` and `[value]` tags. The value is being checked against the existence of required `[problem]` and `[fix]` tags by the static + * private `ValidationError.#guardTemplate()` method. + * * It can be set directly or by the `setTemplate()`, `setMessage()` and `throw()` methods, and its initial value is from the static + * `ValidationError.template`. * * By default, it's set to `Problem: [problem] => Fix: [fix]`. + * * Possible tags to use `[fix]`, `[id]`, `[problem]` and `[value]`. */ public get template(): string { - return this.#tpl; + return this.#template; } public set template(value: string) { - ValidationError.#guardTemplate(value) && (this.#tpl = value); + ValidationError.#guardTemplate(value) && (this.#template = value); } /** - * The value affected by the validation error, which must be parsed to `string`. + * The value of any type affected by the validation error, which must be converted to a `string` to build a `message` and it's a + * replacement for an optional tag `[value]` of the `template`. + * * It can be set directly or by the `setValue()`, `setMessage()` and `throw()` methods. + * * It refers to `[value]` tag. + * * The conversion is performed only during message creation and can be done in two ways, automatic or manual. + * * The manual, by providing parser function as parameter (`parser?: ValueParser`) to the `setMessage()` method of an instance. + * * The automatic by the previously defined function of the `ValueParser` type, set by the `setValueParser()` method of an instance to + * automatize `setMessage()` method. */ - public get value(): string { + public get value(): any { return this.#value; } - public set value(value: string) { - guardString(value) && (this.#value = value); + public set value(value: any) { + this.#value = value; } //#endregion instance public properties. //#region static public methods /** * Defines the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface. The message is built - * on the provided `template` or the template from the static property `template`. + * on the provided `template` or the template from the static property `ValidationError.template`. + * * The automatic convert of the `value` of an `ErrorMessage` is performed by the defined function of the `ValueParser` type, set by the + * static `ValidationError.setValueParser()` method. * @param message An `object` of an `ErrorMessage` interface to build a message of `string` type. The value is checked against the proper - * `object` of `ErrorMessage`. + * `object` of `ErrorMessage` by the static private `ValidationError.#guarMessage()` method. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message is the proper * `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the optional `template` property has - * `[problem]` and `[fix]` tags. + * required `[problem]` and `[fix]` tags and parser is a `function` type. + * @param parser An optional `function` to convert property `value` from the provided `message` to `string` during message creation. It + * can be set by static `ValidationError.setValueParser()` as the default value parser for static and instance `ValidationError`. * @returns The return value is a message of `string` type created from the provided `message` of the `ErrorMessage` interface or it's an * empty `string` if the provided message object isn't proper. * @angularpackage */ public static defineMessage( message: ErrorMessage, - callback?: ResultCallback + callback: ResultCallback = (result) => result, + parser: ValueParser = this.#defaultValueParser ): string { - return this.#guardMessage(message, callback) - ? this.#buildMessage(message) + return callback( + this.#guardMessage(message) && guardFunction(parser), + message + ) + ? this.#buildMessage(message, parser) : ''; } + + /** + * Sets the `template` of static `ValidationError` and the default value for `template` of an instance. + * @param template A template for error `message` guarded by `string` type with replaceable required `[problem]` and `[fix]` tags and + * optional `[id]` and `[value]`. + * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a + * `string` that contains required `[fix]` and `[problem]` tags. + * @returns The return value is static `ValidationError`. + * @angularpackage + */ + public static setTemplate( + template: string, + callback?: ResultCallback + ): typeof ValidationError { + ValidationError.#guardTemplate(template, callback) && + (this.#defaultTemplate = template); + return ValidationError; + } + + /** + * Sets the `function` to convert any value to `string` during message creation performed by the static `defineMessage()` method and as + * the initial parser for the instance. + * @param parser The `function` of the `ValueParser` type to convert the value of `any` type to `string`. + * @param callback An optional callback function of `ResultCallback` type to handle the result of the check whether the provided `parser` + * is `function` type. + * @returns The return value is static `ValidationError`. + * @angularpackage + */ + public static setValueParser( + parser: ValueParser, + callback?: ResultCallback + ): typeof ValidationError { + guardFunction(parser, callback) && (this.#defaultValueParser = parser); + return ValidationError; + } //#endregion static public methods //#region static private methods. @@ -162,23 +245,36 @@ export class ValidationError extends Error { * Builds the validation error message of `string` type based on the provided `message` of the `ErrorMessage` interface. * @param message An `object` of an `ErrorMessage` interface to build a message of `string` type. The value is not checked against the * proper `object`. + * @param parser The required `function` to convert property `value` from the provided `message` to `string` during message creation. * @returns The return value is a message of `string` type created from the provided `message` parameter. * @angularpackage */ - static #buildMessage(message: ErrorMessage): string { + static #buildMessage(message: ErrorMessage, parser: ValueParser): string { let m = message.template || this.template; return ( this.#tags.forEach( (tag) => (m = m.replace( `[${tag}]`, - String((message[tag as keyof Omit])) + ( + { + fix: (value: any): string => String(value), + id: (value: any): string => String(value || ''), + problem: (value: any): string => String(value), + value: (value: any): string => parser(value), + } as { [index: string]: ValueParser } + )[tag](message[tag as keyof Omit]) )) ), m ); } + /** + * The default parser `function` of static `ValidationError` and the instance. + */ + static #defaultValueParser: ValueParser = (value) => String(value); + /** * Guards the provided message to be of `ErrorMessage` shape. * @param message An object of the `ErrorMessage` interface to build the message of `string` type. The value is checked against the proper @@ -221,23 +317,30 @@ export class ValidationError extends Error { //#endregion static private methods. /** - * Creates a new instance with the message. If the provided `message` is an `object`, then its properties are assigned to the instance. - * @param message The message of `string` type or of an `ErrorMessage` interface that is used to throw with an `Error`. + * Creates a new instance with an optional message of `ErrorMessage` interface. + * @param message An optional `message` of an `object` of the `ErrorMessage` interface to build a message of `string` and throw with + * an `Error`. Its properties are assigned to the instance. + * @param parser An optional `function` to convert property `value` of the provided `message` of `ErrorMessage` to a `string` during + * message creation and set as the default value parser for the instance. By default, it uses the parser function of private property + * `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. * @angularpackage */ - constructor(message: string | ErrorMessage = '') { + constructor(message?: ErrorMessage, parser?: ValueParser) { super(); - // Initializes the message and assigns message properties `fix`, `problem` and optionally `template` to a new instance. - this.setMessage(message); + // Sets the parser. + isDefined(parser) && this.setValueParser(parser); + + // Initializes the message and assigns its properties to a new instance. + isDefined(message) && this.setMessage(message); } //#region instance public methods. /** * Sets the `fix` a possible solution to the described `problem`. - * @param fix A possible solution to the described `problem` guarded by `string` type. + * @param fix A possible solution to the described `problem` guarded by `string` type and a replacement to the `[fix]` tag of `template`. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `fix` is a `string`. - * @returns The return value is an instance of an `ValidationError`. + * @returns The return value is an instance of a `ValidationError`. * @angularpackage */ public setFix(fix: string, callback?: ResultCallback): this { @@ -246,41 +349,53 @@ export class ValidationError extends Error { } /** - * Sets the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface or `string` type. - * @param message A `string` type or an `object` of an `ErrorMessage` interface to build the message of `string` type. The value is - * checked against the proper `object` of `ErrorMessage`. + * Sets the `id` an identifier of the described problem. + * @param id The identifier, guarded by a `number` type and a replacement to the `[id]` tag of `template`. + * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `id` is `number`. + * @returns The return value is an instance of a `ValidationError`. + * @angularpackage + */ + public setId(id: number, callback?: ResultCallback): this { + guardNumber(id, callback) && (this.#id = id); + return this; + } + + /** + * Sets the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface. + * @param message An `object` of an `ErrorMessage` interface to build the message of `string` type. The value is checked against the + * proper `object` of `ErrorMessage` and if it's a proper object then its properties are being assigned to the instance. * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether the provided `message` is `string` * type or whether it's the proper `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the * optional `template` has `[problem]` and `[fix]` tags. - * @returns The return value is an instance of an `ValidationError`. + * @param parser An optional `function` to convert property `value` of the provided `message` to a `string` during message creation. + * If it's not provided then the function uses the parser function of private property `#valueParser` of an instance that is set initially + * to the static `ValidationError.#defaultValueParser`. + * @returns The return value is an instance of a `ValidationError`. * @angularpackage */ public setMessage( - message: string | ErrorMessage, - callback?: ResultCallback + message: ErrorMessage, + callback?: ResultCallback, + parser: ValueParser = this.#valueParser ): this { - super.message = isString(message, callback) - ? message - : ValidationError.defineMessage( - { ...message, ...{ template: message.template || this.#tpl } }, - callback - ); - - isObject(message) - // Sets `fix`, `problem`, `value` and `template` from the provided `message`. - ? (this.setFix(message.fix).setProblem(message.problem).setValue(message.value as string), - isDefined(message.template) && this.setTemplate(message.template)) - // Sets `fix`, `problem` to empty string. - : (this.#fix = '', this.#problem = '', this.#value = ''); + super.message = ValidationError.defineMessage( + { ...message, ...{ template: message.template || this.#template } }, + (result, value, payload) => ( + isDefined(callback) && callback(result, value, payload), + isTrue(result) && this.assignMessageProperties(message), + result + ), + parser + ); return this; } /** - * Sets description of the validation problem. - * @param problem Description of validation problem guarded by `string` type. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `problem` is a + * Sets the description of the validation problem. + * @param problem Description of the validation problem guarded by `string` type and a replacement to the `[problem]` tag of `template`. + * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether the provided `problem` is a * `string`. - * @returns The return value is an instance of an `ValidationError`. + * @returns The return value is an instance of a `ValidationError`. * @angularpackage */ public setProblem(problem: string, callback?: ResultCallback): this { @@ -290,10 +405,11 @@ export class ValidationError extends Error { /** * Sets the template of validation error `message`. - * @param template A `message` template guarded by `string` type with replaceable `[problem]` and `[fix]` tags. + * @param template A `message` template guarded by `string` type with replaceable required `[problem]` and `[fix]` tags and optional + * `[id]` and `[value]` tags. * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a - * `string` that contains `[fix]` and `[problem]` tags. - * @returns The return value is an instance of an `ValidationError`. + * `string` that contains required `[fix]` and `[problem]` tags. + * @returns The return value is an instance of a `ValidationError`. * @angularpackage */ public setTemplate( @@ -301,52 +417,92 @@ export class ValidationError extends Error { callback?: ResultCallback ): this { ValidationError.#guardTemplate(template, callback) && - (this.#tpl = template); + (this.#template = template); return this; } /** - * Sets the value affected by the validation error (must be parsed to string type). - * @param value The value of `string` type as a replacement to the `[value]` tag of `template` that relates to the given `problem`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `value` is `string` - * type. - * @returns The return value is an instance of an `ValidationError`. + * Sets the `value` affected by the validation error (must be converted to string). + * @param value The value of `any` type as a replacement to the `[value]` tag of `template` that relates to the given `problem`. + * @returns The return value is an instance of a `ValidationError`. * @angularpackage */ - public setValue( - value: string, - callback?: ResultCallback + public setValue(value: any): this { + this.#value = value; + return this; + } + + /** + * Sets the function to automatically convert the `value` of `any` type to the `string` during message creation. The defined `function` is + * being used implicitly by the `updateMessage()` and `throw()` methods and and by the `setMessage()` method as the default value of + * parameter `parser`. + * @param parser The `function` of the `ValueParser` type, to convert the `value` of `any` type to `string`. + * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `parser` is `function`. + * @returns The return value is an instance of `ValidationError`. + * @angularpackage + */ + public setValueParser( + parser: ValueParser, + callback?: ResultCallback ): this { - guardString(value, callback) && (this.#value = value); + guardFunction(parser, callback) && (this.#valueParser = parser); return this; } /** - * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem` and `template` or optionally from the - * provided `message`. - * @param message An optional parameter of `string` type or an `object` of an `ErrorMessage` interface to build the message of `string` - * type. The value is checked against the proper `object` of `ErrorMessage`. + * Throws an error of new instance `ValidationError` with the message built from the stored required `fix`, `problem` and optional `id`, + * `template` and `value` properties or optionally from the provided `message`. + * @param message An optional `object` of an `ErrorMessage` interface to build the message of `string` type. The value is checked against + * the proper `object` of `ErrorMessage` and if it's a proper `object` then its properties are being assigned to the instance. * @angularpackage */ - public throw(message?: string | ErrorMessage): void { + public throw(message?: ErrorMessage): void { isDefined(message) ? this.setMessage(message) : this.updateMessage(); - throw this; + throw new ValidationError( + { + fix: this.#fix, + id: this.#id, + problem: this.#problem, + template: this.#template, + value: this.#value, + }, + this.#valueParser + ); } /** - * Updates the message with stored `fix`, `problem`, and `template` properties. - * @returns The return value is an instance of an `ValidationError`. + * Updates the `message` with stored required `fix`, `problem`, and optional `id`, `template`, `value` properties. + * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether stored properties used to build a + * message of `string` type are proper. + * @returns The return value is an instance of a `ValidationError`. * @angularpackage */ - public updateMessage(): this { - guardStringLength(this.#problem, { min: 1 }) && - (super.message = ValidationError.defineMessage({ + public updateMessage(callback?: ResultCallback): this { + super.message = ValidationError.defineMessage( + { fix: this.#fix, + id: this.#id, problem: this.#problem, - template: this.#tpl, + template: this.#template, value: this.#value, - })); + }, + callback, + this.#valueParser + ); return this; } //#endregion instance public methods. + + /** + * Assigns properties from the provided `message` to the instance. + * @param message An `object` of `ErrorMessage` to build a message of `string` type. + * @returns The return value is an instance of a `ValidationError`. + */ + private assignMessageProperties(message: ErrorMessage): this { + isDefined(message.id) && this.setId(message.id), + isDefined(message.value) && this.setValue(message.value), + isDefined(message.template) && this.setTemplate(message.template), + this.setFix(message.fix).setProblem(message.problem); + return this; + } } From 3a0380b494886cad6ede7cbd47eb2dc95cb8fd70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 16:04:43 +0200 Subject: [PATCH 061/179] refactor(ValueParser): move to `type` package. --- src/type/value-parser.type.ts | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/type/value-parser.type.ts diff --git a/src/type/value-parser.type.ts b/src/type/value-parser.type.ts deleted file mode 100644 index 31e13c5..0000000 --- a/src/type/value-parser.type.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * The type of function to convert the value of any type to `string`. - */ -export type ValueParser = (value: any) => string; From 913511ab807280d8ac636b3eb46bd22e5854d59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 16:04:52 +0200 Subject: [PATCH 062/179] docs(README.md): update. --- README.md | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 6885c87..7fd3012 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,7 @@ The value of [`any`][ts-any] type affected by the validation error, which must b The conversion is performed only during [`message`][error-property-message] creation and can be done in **two** ways, **automatic** or **manual**. * The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the [`setMessage()`][error-method-setmessage] method of an instance. -* The **automatic** by the previously defined function of the [`ValueParser`](#valueparser) type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage] method. +* The **automatic** by the previously defined function of the [`ValueParser`][package-type-valueparser] type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage] method. ```typescript public get value(): any { @@ -361,7 +361,7 @@ public set value(value: any) { Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). -> The **automatic** convert of the `value` of an [`ErrorMessage`](#errormessage) is performed by the defined function of the [`ValueParser`](#valueparser) type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method. +> The **automatic** convert of the `value` of an [`ErrorMessage`](#errormessage) is performed by the defined function of the [`ValueParser`][package-type-valueparser] type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method. ```typescript // Syntax. @@ -372,8 +372,7 @@ public static defineMessage( ): string { return callback( this.#guardMessage(message) && guardFunction(parser), - message, - this + message ) ? this.#buildMessage(message, parser) : ''; @@ -548,7 +547,7 @@ public static setValueParser( | Name: type | Description | | :--------------------------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type to convert the value of [`any`][ts-any] type to [`string`][js-string]. | +| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`][package-type-valueparser] type to convert the value of [`any`][ts-any] type to [`string`][js-string]. | | `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the result of the check whether the provided `parser` is [`function`][js-function] type. | **Returns:** @@ -605,7 +604,7 @@ constructor(message?: ErrorMessage, parser?: ValueParser) { isDefined(parser) && this.setValueParser(parser); // Initializes the message and assigns its properties to a new instance. - isDefined(message) && this.setMessage(message, undefined, parser); + isDefined(message) && this.setMessage(message); } ``` @@ -1079,7 +1078,7 @@ public setValueParser( | Name: type | Description | | :--------------------------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`](#valueparser) type, to **convert** the [`value`][error-property-value] of [`any`][ts-any] type to [`string`][js-string]. | +| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`][package-type-valueparser] type, to **convert** the [`value`][error-property-value] of [`any`][ts-any] type to [`string`][js-string]. | | `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `parser` is [`function`][js-function]. | **Returns:** @@ -1398,18 +1397,6 @@ An optional value affected by the validation error, which must be converted to [
-## Type - -#### `ValueParser` - -The type of [`function`][js-function] to convert the value of [`any`][ts-any] type to a [`string`][js-string]. - -```typescript -type ValueParser = (value: any) => string; -``` - -
- ## Experimental ![experimental] [![update]][error-github-changelog] @@ -1693,8 +1680,9 @@ MIT © angular-package ([license][error-license]) [type-github-readme]: https://github.com/angular-package/type#readme - [package-type-resultcallback]: https://github.com/angular-package/type#resultcallback + [package-type-valueparser]: https://github.com/angular-package/type#valueparser [package-type-key]: https://github.com/angular-package/type#key + [package-type-resultcallback]: https://github.com/angular-package/type#resultcallback From 28078b12528295fe2f4951156eb6c2761dd86eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 17:40:32 +0200 Subject: [PATCH 063/179] docs(README.md): update. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 7fd3012..0301023 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,12 @@ Run `ng build error` to build the package. The build artifacts will be stored in ### Running unit tests +Install `@angular-package/testing` with command: + +```typescript +npm i @angular-package/testing --no-save +``` + Run `ng test error` to execute the unit tests via [Karma](https://karma-runner.github.io).
From 32da9ef060bd84529858ddfc4176aaf70459b445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 17:40:48 +0200 Subject: [PATCH 064/179] docs(CHANGELOG.md): update 3.0.0. --- CHANGELOG.md | 72 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 476c594..b485660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,22 +6,59 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [3.0.0] - 2021-10-05 +## [3.0.0] - 2021-10-10 + +- Added `id` the identifier of the described `problem`, guarded by number type and it's a replacement for an optional tag [id] of the `template`. +- Added the `value` of any type affected by the validation error, which must be converted to a `string` to build a message and it's a replacement for an optional tag [value] of the `template`. +- Added `setTemplate()` static method to set the `template` of static `ValidationError` and as the default value for the `template` of an instance. +- Added `setValueParser()` static method to set the `function` to convert `any` value to `string` during message creation performed by the static `ValidationError.defineMessage()` method and as the default parser for the instance. +- Added `setId()` to set the `id` as an identifier of the described `problem`. +- Added `setValue()` to set the `value` affected by the validation error (must be converted to string). +- Added `setValueParser()` to sets the `function` to automatically convert the value of `any` type to the `string` during message creation. +- Updated `template` by adding optional `[id]`, `[value]` tags. +- Update `throw()` method to throw a **new** instance instead of this. ### 3.0.0 Added -- [53c2043] - Added `setValue()` method. - Added private property `#value` by default equal to `''`. - Added tag `[value]`. - Added `value` instance property that refers to private `#value`. +- [53c2043] [c00758a] + + **property `template`** + Added static public method `setTemplate()` to set the `template`. + Added tag `[id]` to the static private property `#defaultTemplate`. + + **property `value`** + Added private property `#value` by default equal to `''`. + Added `value` instance property that refers to private `#value`. + Added tag `[value]`. + Added `setValue()` method to set the `value` affected by the validation error. + Added static private `#defaultValueParser` property of the default value equal to `(value) => String(value)`. + Added private property `#valueParser` function of `ValueParser` type to convert the value of `any` type to `string` with the default value equal to static `ValidationError.#defaultValueParser`. + Added `setValueParser()` method of an instance to convert property `value` of an instance to `string` during message creation. + Added `setValueParser()` of static `ValidationError` to set value parser. + + **property `id`** + Added private property `#id` of an instance as an identifier of the described problem. + Added a public property `id` as an identifier of the described `problem`, guarded by `number` type, and as a replacement for an optional tag `[id]`. + Added tag `[id]` to static private property `#tags`. + Added public `setId()` method of an instance to set the `id`. - [285ef51] - Added property `template` of an instance that refers to private property `#tpl`. + Added property `template` of an instance that refers to private property `#tpl`. (changed to `#template` [c00758a]) Added private property `#tags` of `string[]` to replace the template `[problem]` and `[fix]` tags. ### 3.0.0 Changed +- [c00758a] + Changed `updateMessage()` by adding an optional `callback` parameter and use of value parser function. + Changed `setMessage()` method of an instance by adding an optional parameter `parser` of `ValueParser` type and the way it assigns the message properties to only when they are all proper. + Changed `throw()` method to throw new instance of `ValidationError` based on actual settings. + Changed `constructor()` by adding an optional `parser` parameter of `ValueParser` type that initialize the default parser for the instance. + + **property `template`** + Changed private property `#tpl` of an instance change name to `#template`. + Changed static private property `#template` change name to `#defaultTemplate`. + Changed public property `template` of an instance to refers to private property `#template` of an instance. (changes [285ef51]) + - [53c2043] Changed `updateMessage()` method to include `#value` property. @@ -29,28 +66,31 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Changed library to `es2020`. - [285ef51] [25ea72b] - Changed all conditionals to short if expressions. + Changed conditionals to short if expressions. Changed use of static `ValidationError` to `this`. Changed to `ResultCallback` with generic type variable `Value`. Changed usage of `is` and `guard` objects to use functions of `@angular-package/type`. - Changed the `setMessage()` method to set the `problem` and `fix` properties to empty string if the provided message is a `string` type. - Changed `setMessage()` method to use as first, template from the given `message` parameter, and if it's empty the private property `#tpl`. + ~~Changed the `setMessage()` method to set the `problem` and `fix` properties to empty string if the provided message is a `string` type~~. + Changed `setMessage()` method to use as first, template from the given `message` parameter, and if it's empty the private property ~~`#tpl`~~ `#template`. Changed `defineMessage()` static method to use private property `#tags` to replace them in the template. - [25ea72b] - Change `set` prefix to `replace` of methods. - -[53c2043]: https://github.com/angular-package/error/commit/53c20435df2225cb38e8d45d8f07e9c60db4d95a -[25ea72b]: https://github.com/angular-package/error/commit/25ea72b733939264b6546d01ef2945cc1e716fa5 -[8a0f58d]: https://github.com/angular-package/error/commit/8a0f58dae2b8a474848a2eccbe3218a0da0c017e -[285ef51]: https://github.com/angular-package/error/commit/285ef51805a3f1528b62d389214c106c3f213dfa + Changed `set` prefix to `replace` of methods. ### 3.0.0 Removed +- [c00758a] + Removed possibility to add `string` type message in the `constructor`, `throw()`, `setMessage()` methods of an instance. + - [7ed2b5c] [96c5563] [4af703a] Removed the default usage of callback. Removed `AllowedCallback` type because the removed default usage of callback. +[53c2043]: https://github.com/angular-package/error/commit/53c20435df2225cb38e8d45d8f07e9c60db4d95a +[25ea72b]: https://github.com/angular-package/error/commit/25ea72b733939264b6546d01ef2945cc1e716fa5 +[8a0f58d]: https://github.com/angular-package/error/commit/8a0f58dae2b8a474848a2eccbe3218a0da0c017e +[285ef51]: https://github.com/angular-package/error/commit/285ef51805a3f1528b62d389214c106c3f213dfa +[c00758a]: https://github.com/angular-package/error/commit/c00758a02cd32e61b813719df7ee8b0f7d18cb12 [7ed2b5c]: https://github.com/angular-package/error/commit/7ed2b5c90ed9922b8ced3622db4e0aa9b51afc38 [96c5563]: https://github.com/angular-package/error/commit/96c5563285f86eb2ce5d1ecff39aa2110507f220 [4af703a]: https://github.com/angular-package/error/commit/4af703afa7c2c432a902faad96fa535640bc5671 From 6b28e945199b135f213d4592eb4819fab12d7cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 17:43:06 +0200 Subject: [PATCH 065/179] docs(CHANGELOG.md): update 3.0.0. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b485660..10322d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `setId()` to set the `id` as an identifier of the described `problem`. - Added `setValue()` to set the `value` affected by the validation error (must be converted to string). - Added `setValueParser()` to sets the `function` to automatically convert the value of `any` type to the `string` during message creation. -- Updated `template` by adding optional `[id]`, `[value]` tags. +- Added `template` property with optional `[id]`, `[value]` tags. - Update `throw()` method to throw a **new** instance instead of this. ### 3.0.0 Added From 19705e176ecf0f1bcceaef86d1c325def51938fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 17:44:12 +0200 Subject: [PATCH 066/179] docs(CHANGELOG.md): update 3.0.0. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10322d6..6810057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `setValue()` to set the `value` affected by the validation error (must be converted to string). - Added `setValueParser()` to sets the `function` to automatically convert the value of `any` type to the `string` during message creation. - Added `template` property with optional `[id]`, `[value]` tags. -- Update `throw()` method to throw a **new** instance instead of this. +- Updated `throw()` method to throw a **new** instance instead of this. +- Removed possibility to throw just a `string` message. ### 3.0.0 Added From 75b6814cf14093315d16693c9228ca833977860b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 17:46:33 +0200 Subject: [PATCH 067/179] style(CHANGELOG.md): fix. --- CHANGELOG.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6810057..cddcbf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,15 +32,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Added `value` instance property that refers to private `#value`. Added tag `[value]`. Added `setValue()` method to set the `value` affected by the validation error. - Added static private `#defaultValueParser` property of the default value equal to `(value) => String(value)`. - Added private property `#valueParser` function of `ValueParser` type to convert the value of `any` type to `string` with the default value equal to static `ValidationError.#defaultValueParser`. + Added static private `#defaultValueParser` property of the default value equal to `(value) => String(value)`. + Added private property `#valueParser` function of `ValueParser` type to convert the value of `any` type to `string` with the default value equal to static `ValidationError.#defaultValueParser`. Added `setValueParser()` method of an instance to convert property `value` of an instance to `string` during message creation. Added `setValueParser()` of static `ValidationError` to set value parser. **property `id`** - Added private property `#id` of an instance as an identifier of the described problem. + Added private property `#id` of an instance as an identifier of the described problem. Added a public property `id` as an identifier of the described `problem`, guarded by `number` type, and as a replacement for an optional tag `[id]`. - Added tag `[id]` to static private property `#tags`. + Added tag `[id]` to static private property `#tags`. Added public `setId()` method of an instance to set the `id`. - [285ef51] @@ -50,14 +50,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### 3.0.0 Changed - [c00758a] - Changed `updateMessage()` by adding an optional `callback` parameter and use of value parser function. - Changed `setMessage()` method of an instance by adding an optional parameter `parser` of `ValueParser` type and the way it assigns the message properties to only when they are all proper. - Changed `throw()` method to throw new instance of `ValidationError` based on actual settings. + Changed `updateMessage()` by adding an optional `callback` parameter and use of value parser function. + Changed `setMessage()` method of an instance by adding an optional parameter `parser` of `ValueParser` type and the way it assigns the message properties to only when they are all proper. + Changed `throw()` method to throw new instance of `ValidationError` based on actual settings. Changed `constructor()` by adding an optional `parser` parameter of `ValueParser` type that initialize the default parser for the instance. - **property `template`** + **property `template`** Changed private property `#tpl` of an instance change name to `#template`. - Changed static private property `#template` change name to `#defaultTemplate`. + Changed static private property `#template` change name to `#defaultTemplate`. Changed public property `template` of an instance to refers to private property `#template` of an instance. (changes [285ef51]) - [53c2043] @@ -66,7 +66,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [8a0f58d] Changed library to `es2020`. -- [285ef51] [25ea72b] +- [285ef51] [25ea72b] Changed conditionals to short if expressions. Changed use of static `ValidationError` to `this`. Changed to `ResultCallback` with generic type variable `Value`. @@ -84,7 +84,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Removed possibility to add `string` type message in the `constructor`, `throw()`, `setMessage()` methods of an instance. - [7ed2b5c] [96c5563] [4af703a] - Removed the default usage of callback. + Removed the default usage of callback. Removed `AllowedCallback` type because the removed default usage of callback. [53c2043]: https://github.com/angular-package/error/commit/53c20435df2225cb38e8d45d8f07e9c60db4d95a From 579b840cf8146c5a82412492d8628427f14cf9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 17:53:21 +0200 Subject: [PATCH 068/179] docs(CHANGELOG.md): fix. --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cddcbf6..4c779b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Added `value` instance property that refers to private `#value`. Added tag `[value]`. Added `setValue()` method to set the `value` affected by the validation error. + + **property `#valueParser`** Added static private `#defaultValueParser` property of the default value equal to `(value) => String(value)`. Added private property `#valueParser` function of `ValueParser` type to convert the value of `any` type to `string` with the default value equal to static `ValidationError.#defaultValueParser`. Added `setValueParser()` method of an instance to convert property `value` of an instance to `string` during message creation. @@ -56,8 +58,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Changed `constructor()` by adding an optional `parser` parameter of `ValueParser` type that initialize the default parser for the instance. **property `template`** - Changed private property `#tpl` of an instance change name to `#template`. - Changed static private property `#template` change name to `#defaultTemplate`. + Changed private property name `#tpl` of an instance to `#template`. + Changed static private property name `#template` to `#defaultTemplate`. Changed public property `template` of an instance to refers to private property `#template` of an instance. (changes [285ef51]) - [53c2043] From 1bd1af47feed7f3d6a3bb2e927a403b1747d5dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 18:28:32 +0200 Subject: [PATCH 069/179] refactor(Parameter): remove interface. --- src/message-builder/interface/parameter.interface.ts | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/message-builder/interface/parameter.interface.ts diff --git a/src/message-builder/interface/parameter.interface.ts b/src/message-builder/interface/parameter.interface.ts deleted file mode 100644 index 8d17d26..0000000 --- a/src/message-builder/interface/parameter.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface Parameter { - name: string; - type: string; -} From c3e42eb9ec6e822e4aa10cc78e94c943533c5014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sat, 9 Oct 2021 18:32:19 +0200 Subject: [PATCH 070/179] refactor(Parameter): bring back `Parameter` interface 37 (b***h) --- src/message-builder/interface/parameter.interface.ts | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/message-builder/interface/parameter.interface.ts diff --git a/src/message-builder/interface/parameter.interface.ts b/src/message-builder/interface/parameter.interface.ts new file mode 100644 index 0000000..8d17d26 --- /dev/null +++ b/src/message-builder/interface/parameter.interface.ts @@ -0,0 +1,4 @@ +export interface Parameter { + name: string; + type: string; +} From 79bd9af0a76f9baa84914117a7d9c7a8d4319c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Aeebor=20Rudnicki?= Date: Sun, 10 Oct 2021 15:34:40 +0200 Subject: [PATCH 071/179] docs(README.md): update. --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 0301023..a0b64f8 100644 --- a/README.md +++ b/README.md @@ -85,10 +85,19 @@ This package was built by the [library skeleton][skeleton] which was generated w Copy this package to the `packages/error` folder of the [library skeleton][skeleton] then run the commands below. +### Code scaffolding + +Run `ng generate component component-name --project error` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project storage`. +> Note: Don't forget to add `--project error` or else it will be added to the default project in your `angular.json` file. + ### Build Run `ng build error` to build the package. The build artifacts will be stored in the `dist/` directory. +### Publishing + +After building your library with `ng build error`, go to the dist folder `cd dist/error` and run `npm publish`. + ### Running unit tests Install `@angular-package/testing` with command: From 60f07a340cfeeca997cf2b8541971f2a2e24b0dc Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 18 Dec 2021 23:46:03 +0100 Subject: [PATCH 072/179] refactor(MessageBuilder): move to text package. --- src/message-builder/index.ts | 3 - .../interface/message-template.interface.ts | 3 - .../interface/parameter.interface.ts | 4 - .../src/message-builder-template.class.ts | 29 ---- .../src/message-builder.class.ts | 131 ------------------ .../src/message-function-builder.class.ts | 97 ------------- .../test/message-builder.spec.ts | 66 --------- .../test/message-function-builder.spec.ts | 38 ----- 8 files changed, 371 deletions(-) delete mode 100644 src/message-builder/index.ts delete mode 100644 src/message-builder/interface/message-template.interface.ts delete mode 100644 src/message-builder/interface/parameter.interface.ts delete mode 100644 src/message-builder/src/message-builder-template.class.ts delete mode 100644 src/message-builder/src/message-builder.class.ts delete mode 100644 src/message-builder/src/message-function-builder.class.ts delete mode 100644 src/message-builder/test/message-builder.spec.ts delete mode 100644 src/message-builder/test/message-function-builder.spec.ts diff --git a/src/message-builder/index.ts b/src/message-builder/index.ts deleted file mode 100644 index 6a9974f..0000000 --- a/src/message-builder/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { MessageBuilder } from './src/message-builder.class'; -export { MessageBuilderTemplate } from './src/message-builder-template.class'; -export { MessageFunctionBuilder } from './src/message-function-builder.class'; diff --git a/src/message-builder/interface/message-template.interface.ts b/src/message-builder/interface/message-template.interface.ts deleted file mode 100644 index a2388da..0000000 --- a/src/message-builder/interface/message-template.interface.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface MessageTemplate { - [index: string]: string; -} diff --git a/src/message-builder/interface/parameter.interface.ts b/src/message-builder/interface/parameter.interface.ts deleted file mode 100644 index 8d17d26..0000000 --- a/src/message-builder/interface/parameter.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface Parameter { - name: string; - type: string; -} diff --git a/src/message-builder/src/message-builder-template.class.ts b/src/message-builder/src/message-builder-template.class.ts deleted file mode 100644 index 9d46858..0000000 --- a/src/message-builder/src/message-builder-template.class.ts +++ /dev/null @@ -1,29 +0,0 @@ -// @angular-package/type -import { isString } from '@angular-package/type'; -// Interface. -import { MessageTemplate } from '../interface/message-template.interface'; -/** - * `MessageBuilderTemplate`. - */ -export class MessageBuilderTemplate { - // argument value is [value.type] type, must be [param.type] type - - #template: MessageTemplate = { - class: `[class][method]([param.name][param.type])[return]`, - function: `[function]([param.name][param.type])[return]`, - method: `[method]([param.name][param.type])[return]` - }; - - #type!: keyof MessageTemplate; - - /** - * Gets privately stored template of the provided in the constructor type. - */ - get get(): string { - return this.#template[this.#type]; - } - - constructor(type: 'class' | 'function' | 'method') { - isString(type) && (this.#type = type); - } -} diff --git a/src/message-builder/src/message-builder.class.ts b/src/message-builder/src/message-builder.class.ts deleted file mode 100644 index 4b7c471..0000000 --- a/src/message-builder/src/message-builder.class.ts +++ /dev/null @@ -1,131 +0,0 @@ -// @angular-package/type -import { - ResultCallback, - guardString, - isDefined, - isString, -} from '@angular-package/type'; -// Class. -import { MessageBuilderTemplate } from './message-builder-template.class'; -/** - * Message builder to build `class`, `function` and `method` of a `string` type. - * @version experimental - */ -export class MessageBuilder { - /** - * An object with regexp to replace. - */ - #regExp = { - class: /\[class\]/i, - function: /\[function\]/i, - method: /\[method\]/i, - param: { - name: /\[param.name\]/i, - type: /\[param.type\]/i, - }, - return: /\[return\]/i, - }; - - /** - * - */ - #template: string; - - /** - * - */ - get get(): string { - return this.#template; - } - - /** - * - * @param template - */ - constructor(template: 'class' | 'function' | 'method') { - this.#template = new MessageBuilderTemplate(template).get; - } - - /** - * - * @param name - * @param callback - * @returns - */ - public replaceClassName(name: string, callback?: ResultCallback): this { - guardString(name, callback) && this.replace(this.#regExp.class, name); - return this; - } - - /** - * - * @param name - * @param callback - * @returns - */ - public replaceFunctionName(name: string, callback?: ResultCallback): this { - guardString(name, callback) && this.replace(this.#regExp.function, name); - return this; - } - - /** - * - * @param name - * @param callback - * @returns - */ - public replaceMethodName(name: string, callback?: ResultCallback): this { - guardString(name, callback) && this.replace(this.#regExp.method, name); - return this; - } - - /** - * - * @param name - * @param type - * @returns - */ - public replaceParam(name: string, type: string = ''): this { - if (guardString(name)) { - const param = `${name}${type}`; - this.replace(this.#regExp.param.name, name).replace( - this.#regExp.param.type, - type - ); - - if (type.length > 0) { - this.replace(type, `: ${type}`); - } - } - return this; - } - - /** - * - * @param returns - * @param callback - * @returns - */ - public replaceReturn(returns: string, callback?: ResultCallback): this { - guardString(returns, callback) && - this.replace( - this.#regExp.return, - returns.length > 0 ? `: ${returns}` : returns - ); - return this; - } - - /** - * - * @param searchValue - * @param replaceValue - * @returns - */ - private replace(searchValue: string | RegExp, replaceValue: string): this { - isDefined(searchValue) && (this.#template = this.#template.replace( - searchValue, - isString(replaceValue) ? replaceValue : '' - )); - return this; - } -} diff --git a/src/message-builder/src/message-function-builder.class.ts b/src/message-builder/src/message-function-builder.class.ts deleted file mode 100644 index 68aaac7..0000000 --- a/src/message-builder/src/message-function-builder.class.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { guardString, isString, ResultCallback } from '@angular-package/type'; -// Class. -import { MessageBuilder } from './message-builder.class'; -// Interface. -import { Parameter } from '../interface/parameter.interface'; -/** - * Message builder to build a `function` of a `string` type. - * @version experimental - */ -export class MessageFunctionBuilder { - #messageBuilder: MessageBuilder; - #name = ''; - #param: Parameter = { - name: '', - type: '', - }; - #return = ''; - - /** - * Gets the build function of a `string` type. - */ - get get(): string { - return this.#messageBuilder.get; - } - - get param(): string { - return this.#param.name; - } - - get name(): string { - return this.#name; - } - - get return(): string { - return this.#return; - } - - /** - * Creates an instance of `MessageFunctionBuilder`. - */ - constructor() { - this.#messageBuilder = new MessageBuilder('function'); - } - - /** - * Builds string-type function from the privately stored `name`, `param`, and `return`. - */ - public build(): this { - this.#messageBuilder - .replaceFunctionName(this.#name) - .replaceParam(this.#param.name, this.#param.type) - .replaceReturn(this.#return); - return this; - } - - /** - * Sets the function name to build function of a `string` type. - * @param name Function name of a `string` type. - * @param callback An optional callback function of `ResultCallback` to handle the result of the check whether the provided `name` - * is a `string` type. - * @returns The return value is an instance of `MessageFunctionBuilder`. - */ - public setName(name: string, callback?: ResultCallback): this { - guardString(name, callback) && (this.#name = name); - return this; - } - - /** - * Sets param name with an optional type of `function` to build function of a `string` type. - * @param name Parameter name of a function guarded by string. - * @param type An optional parameter type of function guarded by string. - * @param callback An optional callback function of `ResultCallback` to handle the result of the check whether the provided `name` - * is a `string` type. - * @returns The return value is an instance of `MessageFunctionBuilder`. - */ - public setParam( - name: string, - type?: string, - callback?: ResultCallback - ): this { - guardString(name, callback) && - ((this.#param.name = name), isString(type) && (this.#param.type = type)); - return this; - } - - /** - * Sets return type to build the function of a `string` type. - * @param returns A return type of `function` guarded by `string`. - * @param callback An optional callback function of `ResultCallback` to handle the result of the check whether the provided `returns` - * is a `string` type. - * @returns The return value is an instance of `MessageFunctionBuilder`. - */ - public setReturn(returns: string, callback?: ResultCallback): this { - guardString(returns, callback) && (this.#return = returns); - return this; - } -} diff --git a/src/message-builder/test/message-builder.spec.ts b/src/message-builder/test/message-builder.spec.ts deleted file mode 100644 index 4be39d8..0000000 --- a/src/message-builder/test/message-builder.spec.ts +++ /dev/null @@ -1,66 +0,0 @@ -// External class. -import { Testing, TestingToBeMatchers } from '@angular-package/testing'; -// Class. -import { MessageBuilder } from '../src/message-builder.class'; -/** - * Initialize `Testing`. - */ -const testing = new Testing(true, true); -const toBe = new TestingToBeMatchers(); -/** - * Tests. - */ -testing.describe(`MessageBuilder`, () => { - let messageClassBuilder = new MessageBuilder('class'); - let messageFunctionBuilder = new MessageBuilder('function'); - let messageMethodBuilder = new MessageBuilder('method'); - - beforeEach(() => (messageClassBuilder = new MessageBuilder('class'))); - beforeEach(() => (messageFunctionBuilder = new MessageBuilder('function'))); - beforeEach(() => (messageMethodBuilder = new MessageBuilder('method'))); - - // Basic testing. - testing - .it(`defined`, () => - toBe - .defined(MessageBuilder) - .defined(messageClassBuilder) - .defined(messageFunctionBuilder) - .defined(messageMethodBuilder) - .instance(messageClassBuilder, MessageBuilder) - .instance(messageFunctionBuilder, MessageBuilder) - .instance(messageMethodBuilder, MessageBuilder) - ) - .toBeClass(MessageBuilder); - - testing.it(`build function`, () => { - messageFunctionBuilder - .replaceFunctionName('guardString') - .replaceParam('value', 'string') - .replaceReturn('boolean'); - - toBe.string(messageFunctionBuilder.get); - expect(messageFunctionBuilder.get).toEqual('guardString(value: string): boolean'); - }); - - testing.it(`build class`, () => { - messageClassBuilder - .replaceClassName('Person.prototype.') - .replaceMethodName('setPerson') - .replaceParam('value?', 'object') - .replaceReturn('object'); - - toBe.string(messageClassBuilder.get); - expect(messageClassBuilder.get).toEqual('Person.prototype.setPerson(value?: object): object'); - }); - - testing.it(`build method`, () => { - messageMethodBuilder - .replaceMethodName('setPerson') - .replaceParam('value', 'string') - .replaceReturn('this'); - - toBe.string(messageMethodBuilder.get); - expect(messageMethodBuilder.get).toEqual('setPerson(value: string): this'); - }); -}); diff --git a/src/message-builder/test/message-function-builder.spec.ts b/src/message-builder/test/message-function-builder.spec.ts deleted file mode 100644 index 6e0c2db..0000000 --- a/src/message-builder/test/message-function-builder.spec.ts +++ /dev/null @@ -1,38 +0,0 @@ -// External class. -import { Testing, TestingToBeMatchers } from '@angular-package/testing'; -// Class. -import { MessageFunctionBuilder } from '../src/message-function-builder.class'; -/** - * Initialize `Testing`. - */ -const testing = new Testing(true, true); -const toBe = new TestingToBeMatchers(); -/** - * Tests. - */ -testing.describe(`MessageFunctionBuilder`, () => { - let messageFunctionBuilder = new MessageFunctionBuilder(); - - beforeEach(() => (messageFunctionBuilder = new MessageFunctionBuilder())); - - // Basic testing. - testing - .it(`defined`, () => - toBe - .defined(messageFunctionBuilder) - .defined(messageFunctionBuilder) - .instance(messageFunctionBuilder, MessageFunctionBuilder) - ) - .toBeClass(MessageFunctionBuilder); - - testing.it(`build function`, () => { - messageFunctionBuilder - .setName('guardString') - .setParam('value', 'string') - .setReturn('boolean') - .build(); - - toBe.string(messageFunctionBuilder.get); - expect(messageFunctionBuilder.get).toEqual('guardString(value: string): boolean'); - }); -}); From b2616625bb80790f97da9138f75305ceb3c55af2 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 18 Dec 2021 23:46:36 +0100 Subject: [PATCH 073/179] refactor(ValidationError): the new use of text package. --- src/lib/validation-error.class.ts | 515 +++--------------------------- 1 file changed, 42 insertions(+), 473 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index a718e0c..3a406d5 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,508 +1,77 @@ // @angular-package/type. import { - // Type. - ResultCallback, - ValueParser, // Function. - areString, - guardFunction, - guardNumber, - guardObjectKeys, - guardString, - guardStringIncludes, - isDefined, - isTrue, + guardObject, + isInstance, + isString, } from '@angular-package/type'; +// Class. +// import { Text } from '@angular-package/text'; +import { Text } from '@angular-package/text'; + // Interface. import { ErrorMessage } from '../interface/error-message.interface'; /** - * Manages an `Error` of validation. + * The `ValidationError` object to throw an error with a solution to the described problem. */ export class ValidationError extends Error { - //#region static private properties - /** - * Tags to be replaced. - */ - static #tags = ['fix', 'id', 'problem', 'value']; - - /** - * A static, privately stored template of the error `message`. - */ - static #defaultTemplate = `Problem[id]: [problem] => Fix: [fix]`; - //#endregion static private properties - - //#region instance private properties. - /** - * A privately stored possible solution to the described problem of `string` type. - * * By default, it's an empty `string`. - */ - #fix = ''; - - /** - * A privately stored identifier of the described problem. By default, it's `0`. - */ - #id = 0; - - /** - * A privately stored validation problem of `string` type. - * * By default, it's an empty `string`. - */ - #problem = ''; - - /** - * A string-type privately stored template of the error message that contains replaceable required `[fix]` and `[problem]` tags and - * optional `[id]` and `[value]`. - * * By default, it's set to `Problem: [problem] => Fix: [fix]`. - */ - #template = ValidationError.template; - - /** - * The value affected by the validation error. - */ - #value = ''; - - /** - * Private function to convert the value of any type to `string`. - */ - #valueParser: ValueParser = ValidationError.#defaultValueParser; - //#endregion instance private properties. + public static template = `Problem[id]: [problem] => Fix: [fix], got [value]`; - //#region static public properties - /** - * A template of static `ValidationError` and the default value of `template` of an instance for the error message guarded by `string` - * type with the replaceable required `[problem]` and `[fix]` tags and optional `[id]` and `[value]`. - * * By default, it's set to `Problem: [problem] => Fix: [fix]`. - * * The value is being checked against the existence of `[problem]` and `[fix]` tags. - * * Possible tags to be replaced `[fix]`, `[id]`, `[problem]`, `[value]`. - */ - static get template(): string { - return this.#defaultTemplate; - } - static set template(value: string) { - this.#guardTemplate(value) && (this.#defaultTemplate = value); - } - //#endregion static public properties + public name = ValidationError.name; - //#region instance public properties. - /** - * A possible solution to the described `problem` of validation that is guarded by `string` and it's a replacement for the required tag - * `[fix]` of the `template`. - * * It can be set directly or by the `setFix()`, `setMessage()` and `throw()` methods. By default, it's an empty string. - */ public get fix(): string { return this.#fix; } - public set fix(value: string) { - guardString(value) && (this.#fix = value); - } - /** - * The identifier of the described `problem`, guarded by `number` type and it's a replacement for an optional tag `[id]` of the - * `template`. - * * It refers to `[id]` tag. - * * It can be set directly or by the `setId()`, `setMessage()` and `throw()` methods of an instance. By default, it's 0. - */ - public get id(): number { + public get id(): string | undefined { return this.#id; } - public set id(value: number) { - guardNumber(value) && (this.#id = value); - } - - /** - * A validation error `message` guarded by `string` type that can be built from the required `problem` and `fix` with optional `id` and - * `value` on the `template` of an instance of `ValidationError`. - * * It can be set directly or by the `setMessage()` and `throw()` methods. - */ - public get message(): string { - return super.message; - } - public set message(value: string) { - guardString(value) && (super.message = value); - } - - /** - * Error name of `string` type that is being thrown. - * * By default, it's `ValidationError`. - */ - public name = ValidationError.name; - /** - * Description of the validation problem guarded by `string` type and it's a replacement for the required tag `[problem]` of the - * `template`. - * * It refers to `[problem]` tag. - * * It can be set directly or by the `setProblem()`, `setMessage()` and `throw()` methods. By default, it's an empty string. - */ public get problem(): string { return this.#problem; } - public set problem(value: string) { - guardString(value) && (this.#problem = value); - } - /** - * A template of the error `message` guarded by `string` type with the replaceable required `[problem]` and `[fix]` tags and optional - * `[id]` and `[value]` tags. The value is being checked against the existence of required `[problem]` and `[fix]` tags by the static - * private `ValidationError.#guardTemplate()` method. - * * It can be set directly or by the `setTemplate()`, `setMessage()` and `throw()` methods, and its initial value is from the static - * `ValidationError.template`. - * * By default, it's set to `Problem: [problem] => Fix: [fix]`. - * * Possible tags to use `[fix]`, `[id]`, `[problem]` and `[value]`. - */ - public get template(): string { - return this.#template; - } - public set template(value: string) { - ValidationError.#guardTemplate(value) && (this.#template = value); - } - - /** - * The value of any type affected by the validation error, which must be converted to a `string` to build a `message` and it's a - * replacement for an optional tag `[value]` of the `template`. - * * It can be set directly or by the `setValue()`, `setMessage()` and `throw()` methods. - * * It refers to `[value]` tag. - * * The conversion is performed only during message creation and can be done in two ways, automatic or manual. - * * The manual, by providing parser function as parameter (`parser?: ValueParser`) to the `setMessage()` method of an instance. - * * The automatic by the previously defined function of the `ValueParser` type, set by the `setValueParser()` method of an instance to - * automatize `setMessage()` method. - */ - public get value(): any { + public get value(): string | undefined { return this.#value; } - public set value(value: any) { - this.#value = value; - } - //#endregion instance public properties. - //#region static public methods - /** - * Defines the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface. The message is built - * on the provided `template` or the template from the static property `ValidationError.template`. - * * The automatic convert of the `value` of an `ErrorMessage` is performed by the defined function of the `ValueParser` type, set by the - * static `ValidationError.setValueParser()` method. - * @param message An `object` of an `ErrorMessage` interface to build a message of `string` type. The value is checked against the proper - * `object` of `ErrorMessage` by the static private `ValidationError.#guarMessage()` method. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message is the proper - * `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the optional `template` property has - * required `[problem]` and `[fix]` tags and parser is a `function` type. - * @param parser An optional `function` to convert property `value` from the provided `message` to `string` during message creation. It - * can be set by static `ValidationError.setValueParser()` as the default value parser for static and instance `ValidationError`. - * @returns The return value is a message of `string` type created from the provided `message` of the `ErrorMessage` interface or it's an - * empty `string` if the provided message object isn't proper. - * @angularpackage - */ - public static defineMessage( - message: ErrorMessage, - callback: ResultCallback = (result) => result, - parser: ValueParser = this.#defaultValueParser - ): string { - return callback( - this.#guardMessage(message) && guardFunction(parser), - message - ) - ? this.#buildMessage(message, parser) - : ''; - } - - /** - * Sets the `template` of static `ValidationError` and the default value for `template` of an instance. - * @param template A template for error `message` guarded by `string` type with replaceable required `[problem]` and `[fix]` tags and - * optional `[id]` and `[value]`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a - * `string` that contains required `[fix]` and `[problem]` tags. - * @returns The return value is static `ValidationError`. - * @angularpackage - */ - public static setTemplate( - template: string, - callback?: ResultCallback - ): typeof ValidationError { - ValidationError.#guardTemplate(template, callback) && - (this.#defaultTemplate = template); - return ValidationError; - } - - /** - * Sets the `function` to convert any value to `string` during message creation performed by the static `defineMessage()` method and as - * the initial parser for the instance. - * @param parser The `function` of the `ValueParser` type to convert the value of `any` type to `string`. - * @param callback An optional callback function of `ResultCallback` type to handle the result of the check whether the provided `parser` - * is `function` type. - * @returns The return value is static `ValidationError`. - * @angularpackage - */ - public static setValueParser( - parser: ValueParser, - callback?: ResultCallback - ): typeof ValidationError { - guardFunction(parser, callback) && (this.#defaultValueParser = parser); - return ValidationError; - } - //#endregion static public methods - - //#region static private methods. - /** - * Builds the validation error message of `string` type based on the provided `message` of the `ErrorMessage` interface. - * @param message An `object` of an `ErrorMessage` interface to build a message of `string` type. The value is not checked against the - * proper `object`. - * @param parser The required `function` to convert property `value` from the provided `message` to `string` during message creation. - * @returns The return value is a message of `string` type created from the provided `message` parameter. - * @angularpackage - */ - static #buildMessage(message: ErrorMessage, parser: ValueParser): string { - let m = message.template || this.template; - return ( - this.#tags.forEach( - (tag) => - (m = m.replace( - `[${tag}]`, - ( - { - fix: (value: any): string => String(value), - id: (value: any): string => String(value || ''), - problem: (value: any): string => String(value), - value: (value: any): string => parser(value), - } as { [index: string]: ValueParser } - )[tag](message[tag as keyof Omit]) - )) - ), - m - ); - } - - /** - * The default parser `function` of static `ValidationError` and the instance. - */ - static #defaultValueParser: ValueParser = (value) => String(value); - - /** - * Guards the provided message to be of `ErrorMessage` shape. - * @param message An object of the `ErrorMessage` interface to build the message of `string` type. The value is checked against the proper - * `object`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `message` contains - * required `problem`, `fix` properties and the optional `template` has `[problem]` and `[fix]` tags. - * @returns The return value is a `boolean` indicating whether the provided `message` is an object of `ErrorMessage`. - * @angularpackage - */ - static #guardMessage( - message: ErrorMessage, - callback: ResultCallback = (result) => result - ): message is ErrorMessage { - return callback( - guardObjectKeys(message, ['fix', 'problem']) && - areString(message.fix, message.problem).every() - ? isDefined(message.template) - ? this.#guardTemplate(message.template) - : true - : false, - message - ); - } - - /** - * Guards the provided `template` to be `string` type that includes `[fix]` and `[problem]` tags. - * @param template A string-type value to guard. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided message includes - * `[fix]` and `[problem]` tags. - * @returns The return value is a `boolean` indicating whether the provided `template` is a `string` that includes `[fix]` and `[problem]` - * tags. - * @angularpackage - */ - static #guardTemplate( - template: string, - callback?: ResultCallback - ): template is string { - return guardStringIncludes(template, ['[fix]', '[problem]'], callback); + public get template(): string { + return this.#template; } - //#endregion static private methods. - - /** - * Creates a new instance with an optional message of `ErrorMessage` interface. - * @param message An optional `message` of an `object` of the `ErrorMessage` interface to build a message of `string` and throw with - * an `Error`. Its properties are assigned to the instance. - * @param parser An optional `function` to convert property `value` of the provided `message` of `ErrorMessage` to a `string` during - * message creation and set as the default value parser for the instance. By default, it uses the parser function of private property - * `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. - * @angularpackage - */ - constructor(message?: ErrorMessage, parser?: ValueParser) { - super(); - // Sets the parser. - isDefined(parser) && this.setValueParser(parser); + #fix: string; + #id?: string; + #problem: string; + #value?: string; + #template: string; - // Initializes the message and assigns its properties to a new instance. - isDefined(message) && this.setMessage(message); + public static isValidationError(value: any): value is ValidationError { + return isInstance(value, ValidationError); } - //#region instance public methods. - /** - * Sets the `fix` a possible solution to the described `problem`. - * @param fix A possible solution to the described `problem` guarded by `string` type and a replacement to the `[fix]` tag of `template`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `fix` is a `string`. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public setFix(fix: string, callback?: ResultCallback): this { - guardString(fix, callback) && (this.#fix = fix); - return this; - } - - /** - * Sets the `id` an identifier of the described problem. - * @param id The identifier, guarded by a `number` type and a replacement to the `[id]` tag of `template`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `id` is `number`. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public setId(id: number, callback?: ResultCallback): this { - guardNumber(id, callback) && (this.#id = id); - return this; + public static messageTemplate( + tsa: TemplateStringsArray, + ...values: any[] + ): string { + let message: ErrorMessage, template: string; + [message, template] = values; + return new Text(template, ['fix', 'id', 'problem', 'value'], `[`, `]`) + .replace('fix', message?.fix) + .replace('id', message?.id || '') + .replace('problem', message.problem) + .replace('value', message.value) + .getText(); } - /** - * Sets the validation error message of `string` type from the provided `message` of the `ErrorMessage` interface. - * @param message An `object` of an `ErrorMessage` interface to build the message of `string` type. The value is checked against the - * proper `object` of `ErrorMessage` and if it's a proper object then its properties are being assigned to the instance. - * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether the provided `message` is `string` - * type or whether it's the proper `object` of `ErrorMessage` which means it contains required `problem`, `fix` properties, and the - * optional `template` has `[problem]` and `[fix]` tags. - * @param parser An optional `function` to convert property `value` of the provided `message` to a `string` during message creation. - * If it's not provided then the function uses the parser function of private property `#valueParser` of an instance that is set initially - * to the static `ValidationError.#defaultValueParser`. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public setMessage( + constructor( message: ErrorMessage, - callback?: ResultCallback, - parser: ValueParser = this.#valueParser - ): this { - super.message = ValidationError.defineMessage( - { ...message, ...{ template: message.template || this.#template } }, - (result, value, payload) => ( - isDefined(callback) && callback(result, value, payload), - isTrue(result) && this.assignMessageProperties(message), - result - ), - parser - ); - return this; - } - - /** - * Sets the description of the validation problem. - * @param problem Description of the validation problem guarded by `string` type and a replacement to the `[problem]` tag of `template`. - * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether the provided `problem` is a - * `string`. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public setProblem(problem: string, callback?: ResultCallback): this { - guardString(problem, callback) && (this.#problem = problem); - return this; - } - - /** - * Sets the template of validation error `message`. - * @param template A `message` template guarded by `string` type with replaceable required `[problem]` and `[fix]` tags and optional - * `[id]` and `[value]` tags. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `template` is a - * `string` that contains required `[fix]` and `[problem]` tags. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public setTemplate( - template: string, - callback?: ResultCallback - ): this { - ValidationError.#guardTemplate(template, callback) && - (this.#template = template); - return this; - } - - /** - * Sets the `value` affected by the validation error (must be converted to string). - * @param value The value of `any` type as a replacement to the `[value]` tag of `template` that relates to the given `problem`. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public setValue(value: any): this { - this.#value = value; - return this; - } - - /** - * Sets the function to automatically convert the `value` of `any` type to the `string` during message creation. The defined `function` is - * being used implicitly by the `updateMessage()` and `throw()` methods and and by the `setMessage()` method as the default value of - * parameter `parser`. - * @param parser The `function` of the `ValueParser` type, to convert the `value` of `any` type to `string`. - * @param callback An optional callback function of `ResultCallback` type to handle the check whether the provided `parser` is `function`. - * @returns The return value is an instance of `ValidationError`. - * @angularpackage - */ - public setValueParser( - parser: ValueParser, - callback?: ResultCallback - ): this { - guardFunction(parser, callback) && (this.#valueParser = parser); - return this; - } - - /** - * Throws an error of new instance `ValidationError` with the message built from the stored required `fix`, `problem` and optional `id`, - * `template` and `value` properties or optionally from the provided `message`. - * @param message An optional `object` of an `ErrorMessage` interface to build the message of `string` type. The value is checked against - * the proper `object` of `ErrorMessage` and if it's a proper `object` then its properties are being assigned to the instance. - * @angularpackage - */ - public throw(message?: ErrorMessage): void { - isDefined(message) ? this.setMessage(message) : this.updateMessage(); - throw new ValidationError( - { - fix: this.#fix, - id: this.#id, - problem: this.#problem, - template: this.#template, - value: this.#value, - }, - this.#valueParser - ); - } - - /** - * Updates the `message` with stored required `fix`, `problem`, and optional `id`, `template`, `value` properties. - * @param callback An optional callback `function` of `ResultCallback` type to handle the check whether stored properties used to build a - * message of `string` type are proper. - * @returns The return value is an instance of a `ValidationError`. - * @angularpackage - */ - public updateMessage(callback?: ResultCallback): this { - super.message = ValidationError.defineMessage( - { - fix: this.#fix, - id: this.#id, - problem: this.#problem, - template: this.#template, - value: this.#value, - }, - callback, - this.#valueParser - ); - return this; - } - //#endregion instance public methods. - - /** - * Assigns properties from the provided `message` to the instance. - * @param message An `object` of `ErrorMessage` to build a message of `string` type. - * @returns The return value is an instance of a `ValidationError`. - */ - private assignMessageProperties(message: ErrorMessage): this { - isDefined(message.id) && this.setId(message.id), - isDefined(message.value) && this.setValue(message.value), - isDefined(message.template) && this.setTemplate(message.template), - this.setFix(message.fix).setProblem(message.problem); - return this; + template: string = ValidationError.template + ) { + super(ValidationError.messageTemplate`${message}${template}`); + this.#fix = message.fix; + this.#id = message.id; + this.#problem = message.problem; + this.#value = message.value; + this.#template = template; } } From c2455aa39fd5324442672c59f2d6d3923c96be19 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:33:09 +0100 Subject: [PATCH 074/179] refactor(ValidationError): simplify the object to not use any dependencies. --- src/lib/validation-error.class.ts | 235 ++++++++++++++++++++++++------ 1 file changed, 193 insertions(+), 42 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 3a406d5..8ae96f4 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,77 +1,228 @@ -// @angular-package/type. -import { - // Function. - guardObject, - isInstance, - isString, -} from '@angular-package/type'; -// Class. -// import { Text } from '@angular-package/text'; -import { Text } from '@angular-package/text'; - -// Interface. -import { ErrorMessage } from '../interface/error-message.interface'; /** * The `ValidationError` object to throw an error with a solution to the described problem. */ -export class ValidationError extends Error { - public static template = `Problem[id]: [problem] => Fix: [fix], got [value]`; - - public name = ValidationError.name; +export class ValidationError extends Error { + /** + * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}` words. + * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. It can be set directly or by the `setTemplate()`. + */ + public static template = `Problem{id}: {problem} => Fix: {fix}`; + //#region public instance accessors. + /** + * The get `accessor` obtains a possible solution to the described problem by returning the `#fix` property of a specified object. + * @returns The return value is the fix of a `string` type. + * @angularpackage + */ public get fix(): string { return this.#fix; } - public get id(): string | undefined { + /** + * The `get` accessor gets the `id` by returning the `#id` property of a specified object. + * @returns The return value is the id of the generic type variable `Id`. + * @angularpackage + */ + public get id(): Id { return this.#id; } + /** + * The `get` accessor gets the problem by returning the `#problem` property of a specified object. + * @returns The return value is the problem of a `string` type. + * @angularpackage + */ public get problem(): string { return this.#problem; } - public get value(): string | undefined { - return this.#value; - } - + /** + * The `get` accessor gets the template by returning the `#template` property of a specified object. + * @returns The return value is the template of a `string` type. + * @angularpackage + */ public get template(): string { return this.#template; } + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'ValidationError'` for an instance of + * `ValidationError`. It can be read by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'ValidationError` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'ValidationError'; + } + //#endregion public instance accessors. + + //#region instance public properties. + /** + * Error name of a `string` type that is being thrown. By default, it's `ValidationError`. + */ + public name = ValidationError.name; + //#endregion instance public properties. + + //#region private instance properties. + /** + * A privately stored possible solution to the described problem of a `string` type. + */ #fix: string; - #id?: string; + + /** + * A privately stored unique identification to the described problem of generic type variable `Id`. + */ + #id: Id = '' as Id; + + /** + * A privately stored validation problem of a `string` type. + */ #problem: string; - #value?: string; + + /** + * A string-type privately stored template of the error message that contains replaceable `{fix}`, `{problem}` and optional `{id}` words. + */ #template: string; + //#endregion private instance properties. + //#region public static methods. + /** + * Checks whether the value of any type is an instance of `ValidationError`. + * @param value The value of any type to check against the `ValidationError` instance. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `ValidationError`. + */ public static isValidationError(value: any): value is ValidationError { - return isInstance(value, ValidationError); + return typeof value === 'object' && value instanceof this; } - public static messageTemplate( - tsa: TemplateStringsArray, + /** + * The static "tag" method builds from the given `values` the validation error message of a string type on the template. + * @param templateStringsArray - + * @param values A rest parameter of expressions in order the `problem`, `fix`, `id` and `template`. + * @returns The return value is the validation error message of a `string` type created from the expressions given in the `values`. + * @angularpackage + */ + public static defineMessage( + templateStringsArray: TemplateStringsArray, ...values: any[] ): string { - let message: ErrorMessage, template: string; - [message, template] = values; - return new Text(template, ['fix', 'id', 'problem', 'value'], `[`, `]`) - .replace('fix', message?.fix) - .replace('id', message?.id || '') - .replace('problem', message.problem) - .replace('value', message.value) - .getText(); + let problem: string, fix: string, id: string, template: string; + [problem, fix, id, template] = values; + return template + .replace('{problem}', problem) + .replace('{fix}', fix) + .replace('{id}', id); } + //#endregion public static methods. + //#region constructor. + /** + * Creates a `ValidationError` instance that represents validation problem with solution and optional unique identification. + * @param problem Description of the validation problem of a string type. + * @param fix A solution to the given `problem` of a string type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of validation error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. + * @angularpackage + */ constructor( - message: ErrorMessage, - template: string = ValidationError.template + problem: string, + fix: string, + id?: Id, + template = ValidationError.template ) { - super(ValidationError.messageTemplate`${message}${template}`); - this.#fix = message.fix; - this.#id = message.id; - this.#problem = message.problem; - this.#value = message.value; + super(ValidationError.defineMessage`${problem}${fix}${id}${template}`); + this.#fix = fix; + this.#problem = problem; + this.#id = id as Id; + this.#template = template; + } + //#endregion constructor. + + //#region instance public methods. + /** + * Sets the fix a possible solution to the described problem. + * @param fix A possible solution to the described problem of a `string` type. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setFix(fix: string): this { + this.#fix = fix; + return this; + } + + /** + * Sets the unique identification to the described problem. + * @param id Unique identification of a string type of described problem. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setId(id: CustomId): this { + this.#id = id; + return this; + } + + /** + * Sets the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface. + * @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against + * the proper `object`. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setMessage( + problem: string, + fix: string, + id?: CustomId + ): this { + this.message = ValidationError.defineMessage`${problem}${fix}${id}${ + this.#template + }`; + return this; + } + + /** + * Sets description problem of a validation. + * @param problem Description of validation problem of a `string` type. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setProblem(problem: string): this { + this.#problem = problem; + return this; + } + + /** + * Sets the template of validation error message. + * @param template A message template of `string` type with replaceable `{problem}`, `{fix}` and optional `{id}` words. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setTemplate(template: string): this { this.#template = template; + return this; + } + + /** + * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem`, and optional `id` on the stored + * `template`. + * @angularpackage + */ + public throw(): void { + throw new ValidationError( + this.#problem, + this.#fix, + this.#id, + this.#template + ); + } + + /** + * Updates the message with a stored `fix`, `problem`, and optional `id` on the stored `template`. + * @angularpackage + */ + public updateMessage(): void { + this.message = ValidationError.defineMessage`${this.#problem}${this.#fix}${ + this.#id + }${this.#template}`; } + //#endregion instance public methods. } From 00229cda3f116766df5d1872519184332ee0402d Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:33:22 +0100 Subject: [PATCH 075/179] refactor(ErrorMessage): remove interface. --- src/interface/error-message.interface.ts | 34 ------------------------ 1 file changed, 34 deletions(-) delete mode 100644 src/interface/error-message.interface.ts diff --git a/src/interface/error-message.interface.ts b/src/interface/error-message.interface.ts deleted file mode 100644 index e592ab7..0000000 --- a/src/interface/error-message.interface.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * The shape of an `object` for the error message that contains a possible solution to the described problem. - * @param fix A possible solution to the described problem of a `string` type. - * @param id An optional identifier of the described problem. - * @param problem Description of validation problem of a `string` type. - * @param template An optional message template of a `string` type. - * @param value An optional value of any type affected by the validation error, which should be converted to `string`. - */ -export interface ErrorMessage { - /** - * A possible solution to the described problem of a `string` type. - */ - fix: string; - - /** - * An optional identifier of the described problem. - */ - id?: number; - - /** - * Description of validation problem of string type. - */ - problem: string; - - /** - * An optional message template of a string type. - */ - template?: string; - - /** - * An optional value of any type affected by the validation error, which should be converted to `string`. - */ - value?: any; -} From af28765495558520d2fe164a6e4adb154cb4f2f5 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:33:42 +0100 Subject: [PATCH 076/179] chore(test): update. --- src/test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test.ts b/src/test.ts index 52e5516..f26f742 100644 --- a/src/test.ts +++ b/src/test.ts @@ -18,7 +18,9 @@ declare const require: { // First, initialize the Angular testing environment. getTestBed().initTestEnvironment( BrowserDynamicTestingModule, - platformBrowserDynamicTesting() + platformBrowserDynamicTesting(), { + teardown: { destroyAfterEach: false } +} ); // Then we find all the tests. const context = require.context('./', true, /\.spec\.ts$/); From 91237773ba5eb50cd7d58c334cc3b9e5e6185f0e Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:34:05 +0100 Subject: [PATCH 077/179] chore(ng-package): remove `umdModuleIds`. --- ng-package.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ng-package.json b/ng-package.json index d83a37a..87acb8b 100644 --- a/ng-package.json +++ b/ng-package.json @@ -2,10 +2,6 @@ "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", "dest": "../../dist/error", "lib": { - "entryFile": "src/public-api.ts", - "umdModuleIds": { - "@angular-package/callback": "angular-package.callback", - "@angular-package/type": "angular-package.type" - } + "entryFile": "src/public-api.ts" } } \ No newline at end of file From 05bd4c014a358c7b9fe591be921cde372c4ab2d2 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:35:01 +0100 Subject: [PATCH 078/179] chore(package): remove dependencies. --- package.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index f25d8b6..e1a859d 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,12 @@ "description": "Manages an error.", "author": "Angular Package (https://angular-package.dev)", "homepage": "https://github.com/angular-package/error#readme", - "peerDependencies": { - "@angular-package/type": "^5.0.0" - }, "dependencies": { "tslib": "^2.3.0" }, "devDependencies": { - "@angular-package/testing": "^2.0.0" + "@angular-package/testing": "^2.0.0", + "@angular-package/type": "^5.0.0" }, "publishConfig": { "access": "public", From d3b884e95da6ee013ca1fb872f97d78f5ff9a042 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:39:53 +0100 Subject: [PATCH 079/179] docs(README.md): update. --- README.md | 1510 ++++------------------------------------------------- 1 file changed, 116 insertions(+), 1394 deletions(-) diff --git a/README.md b/README.md index a0b64f8..dce719f 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,45 @@ -# Packages - -Useful and simple to use packages based on the [angular.io][angulario]. - -| Package | Description | Status | -| :----------------------------------- | :----------------------------------------------------- | -----: | -| [callback][callback-github-readme] | Manages the callback [`function`][js-function]. | [![npm version][callback-npm-badge-png]][callback-npm-badge] | -| [change-detection][cd-github-readme] | Improves application performance. | [![npm version][cd-npm-badge-png]][cd-npm-badge] | -| [component-loader][cl-github-readme] | Handles dynamic loading components. | [![npm version][cl-npm-badge-png]][cl-npm-badge] | -| [core][core-github-readme] | Core features. | [![npm version][core-npm-badge-png]][core-npm-badge] | -| [error][error-github-readme] | Manages an [`Error`][js-error]. | [![npm version][error-npm-badge-png]][error-npm-badge] | -| [prism][prism-github-readme] | [`Prism`][prism-js] highlighter module. | [![npm version][prism-npm-badge-png]][prism-npm-badge] | -| [property][property-github-readme] | Handles object properties. | [![npm version][property-npm-badge-png]][property-npm-badge] | -| [reactive][reactive-github-readme] | Automatize the process of creating some rxjs features. | [![npm version][reactive-npm-badge-png]][reactive-npm-badge] | -| [testing][testing-github-readme] | Support for testing other packages. | [![npm version][testing-npm-badge-png]][testing-npm-badge] | -| [type][type-github-readme] | Common types, type guards, and type checkers. | [![npm version][type-npm-badge-png]][type-npm-badge] | -| [ui][ui-github-readme] | User interface. | *In Progress* | - -> Click on the package name to visit its [GitHub](https://github.com/) page. +# angular-package + + + +The angular-package supports the development process of [angular][angulario]-based applications in varied ways through the thoughtful, reusable, easy-to-use small pieces of code called packages. + +[**docs.angular-package.dev**](https://docs.angular-package.dev) + +
+ +## Packages + +| Package | Description | Status | +| :------------------------------------------- | :---------------------------------------------------------------- | -----: | +| [callback][callback-github-readme] | Manages the callback [`function`][js-function]. | [![npm version][callback-npm-badge-png]][callback-npm-badge] | +| [change-detection][cd-github-readme] | Improves application performance. | [![npm version][cd-npm-badge-png]][cd-npm-badge] | +| [component-loader][cl-github-readme] | Handles dynamic loading components. | [![npm version][cl-npm-badge-png]][cl-npm-badge] | +| [core][core-github-readme] | Core features. | [![npm version][core-npm-badge-png]][core-npm-badge] | +| **[error][error-github-readme]** | **Manages an [`Error`][js-error].** | [![npm version][error-npm-badge-png]][error-npm-badge] | +| [name][name-github-readme] | The name with prefix and suffix. | [![npm version][name-npm-badge-png]][name-npm-badge] | +| [preferences][preferences-github-readme] | Preferences, settings, options, configuration and setup in steps. | [![npm version][preferences-npm-badge-png]][preferences-npm-badge] | +| [prism][prism-github-readme] | [`Prism`][prism-js] highlighter module. | [![npm version][prism-npm-badge-png]][prism-npm-badge] | +| [property][property-github-readme] | Handles object properties. | [![npm version][property-npm-badge-png]][property-npm-badge] | +| [range][range-github-readme] | The range between a minimum and maximum. | [![npm version][range-npm-badge-png]][range-npm-badge] | +| [reactive][reactive-github-readme] | Automatize the process of creating some rxjs features. | [![npm version][reactive-npm-badge-png]][reactive-npm-badge] | +| [storage][storage-github-readme] | The storage of data under allowed names. | [![npm version][storage-npm-badge-png]][storage-npm-badge] | +| [tag][tag-github-readme] | Any tag with optional attributes. | [![npm version][tag-npm-badge-png]][tag-npm-badge] | +| [testing][testing-github-readme] | Support for testing other packages. | [![npm version][testing-npm-badge-png]][testing-npm-badge] | +| [text][text-github-readme] | Text on the template with replaceable tags. | [![npm version][text-npm-badge-png]][text-npm-badge] | +| [type][type-github-readme] | Common types, type guards, and type checkers. | [![npm version][type-npm-badge-png]][type-npm-badge] | +| [ui][ui-github-readme] | User interface. | *In Progress* | +| [wrapper][wrapper-github-readme] | Wrap the text with the opening and closing chars. | [![npm version][wrapper-npm-badge-png]][wrapper-npm-badge] | + +Click on the package name to visit its [GitHub](https://github.com/) page. ## angular-package/error Manages an [`Error`][js-error]. [![Gitter][gitter-badge]][gitter-chat] +[![Discord][discord-badge]][discord-channel] +[![Twitter][twitter-badge]][twitter-follow] [![npm version][error-npm-badge-svg]][error-npm-badge] @@ -42,10 +59,6 @@ Manages an [`Error`][js-error]. * [Skeleton](#skeleton) * [Installation](#installation) * [Api](#api) -* [`ValidationError`](#validationerror) -* [Interface](#interface) -* [Type](#type) -* [Experimental](#experimental) * [Changelog](#changelog) * [Git](#git) * [Commit](#commit) @@ -145,1372 +158,6 @@ import {
-## `ValidationError` - -Manages an [`Error`][js-error] of validation. - -**Static properties:** - -| ValidationError. | Description | -| :--------------------------------------------- | :---------- | -| [`template: string`](#validationerrortemplate) | A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** **`[fix]`** and **`[problem]`** tags and **optional** **`[id]`** and **`[value]`**. | - -**Instance properties:** - -| ValidationError.prototype. | Description | -| :-------------------------------------------- | :---------- | -| [`fix: string`][error-property-fix] | A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. | -| [`id: number`][error-property-id] | The **identifier** of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. | -| [`message: string`][error-property-message] | A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). | -| [`name: string`][error-property-name] | [`Error`][js-error] name of [`string`][js-string] type that is being thrown. | -| [`problem: string`][error-property-problem] | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. | -| [`template: string`][error-property-template] | A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`** tags. | -| [`value: string`][error-property-value] | The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. | - -[error-property-fix]: #validationerrorprototypefix -[error-property-id]: #validationerrorprototypeid -[error-property-message]: #validationerrorprototypemessage -[error-property-name]: #validationerrorprototypename -[error-property-problem]: #validationerrorprototypeproblem -[error-property-template]: #validationerrorprototypetemplate -[error-property-value]: #validationerrorprototypevalue - -**Static methods:** - -| ValidationError. | Description | -| :--------------------------------------------------- | :---------- | -| [`defineMessage()`](#validationerrordefinemessage) | Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | -| [`setTemplate()`](#validationerrorsettemplate) | Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the **default** value for [`template`][error-property-template] of an instance. | -| [`setValueParser()`](#validationerrorsetvalueparser) | Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation performed by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the default parser for the instance. | - -**Constructor:** - -| Constructor | Description | -| :-------------------------------------------------- | :---------- | -| [`ValidationError()`](#validationerror-constructor) | Creates a new instance with the [`message`][error-property-message]. If the provided `message` is an [`object`][js-object], then its properties are assigned to the instance. | - -**Instance methods:** - -| ValidationError.prototype. | Description | -| :------------------------------------------------ | :---------- | -| [`setFix()`][error-method-setfix] | Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. | -| [`setId()`][error-method-setid] | Sets the [`id`][error-property-id] an **identifier** of the described [`problem`][error-property-problem]. | -| [`setMessage()`][error-method-setmessage] | Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. | -| [`setProblem()`][error-method-setproblem] | Sets the description of the validation [`problem`][error-property-problem]. | -| [`setTemplate()`][error-method-settemplate] | Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. | -| [`setValue()`][error-method-setvalue] | Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). | -| [`setValueParser()`][error-method-setvalueparser] | Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] of [`any`][ts-any] type to the [`string`][js-string] during [`message`][error-property-message] creation. | -| [`throw()`][error-method-throw] | Throws an error of new instance [`ValidationError`](#validationerror) with the message built from the stored **required** [`fix`][error-property-fix], [`problem`][error-property-problem] and **optional** [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. | -| [`updateMessage()`][error-method-updatemessage] | Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. | - -[error-method-setfix]: #validationerrorprototypesetfix -[error-method-setid]: #validationerrorprototypesetid -[error-method-setmessage]: #validationerrorprototypesetmessage -[error-method-setproblem]: #validationerrorprototypesetproblem -[error-method-settemplate]: #validationerrorprototypesettemplate -[error-method-setvalue]: #validationerrorprototypesetvalue -[error-method-setvalueparser]: #validationerrorprototypesetvalueparser -[error-method-throw]: #validationerrorprototypethrow -[error-method-updatemessage]: #validationerrorprototypeupdatemessage - -
- -### `ValidationError` static properties - -#### `ValidationError.template` - -[![update]][error-github-changelog] - -A template(guarded by a [`string`][js-string] type) of static [`ValidationError`](#validationerror) and the default value of [`template`][error-property-template] of an instance for the error message with the replaceable **required** **`[fix]`** and **`[problem]`** tags and **optional** **`[id]`** and **`[value]`**. The value is being checked against the existence of **`[fix]`** and **`[problem]`** tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the static [`ValidationError.setTemplate()`](#validationerrorsettemplate) method. By default, it's set to `Problem: [problem] => Fix: [fix]`. - -```typescript -static get template(): string { - return this.#defaultTemplate; -} -static set template(value: string) { - this.#guardTemplate(value) && (this.#defaultTemplate = value); -} -``` - -**Tags:** - -**`[fix]`**: **Required**, replaced by the [`fix`][error-property-fix] property. - -**`[id]`**: Optional, replaced by the [`id`][error-property-id] property. - -**`[problem]`**: **Required**, replaced by the [`problem`][error-property-problem] property. - -**`[value]`**: Optional, replaced by the [`value`][error-property-value] property. - -
- -### `ValidationError` instance public properties - -#### `ValidationError.prototype.fix` - -[![update]][error-github-changelog] - -A possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem] of validation that is guarded by [`string`][js-string] and it's a replacement for the **required** tag **`[fix]`** of the [`template`][error-property-template]. It can be set directly or by the [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. By default, it's an empty [`string`][js-string]. - -```typescript -public get fix(): string { - return this.#fix; -} -public set fix(value: string) { - guardString(value) && (this.#fix = value); -} -``` - -
- -#### `ValidationError.prototype.id` - -[![new]][error-github-changelog] - -The **identifier** of the described [`problem`][error-property-problem], guarded by [`number`][js-number] type and it's a replacement for an optional tag **`[id]`** of the [`template`][error-property-template]. It can be set directly or by the [`setId()`][error-method-setid], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method of an instance. By default, it's `0`. - -```typescript -public get id(): number { - return this.#id; -} -public set id(value: number) { - guardNumber(value) && (this.#id = value); -} -``` - -
- -#### `ValidationError.prototype.message` - -[![update]][error-github-changelog] - -A validation [`error`][js-error] message guarded by [`string`][js-string] type that can be built from the **required** [`problem`][error-property-problem] and [`fix`][error-property-fix] with **optional** [`id`][error-property-id] and [`value`][error-property-value] on the [`template`][error-property-template] of an instance of [`ValidationError`](#validationerror). It can be set directly or by the [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. - -```typescript -public get message(): string { - return super.message; -} -public set message(value: string) { - guardString(value) && (super.message = value); -} -``` - -
- -#### `ValidationError.prototype.name` - -[`Error`][js-error] name of [`string`][js-string] type that is being thrown. By default, it's ['ValidationError'](#validationerror). - -```typescript -public name = ValidationError.name; -``` - -
- -#### `ValidationError.prototype.problem` - -[![update]][error-github-changelog] - -Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and it's a replacement for the **required** tag **`[problem]`** of the [`template`][error-property-template]. It can be set directly or by the [`setProblem()`][error-method-setproblem], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] method. By default, it's an empty [`string`][js-string]. - -```typescript -public get problem(): string { - return this.#problem; -} -public set problem(value: string) { - guardString(value) && (this.#problem = value); -} -``` - -
- -#### `ValidationError.prototype.template` - -[![new]][error-github-changelog] - -A template of the [`error`][js-error] message guarded by [`string`][js-string] type with the replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`** tags. The value is being checked against the existence of **required** **`[problem]`** and **`[fix]`** tags by the static private `ValidationError.#guardTemplate()` method. It can be set directly or by the [`setTemplate()`][error-method-settemplate], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods, and its **initial** value is from the static [`ValidationError.template`](#validationerrortemplate). By default, it's set to `Problem: [problem] => Fix: [fix]`. - -```typescript -public get template(): string { - return this.#template; -} -public set template(value: string) { - ValidationError.#guardTemplate(value) && (this.#template = value); -} -``` - -**Tags:** - -Equivalent to the static [`ValidationError.template`](#validationerrortemplate). - -
- -#### `ValidationError.prototype.value` - -[![new]][error-github-changelog] - -The value of [`any`][ts-any] type affected by the validation error, which must be converted to a [`string`][js-string] to build a [`message`][error-property-message] and it's a replacement for an optional tag **`[value]`** of the [`template`][error-property-template]. It can be set directly or by the [`setValue()`][error-method-setvalue], [`setMessage()`][error-method-setmessage] and [`throw()`][error-method-throw] methods. - -**Conversion:** -The conversion is performed only during [`message`][error-property-message] creation and can be done in **two** ways, **automatic** or **manual**. - -* The **manual**, by providing parser function as parameter (`parser?: ValueParser`) to the [`setMessage()`][error-method-setmessage] method of an instance. -* The **automatic** by the previously defined function of the [`ValueParser`][package-type-valueparser] type, set by the [`setValueParser()`][error-method-setvalueparser] method of an instance to automatize [`setMessage()`][error-method-setmessage] method. - -```typescript -public get value(): any { - return this.#value; -} -public set value(value: any) { - this.#value = value; -} -``` - -
- -### `ValidationError` static methods - -#### `ValidationError.defineMessage()` - -[![update]][error-github-changelog] - -Defines the validation [`error`][js-error] message of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. The message is built on the provided `template` or the template from the static property [`ValidationError.template`](#validationerrortemplate). - -> The **automatic** convert of the `value` of an [`ErrorMessage`](#errormessage) is performed by the defined function of the [`ValueParser`][package-type-valueparser] type, set by the static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) method. - -```typescript -// Syntax. -public static defineMessage( - message: ErrorMessage, - callback: ResultCallback = (result) => result, - parser: ValueParser = this.#defaultValueParser -): string { - return callback( - this.#guardMessage(message) && guardFunction(parser), - message - ) - ? this.#buildMessage(message, parser) - : ''; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------------- | :---------- | -| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) by the static private `ValidationError.#guarMessage()` method. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `message` is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, and the **optional** [`template`][error-property-template] property has **`[problem]`** and **`[fix]`** tags and parser is [`function`][js-function] type. | -| `parser: ValueParser` | An optional [`function`][js-function] to **convert** property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during [`message`][error-property-message] creation. It can be set by static [`ValidationError.setValueParser()`](#validationerrorsetvalueparser) as the default value parser for static and instance [`ValidationError`](#validationerror). | - -**Returns:** - -The **return value** is a message of [`string`][js-string] type created from the provided `message` of the [`ErrorMessage`](#errormessage) interface, or it's an empty [`string`][js-string] if the provided message [`object`][js-object] isn't proper. - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: Symbol(123) - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError.defineMessage({ fix, id, problem, value, template }); - -// Define with only required tags. -/* - Returns - ------- - Problem: The value must be a string type. => Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError.defineMessage({ fix, problem }); - -// Define the template in static `ValidationError`. -ValidationError.template = template; - -// Define the message again without `template`. -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: Symbol(123) - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError.defineMessage({ fix, id, problem, value }); - -// Define with only required tags. -/* - Returns - ------- - Problem(VE): The value must be a string type. - Got: undefined - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError.defineMessage({ fix, problem }); - -// Define with the callback and value parser. -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: 12345 - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError.defineMessage( - { fix, id, problem, value, template }, - (result, value, payload) => { - result // true - value // {fix ... } the given message. - return result; - }, - (value) => String(12345) // Should return converted value instead of 12345. -); -``` - -
- -#### `ValidationError.setTemplate()` - -[![new]][error-github-changelog] - -Sets the [`template`](#validationerrortemplate) of static [`ValidationError`](#validationerror) and as the **default** value for [`template`][error-property-template] of an instance. - -```typescript -// Syntax. -public static setTemplate( - template: string, - callback?: ResultCallback -): typeof ValidationError { - ValidationError.#guardTemplate(template, callback) && - (this.#defaultTemplate = template); - return ValidationError; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------- | :---------- | -| `template: string` | A [template](#validationerrortemplate) for error [`message`][error-property-message] guarded by [`string`][js-string] type with replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`**. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains required **`[fix]`** and **`[problem]`** tags. | - -**Returns:** - -The **return value** is static [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: Symbol(123) - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError - .setTemplate(template, (result) => result) - .defineMessage({ - fix, - id, - problem, - value, - }); -``` - -
- -#### `ValidationError.setValueParser()` - -[![new]][error-github-changelog] - -Sets the [`function`][js-function] to convert [`any`][ts-any] value to [`string`][js-string] during message creation by the static [`ValidationError.defineMessage()`](#validationerrorsetvalueparser) method and as the initial parser for the instance. - -```typescript -// Syntax. -public static setValueParser( - parser: ValueParser, - callback?: ResultCallback -): typeof ValidationError { - guardFunction(parser, callback) && (this.#defaultValueParser = parser); - return ValidationError; -} -``` - -**Parameters:** - -| Name: type | Description | -| :--------------------------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`][package-type-valueparser] type to convert the value of [`any`][ts-any] type to [`string`][js-string]. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the result of the check whether the provided `parser` is [`function`][js-function] type. | - -**Returns:** - -The **return value** is static [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: 12345 - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -ValidationError - .setValueParser((value: any) => String(12345), (result) => result) - .defineMessage({ - fix, - id, - problem, - value, - template - }); -``` - -
- -### `ValidationError` constructor - -#### `ValidationError()` - -[![update]][error-github-changelog] - -Creates a new instance with an optional [`message`][error-property-message] of [`ErrorMessage`](#errormessage) interface. - -```typescript -// Syntax. -constructor(message?: ErrorMessage, parser?: ValueParser) { - super(); - - // Sets the parser. - isDefined(parser) && this.setValueParser(parser); - - // Initializes the message and assigns its properties to a new instance. - isDefined(message) && this.setMessage(message); -} -``` - -**Parameters:** - -| Name: type | Description | -| :----------------------- | :---------- | -| `message?: ErrorMessage` | An optional `message` of an [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a [`message`][error-property-message] of [`string`][js-string] and throw with an [`Error`][js-error]. Its properties are assigned to the instance. | -| `parser: ValueParser` | An optional [`function`][js-function] to **convert** the property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during message creation and set as the **default** value parser for the instance. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | - -**Returns:** - -The **return value** is an instance of [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -/* - Returns - ------- - ValidationError: Problem(VE427): The value must be a string type. - Got: Symbol(123) - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -const validationError = new ValidationError({ fix, id, problem, template, value }); -``` - -```typescript -// Example usage with callback. -import { ValidationError } from '@angular-package/error'; -import { Callback, ErrorMessage } from '@angular-package/callback'; -import { ResultCallback } from '@angular-package/type'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; - -class CustomError { - #callback = new Callback('setFix', 'setMessage', 'setProblem', 'setTemplate'); - #validationError = new ValidationError(); - - constructor( - message?: ErrorMessage, - callback?: ( - callback: Callback<'setFix' | 'setMessage' | 'setProblem' | 'setTemplate'> - ) => void - ) { - callback && callback(this.#callback); - message && this.#validationError.setMessage(message); - } - - public setMessage( - message: ErrorMessage, - callback: ResultCallback = this.#callback.getResultCallback( - 'setMessage' - ) - ): this { - this.#validationError.setMessage(message, callback); - return this; - } - - public throw(): void { - this.#validationError.throw(); - } -} - -const customError = new CustomError(undefined, (callback) => - callback - .setResultCallback('setFix', (result, value) => - console.log(`setFix`, result, value) - ) - - // Console: 'setFix true There is no solution to the described problem.' - .setResultCallback('setMessage', (result, value) => - console.log(`setMessage`, result, value) - ) - - // Console: 'setProblem true The problem has no solution.' - .setResultCallback('setProblem', (result, value) => - console.log(`setProblem`, result, value) - ) - - // Console: 'setTemplate true PROBLEM: [problem] FIX: [fix]' - .setResultCallback('setTemplate', (result, value) => - console.log(`setTemplate`, result, value) - ) -); - -/** - * ! Console returns - * setMessage true - * { - * fix: 'Provide string type value. Read more: https://duckduckgo.com/', - * problem: 'The value must be a string type.', - * template: 'Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]' - * } - */ -customError.setMessage({ fix, problem, template }); -``` - -
- -### `ValidationError` instance public methods - -#### `ValidationError.prototype.setFix()` - -[![update]][error-github-changelog] - -Sets the [`fix`][error-property-fix] a possible solution to the described [`problem`][error-property-problem]. - -```typescript -// Syntax. -public setFix(fix: string, callback?: ResultCallback): this { - guardString(fix, callback) && (this.#fix = fix); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------- | :---------- | -| `fix: string` | A possible [solution][error-property-fix] to the described [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the **`[fix]`** tag of [`template`][error-property-template]. | -| `callback?: ResultCallback` | 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]. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - -// Initialize an instance. -const validationError = new ValidationError(); - -// Returns 'Provide string type value. Read more: https://duckduckgo.com/' -validationError.setFix(fix).fix; -``` - -```typescript -// Example usage with a callback. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - -// Initialize an instance. -const validationError = new ValidationError(); - -// Set the fix and handle the check of it with a callback. -validationError.setFix(fix, (result, value) => { - // Returns `true`. - result; - // Returns 'Provide string type value. Read more: https://duckduckgo.com/' - value; - return result; -}); -``` - -
- -#### `ValidationError.prototype.setId()` - -[![new]][error-github-changelog] - -Sets the [`id`][error-property-id] an **identifier** of the described [`problem`][error-property-problem]. - -```typescript -// Syntax. -public setId(id: number, callback?: ResultCallback): this { - guardNumber(id, callback) && (this.#id = id); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------- | :---------- | -| `id: number` | The identifier, guarded by [`number`][js-number] type and a replacement to the **`[id]`** tag of [`template`][error-property-template]. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`id`][error-property-id] is [`number`][js-number]. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage with a callback. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const id = 427; - -// Initialize an instance. -const validationError = new ValidationError(); - -// Set the id and handle the check of it with a callback. -validationError.setId(id, (result, value) => { - // Returns `true`. - result; - // Returns 427 - value; - return result; -}).id; // Returns 427 -``` - -
- -#### `ValidationError.prototype.setMessage()` - -[![update]][error-github-changelog] - -Sets the validation [`error`][js-error] [`message`][error-property-message] of [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface. - -```typescript -// Syntax. -public setMessage( - message: ErrorMessage, - callback?: ResultCallback, - parser: ValueParser = this.#valueParser -): this { - super.message = ValidationError.defineMessage( - { ...message, ...{ template: message.template || this.#template } }, - (result, value, payload) => ( - isDefined(callback) && callback(result, value, payload), - isTrue(result) && this.assignMessageProperties(message), - result - ), - parser - ); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :------------------------------------------ | :---------- | -| `message: ErrorMessage` | An [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) and if it's a proper object then its properties are being assigned to the instance. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided [`message`][error-property-message] is the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) which means it contains **required** [`problem`][error-property-problem], [`fix`][error-property-fix] properties, the **optional** [`template`][error-property-template] property has **`[problem]`** and **`[fix]`** tags and `parser` is a function type. | -| `parser: ValueParser` | An optional [`function`][js-function] to **convert** property [`value`][error-property-value] of the provided `message` of [`ErrorMessage`](#errormessage) to a [`string`][js-string] during message creation. By default, it uses the parser function of private property `#valueParser` of an instance that is set initially to the static `ValidationError.#defaultValueParser`. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage with a callback. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -// Initialize an instance. -const validationError = new ValidationError(); - -// Set the message and handle the check of it with a callback. -validationError.setMessage({ fix, id, problem, template, value }, (result, value) => { - // Returns `true`. - result; - /* - { - fix: 'Provide string type value. Read more: https://duckduckgo.com/', id: 427, - problem: 'The value must be a string type.', - template: 'Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]', value: Symbol(123)} - */ - value; - return result; -}, (value) => String(12345)); -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: 12345 - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -validationError.message; -``` - -
- -#### `ValidationError.prototype.setProblem()` - -[![update]][error-github-changelog] - -Sets the description of the validation [`problem`][error-property-problem]. - -```typescript -public setProblem(problem: string, callback?: ResultCallback): this { - guardString(problem, callback) && (this.#problem = problem); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------- | :---------- | -| `problem: string` | Description of the validation [`problem`][error-property-problem] guarded by [`string`][js-string] type and a replacement to the **`[problem]`** tag of [`template`][error-property-template]. | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `problem` is a [`string`][js-string]. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage with a callback. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const problem = 'The value must be a string type.'; - -// Initialize an instance. -const validationError = new ValidationError(); - -// Returns 'The value must be a string type.' -validationError.setProblem(problem, (result, value) => { - // Returns `true`. - result; - // Returns 'The value must be a string type.' - value; - return result; -}).problem; -``` - -
- -#### `ValidationError.prototype.setTemplate()` - -[![update]][error-github-changelog] - -Sets the [`template`][error-property-template] of validation error [`message`][error-property-message]. - -```typescript -public setTemplate( - template: string, - callback?: ResultCallback -): this { - ValidationError.#guardTemplate(template, callback) && - (this.#template = template); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------- | :---------- | -| `template: string` | A message [`template`][error-property-template] guarded by [`string`][js-string] type with replaceable **required** **`[problem]`** and **`[fix]`** tags and **optional** **`[id]`** and **`[value]`** tags. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `template` is a [`string`][js-string] that contains **required** **`[fix]`** and **`[problem]`** tags. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage with a callback. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; - -// Initialize an instance. -const validationError = new ValidationError(); - -/* - Returns - ------- - Problem(VE[id]): [problem] - Got: [value] - Fix: [fix] -*/ -validationError.setTemplate(template, (result, value) => { - // Returns `true`. - result; - // Returns 'Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]' - value; - return result; -}).template; -``` - -
- -#### `ValidationError.prototype.setValue()` - -[![new]][error-github-changelog] - -Sets the [`value`][error-property-value] affected by the validation error (must be converted to [`string`][js-string]). - -```typescript -public setValue(value: any): this { - this.#value = value; - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :----------- | :---------- | -| `value: any` | The value of [`any`][ts-any] type as a replacement to the **`[value]`** tag of [`template`][error-property-template] that relates to the given [`problem`][error-property-problem]. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const value = Symbol(123); - -// Initialize an instance. -const validationError = new ValidationError(); - -// Symbol(123) -validationError.setValue(value).value; -``` - -
- -#### `ValidationError.prototype.setValueParser()` - -[![new]][error-github-changelog] - -Sets the [`function`][js-function] to automatically **convert** the [`value`][error-property-value] of [`any`][ts-any] type to the [`string`][js-string] during [`message`][error-property-message] creation. The defined [`function`][js-function] is being used implicitly by the [`updateMessage()`][error-method-updatemessage] and [`throw()`][error-method-throw] methods and by the [`setMessage()`][error-method-setmessage] method as the default value of parameter `parser`. - -```typescript -public setValueParser( - parser: ValueParser, - callback?: ResultCallback -): this { - guardFunction(parser, callback) && (this.#valueParser = parser); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :--------------------------------------- | :---------- | -| `parser: ValueParser` | The [`function`][js-function] of the [`ValueParser`][package-type-valueparser] type, to **convert** the [`value`][error-property-value] of [`any`][ts-any] type to [`string`][js-string]. | -| `callback?: ResultCallback` | An optional callback function of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the provided `parser` is [`function`][js-function]. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -// Initialize an instance. -const validationError = new ValidationError(); - -// Symbol(123) -validationError.setValueParser(value => String(12345)); - -// Set the message with predefined parser, the value instead of Symbol(123) is 12345 -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: 12345 - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -validationError.setMessage({ fix, id, problem, value, template }).message; -``` - -
- -#### `ValidationError.prototype.throw()` - -[![update]][error-github-changelog] - -Throws an error of new instance [`ValidationError`](#validationerror) with the message built from the stored **required** [`fix`][error-property-fix], [`problem`][error-property-problem] and **optional** [`id`][error-property-id], [`template`][error-property-template] and [`value`][error-property-value] properties or optionally from the provided `message`. - -```typescript -public throw(message?: ErrorMessage): void { - isDefined(message) ? this.setMessage(message) : this.updateMessage(); - throw new ValidationError( - { - fix: this.#fix, - id: this.#id, - problem: this.#problem, - template: this.#template, - value: this.#value, - }, - this.#valueParser - ); -} -``` - -**Parameters:** - -| Name: type | Description | -| :----------------------- | :---------- | -| `message?: ErrorMessage` | An optional [`object`][js-object] of an [`ErrorMessage`](#errormessage) interface to build the message of [`string`][js-string] type. The value is checked against the proper [`object`][js-object] of [`ErrorMessage`](#errormessage) and if it's a proper [`object`][js-object] then its properties are being assigned to the instance. | - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -// Initialize an instance. -const validationError = new ValidationError({ fix, id, problem, value, template }); - -// throw -try { - validationError.throw(); -} catch (e) { - // Provide string type value. Read more: https://duckduckgo.com/ - console.log(e.fix); - // 427 - console.log(e.id); - // The value must be a string type. - console.log(e.problem); - // Symbol(123) - console.log(e.value); - /* - Problem(VE[id]): [problem] - Got: [value] - Fix: [fix] - */ - console.log(e.template); -} -``` - -
- -#### `ValidationError.prototype.updateMessage()` - -[![update]][error-github-changelog] - -Updates the [`message`][error-property-message] with a stored required [`fix`][error-property-fix], [`problem`][error-property-problem], and optional [`id`][error-property-id], [`template`][error-property-template], [`value`][error-property-value] properties. - -```typescript -public updateMessage(callback?: ResultCallback): this { - super.message = ValidationError.defineMessage( - { - fix: this.#fix, - id: this.#id, - problem: this.#problem, - template: this.#template, - value: this.#value, - }, - callback, - this.#valueParser - ); - return this; -} -``` - -**Parameters:** - -| Name: type | Description | -| :---------------------------------------- | :---------- | -| `callback?: ResultCallback` | An optional callback [`function`][js-function] of [`ResultCallback`][package-callback-resultcallback] type to handle the check whether the stored properties used to build a message of [`string`][js-string] type are proper. | - -**Returns:** - -The **return value** is an instance of a [`ValidationError`](#validationerror). - -**Usage:** - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Prepare the values. -const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; -const id = 427; -const problem = 'The value must be a string type.'; -const template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; -const value = Symbol(123); - -// Initialize an instance. -const validationError = new ValidationError(); - -// Sets defined above fix, problem, and template. -validationError - .setFix(fix) - .setId(id) - .setProblem(problem) - .setTemplate(template) - .setValue(value); - -// Returns empty string. -validationError.message; - -// Update the message with actual settings. -validationError.updateMessage(); - -/* - Returns - ------- - Problem(VE427): The value must be a string type. - Got: Symbol(123) - Fix: Provide string type value. Read more: https://duckduckgo.com/ -*/ -validationError.message; - -// Throw. -validationError.throw(); - -// or throw -throw validationError; -``` - -
- -### More example usage of `ValidationError` - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Declare shape of the person. -interface Person { - firstName: string; - lastName: string; -} - -// Initialize an instance. -const validationErrorOfPerson = new ValidationError(); - -// Define a fix. -const fix = 'Please, provide only alphabetical characters.'; - -// Create an object. -const personError = { - firstName: validationErrorOfPerson.setMessage({ - problem: 'Provided the first name cannot include special characters.', - fix - }), - lastName: validationErrorOfPerson.setMessage({ - problem: 'Provided the last name cannot include special characters.', - fix - }) -}; - - -const addPerson = (person: Person) => { - if (person.firstName.includes('#')) { - personError.firstName.throw(); - } - if (person.firstName.includes('#')) { - personError.lastName.throw(); - } -}; - -addPerson({ - firstName: '#', - lastName: '#' -}); - -``` - -```typescript -// Example usage. -import { ValidationError } from '@angular-package/error'; - -// Initialize an instance. -const validationError = new ValidationError(); - -try { - validationError.throw(); -} catch (e) { - // Problem: => Fix: - console.log(e.message); - // Empty string. - console.log(e.problem); - // Empty string. - console.log(e.fix); -} - -try { - validationError - .setProblem('my problem') - .setFix('my fix') - .throw(); -} catch (e) { - // Problem: my problem => Fix: my fix - console.log(e.message); - // my problem - console.log(e.problem); - // my fix - console.log(e.fix); -} - - -try { - validationError - .setMessage({ problem: 'my problem', fix: 'my fix'}) - .throw(); -} catch (e) { - // Problem: my problem => Fix: my fix - console.log(e.message); - // my problem - console.log(e.problem); - // my fix - console.log(e.fix); -} -``` - -
- -## Interface - -#### `ErrorMessage` - -The shape of an [`object`][js-object] for an [`error`][js-error] message that contains a possible [`solution`][error-property-fix] to the described [`problem`][error-property-problem]. - -```typescript -export interface ErrorMessage { - fix: string; - id?: number; - problem: string; - template?: string; - value?: any; -} -``` - -**Properties:** - -**`fix: string`** -A possible solution to the described problem of a [`string`][js-string] type. - -**`id: number`** -An optional **identifier** of the described problem. - -**`problem: string`** -Description of validation problem of a [`string`][js-string] type. - -**`template?: string`** -An optional error message template of a [`string`][js-string] type. - -**`value?: string`** -[![new]][error-github-changelog] -An optional value affected by the validation error, which must be converted to [`string`][js-string]. - -
- -## Experimental - -![experimental] [![update]][error-github-changelog] - -### Message builder - -#### `MessageBuilder` - -Message builder to build [`class`][js-classes], [`function`][js-function] and `method` of a [`string`][js-string] type. - -```typescript -// Example usage of building a function. -import { MessageBuilder } from '@angular-package/error'; - -/** - * Initialize `MessageBuilder`. - */ -const messageFunctionBuilder = new MessageBuilder('function'); - -messageFunctionBuilder - .replaceFunctionName('guardString') - .replaceParam('value', 'string') - .replaceReturn('boolean'); - -// Console returns `guardString(value: string): boolean` -console.log(messageFunctionBuilder.get); -``` - -```typescript -// Example usage of building a method. -import { MessageBuilder } from '@angular-package/error'; - -/** - * Initialize `MessageBuilder`. - */ -const messageMethodBuilder = new MessageBuilder('method'); - - // Build the method of any class. -messageMethodBuilder - .replaceMethodName('setPerson') - .replaceParam('value', 'string') - .replaceReturn('this'); - -// Console returns `setPerson(value: string): this` -console.log(messageMethodBuilder.get); -``` - -```typescript -// Example usage of building a class. -import { MessageBuilder } from '@angular-package/error'; - -/** - * Initialize `MessageBuilder`. - */ -const messageClassBuilder = new MessageBuilder('class'); - - // Build the method of a specified class. -messageClassBuilder - .replaceClassName('Person.prototype.') - .replaceMethodName('setPerson') - .replaceParam('value?', 'object') - .replaceReturn('object'); - -// Console returns `Person.prototype.setPerson(value?: object): object` -console.log(messageClassBuilder.get); -``` - -
- -#### `MessageFunctionBuilder` - -Message builder to build a [`function`][js-function] of a [`string`][js-string] type. - -```typescript -// Example usage of building a function. -import { MessageFunctionBuilder } from '@angular-package/error'; - -/** - * Initialize `MessageFunctionBuilder`. - */ -const messageFunctionBuilder = new MessageFunctionBuilder(); - -messageFunctionBuilder - .setName('guardString') - .setParam('value', 'string') - .setReturn('boolean'); - -// Console returns [function]([param.name][param.type])[return] -console.log(messageFunctionBuilder.get); - -// Build. -messageFunctionBuilder.build(); - -// Console returns guardString(value: string): boolean -console.log(messageFunctionBuilder.get); -``` - -
- ## Changelog The **changelog** of this package is based on [*keep a changelog*](https://keepachangelog.com/en/1.0.0/). To read it, click on the [CHANGELOG.md](https://github.com/angular-package/error/blob/main/CHANGELOG.md) link. @@ -1567,10 +214,18 @@ MIT © angular-package ([license][error-license]) [new]: https://img.shields.io/badge/-new-green [update]: https://img.shields.io/badge/-update-red + +[discord-badge]: https://img.shields.io/discord/925168966098386944 +[discord-channel]: https://discord.com/channels/925168966098386944/925168966098386948 + [gitter-badge]: https://badges.gitter.im/angularpackage/Lobby.svg [gitter-chat]: https://gitter.im/angularpackage/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge + +[twitter-badge]: https://img.shields.io/twitter/url?style=social&label=Follow%20%40angularpackage&url=https%3A%2F%2Ftwitter.com%2Fangularpackage +[twitter-follow]: https://twitter.com/angularpackage + [git-semver]: http://semver.org/ @@ -1603,6 +258,7 @@ MIT © angular-package ([license][error-license]) [callback-github-readme]: https://github.com/angular-package/callback#readme + [package-callback-callbackpayload]: https://github.com/angular-package/callback#callbackpayload [package-callback-resultcallback]: https://github.com/angular-package/callback#resultcallback @@ -1645,6 +301,26 @@ MIT © angular-package ([license][error-license]) [error-github-readme]: https://github.com/angular-package/error#readme + + + [name-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Fname.svg + [name-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Fname.png + [name-npm-badge]: https://badge.fury.io/js/%40angular-package%2Fname + [name-npm-readme]: https://www.npmjs.com/package/@angular-package/name#readme + + + [name-github-readme]: https://github.com/angular-package/name#readme + + + + [preferences-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Fpreferences.svg + [preferences-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Fpreferences.png + [preferences-npm-badge]: https://badge.fury.io/js/%40angular-package%2Fpreferences + [preferences-npm-readme]: https://www.npmjs.com/package/@angular-package/preferences#readme + + + [preferences-github-readme]: https://github.com/angular-package/preferences#readme + [prism-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Fprism.svg @@ -1665,6 +341,16 @@ MIT © angular-package ([license][error-license]) [property-github-readme]: https://github.com/angular-package/property#readme + + + [range-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Frange.svg + [range-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Frange.png + [range-npm-badge]: https://badge.fury.io/js/%40angular-package%2Frange + [range-npm-readme]: https://www.npmjs.com/package/@angular-package/range#readme + + + [range-github-readme]: https://github.com/angular-package/range#readme + [reactive-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Freactive.svg @@ -1675,6 +361,26 @@ MIT © angular-package ([license][error-license]) [reactive-github-readme]: https://github.com/angular-package/reactive#readme + + + [storage-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Fstorage.svg + [storage-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Fstorage.png + [storage-npm-badge]: https://badge.fury.io/js/%40angular-package%2Fstorage + [storage-npm-readme]: https://www.npmjs.com/package/@angular-package/storage#readme + + + [storage-github-readme]: https://github.com/angular-package/storage#readme + + + + [tag-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Ftag.svg + [tag-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Ftag.png + [tag-npm-badge]: https://badge.fury.io/js/%40angular-package%2Ftag + [tag-npm-readme]: https://www.npmjs.com/package/@angular-package/tag#readme + + + [tag-github-readme]: https://github.com/angular-package/tag#readme + [testing-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Ftesting.svg @@ -1685,6 +391,16 @@ MIT © angular-package ([license][error-license]) [testing-github-readme]: https://github.com/angular-package/testing#readme + + + [text-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Ftext.svg + [text-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Ftext.png + [text-npm-badge]: https://badge.fury.io/js/%40angular-package%2Ftext + [text-npm-readme]: https://www.npmjs.com/package/@angular-package/text#readme + + + [text-github-readme]: https://github.com/angular-package/text#readme + [type-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Ftype.svg @@ -1695,10 +411,6 @@ MIT © angular-package ([license][error-license]) [type-github-readme]: https://github.com/angular-package/type#readme - [package-type-valueparser]: https://github.com/angular-package/type#valueparser - [package-type-key]: https://github.com/angular-package/type#key - [package-type-resultcallback]: https://github.com/angular-package/type#resultcallback - [ui-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Fui.svg @@ -1709,6 +421,16 @@ MIT © angular-package ([license][error-license]) [ui-github-readme]: https://github.com/angular-package/ui#readme + + + [wrapper-npm-badge-svg]: https://badge.fury.io/js/%40angular-package%2Fwrapper.svg + [wrapper-npm-badge-png]: https://badge.fury.io/js/%40angular-package%2Fwrapper.png + [wrapper-npm-badge]: https://badge.fury.io/js/%40angular-package%2Fwrapper + [wrapper-npm-readme]: https://www.npmjs.com/package/@angular-package/wrapper#readme + + + [wrapper-github-readme]: https://github.com/angular-package/wrapper#readme + [angular-component-factory-resolver]: https://angular.io/api/core/ComponentFactoryResolver [angular-view-container-ref]: https://angular.io/api/core/ViewContainerRef @@ -1736,7 +458,7 @@ MIT © angular-package ([license][error-license]) [js-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error [js-function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions -[js-function-rest-parameter]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters +[js-rest-parameter]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters [js-getter]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get [js-object-getownpropertydescriptor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor From e6c8d879ea9d5ab50700be68e8aadcc34634a255 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:41:47 +0100 Subject: [PATCH 080/179] docs(README.md): update. --- README.md | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/README.md b/README.md index dce719f..650ad2b 100644 --- a/README.md +++ b/README.md @@ -51,45 +51,18 @@ Manages an [`Error`][js-error]. [![GitHub sponsors][github-badge-sponsor]][github-sponsor-link] [![Support me on Patreon][patreon-badge]][patreon-link] ----- +
## Table of contents -* [Basic concepts](#basic-concepts) * [Skeleton](#skeleton) * [Installation](#installation) -* [Api](#api) * [Changelog](#changelog) * [Git](#git) * [Commit](#commit) * [Versioning](#versioning) * [License](#license) ----- - -
- -## Basic concepts - -Checks -> It's to check the provided value to be **the same** as **expected**. - -Type guard (constrain) -> Constrains the parameter type to **not let** input **unexpected** value in the **code editor**. - -Guards -> It's a **combination** of both above, **constrains** the type of the parameter in the **code editor**, and checks its provided argument. - -Defines -> Returns defined value from a method of an object. -> Defines new value in an object and returns a defined value. - -Gets -> Returns a value from an object. - -Sets -> Adds or updates an element with a specified key and a value to an object and returns an object. -
## Skeleton From 20c9373a37d07855c38cd096a226043be086b5d7 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 2 Feb 2022 23:42:24 +0100 Subject: [PATCH 081/179] docs(README.md): update. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 650ad2b..244f126 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ The angular-package supports the development process of [angular][angulario]-bas Click on the package name to visit its [GitHub](https://github.com/) page. +
+ ## angular-package/error Manages an [`Error`][js-error]. From 17edbb481e24f98444057dc921565e47dc617bca Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 01:12:37 +0100 Subject: [PATCH 082/179] test(ValidationError): update to the new version. --- src/test/validation-error.spec.ts | 466 ++++++++---------------------- 1 file changed, 127 insertions(+), 339 deletions(-) diff --git a/src/test/validation-error.spec.ts b/src/test/validation-error.spec.ts index 085fc29..ba262ba 100644 --- a/src/test/validation-error.spec.ts +++ b/src/test/validation-error.spec.ts @@ -2,15 +2,10 @@ import { Testing, TestingToBeMatchers } from '@angular-package/testing'; // Class. import { ValidationError } from '../lib/validation-error.class'; -// Interface. -import { ErrorMessage } from '../interface/error-message.interface'; /** * Initialize `Testing`. */ -const testing = new Testing(true, true, { - // describe: [1, 8], - // it: [1, 2] -}); +const testing = new Testing(true, true); const toBe = new TestingToBeMatchers(); /** * Tests. @@ -18,383 +13,176 @@ const toBe = new TestingToBeMatchers(); testing.describe('[counter] ValidationError', () => { let errorMessage: string; let fix: string; - let id: number; - let message: ErrorMessage; + let id: string; let problem: string; let template: string; let value: any; - let validationError: ValidationError; + let validationError: ValidationError; // Prepare the values. fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = 427; + id = '427'; problem = 'The value must be a string type.'; - template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; + template = `Problem(VE{id}): {problem}\nFix: {fix}`; value = Symbol(123); beforeEach(() => { // Prepare the values. fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = 427; + id = '427'; problem = 'The value must be a string type.'; - template = `Problem(VE[id]): [problem]\nGot: [value]\nFix: [fix]`; + template = `Problem(VE{id}): {problem}\nFix: {fix}`; value = Symbol(123); - validationError = new ValidationError(); + validationError = new ValidationError(problem, fix, id, template); }); testing - /** - * defineMessage() - */ - .describe('[counter] .defineMessage()', () => { - testing.it(`[counter] with the message of the \`ErrorMessage\` interface`, () => { - errorMessage = ValidationError.defineMessage({ fix, id, problem, template, value}); - toBe.string(errorMessage); - // toContain. - expect(errorMessage).toContain(fix); - expect(errorMessage).toContain(String(id)); - expect(errorMessage).toContain(problem); - expect(errorMessage).toContain(String(value)); - }) - .it(`[counter] with the message of the \`ErrorMessage\` interface with the provided \`parser\``, () => { - message = { fix, id, problem, template, value }; - errorMessage = ValidationError.defineMessage( - message, - (result, checkedValue, payload) => { - toBe - .true(result) - .object(checkedValue) - .undefined(payload); - - expect(checkedValue).toEqual(message); - return result; - } , valueToConvert => String(12345)); - toBe.string(errorMessage); - // toContain. - expect(errorMessage).toContain(fix); - expect(errorMessage).toContain(String(id)); - expect(errorMessage).toContain(problem); - expect(errorMessage).toContain(String(12345)); - }); - }) - - /** - * setTemplate() - */ - .describe('[counter] .setTemplate()', () => { - testing.it(`[counter] propery set the static \`template\``, () => { - expect(ValidationError.template).toEqual(`Problem[id]: [problem] => Fix: [fix]`); - ValidationError.setTemplate(template, (result, checkedValue) => { - toBe.true(result).string(checkedValue); - expect(checkedValue).toEqual(template); - return result; - }); - expect(ValidationError.template).toEqual(template); - }); - }) - - /** - * setValueParser() - */ - .describe('[counter] .setValueParser()', () => { - testing.it(`[counter] properly set the static private \`#defaultValueParser\``, () => { - ValidationError.setValueParser(valueToConvert => String(12345678910), (result, checkedValue) => { - toBe.true(result).function(checkedValue); - return result; - }); - expect(ValidationError.defineMessage({ fix, problem, value, template })).toContain(String(12345678910)); - // The default value parser. - ValidationError.setValueParser(valueToConvert => String(valueToConvert)); - }); - }) /** - * constructor() + * Accessors. */ - .describe('[counter] instantiate', () => { - beforeEach(() => validationError = new ValidationError({ - fix, id, problem, template, value - })); - - testing.it(`[counter] with the message of the \`ErrorMessage\` interface`, () => { - toBe - .string(validationError.fix) - .number(validationError.id) - .string(validationError.message) - .string(validationError.problem) - .string(validationError.template) - .symbol(validationError.value); + .describe(`accessors`, () => { + testing - // to Equal. + /** + * ValidationError.prototype.fix + */ + .it(`ValidationError.prototype.fix`, () => { expect(validationError.fix).toEqual(fix); - expect(validationError.id).toEqual(id); - expect(validationError.problem).toEqual(problem); - expect(validationError.template).toEqual(template); - expect(validationError.value).toEqual(value); - - // toContain. - expect(validationError.message).toContain(fix); - expect(validationError.message).toContain(String(id)); - expect(validationError.message).toContain(problem); - expect(validationError.message).toContain(String(value)); }) - .it(`[counter] with the provided \`parser\``, () => { - validationError = new ValidationError({ - fix, id, problem, template, value - }, (valueToConvert: any) => String(12345)); - toBe - .string(validationError.fix) - .number(validationError.id) - .string(validationError.message) - .string(validationError.problem) - .string(validationError.template) - .symbol(validationError.value); - // to Equal. - expect(validationError.fix).toEqual(fix); + /** + * ValidationError.prototype.id + */ + .it(`ValidationError.prototype.id`, () => { expect(validationError.id).toEqual(id); + }) + + /** + * ValidationError.prototype.problem + */ + .it(`ValidationError.prototype.problem`, () => { expect(validationError.problem).toEqual(problem); - expect(validationError.template).toEqual(template); - expect(validationError.value).toEqual(value); + }) - // toContain. - expect(validationError.message).toContain(fix); - expect(validationError.message).toContain(String(id)); - expect(validationError.message).toContain(problem); - expect(validationError.message).toContain(String(12345)); - }); + /** + * ValidationError.prototype.template + */ + .it(`ValidationError.prototype.template`, () => { + expect(validationError.template).toEqual(template); + }) + // .it(`[Symbol.toStringTag]`, () => {}); }) /** - * .prototype.setFix() + * Static methods. */ - .describe('[counter] .prototype.setFix()', () => { - testing.it(`set ${fix}`, () => { - validationError.setFix(fix, (result, checkedValue, payload) => { - expect(checkedValue).toEqual(fix); - toBe.boolean(result).string(checkedValue).undefined(payload); - return result; - }); - expect(validationError.fix).toEqual(fix); - }); - }) + .describe(`Static methods`, () => { + testing + /** + * isValidationError() + */ + .it(`ValidationError.isValidationError()`, () => { + expect(validationError.id).toEqual(id); + }) - /** - * .prototype.setId() - */ - .describe('[counter] .prototype.setId()', () => { - testing.it(`set ${id}`, () => { - validationError.setFix(fix, (result, checkedValue, payload) => { - expect(checkedValue).toEqual(fix); - toBe.boolean(result).string(checkedValue).undefined(payload); - return result; - }); - expect(validationError.fix).toEqual(fix); + /** + * defineMessage() + */ + .it(`ValidationError.defineMessage`, () => { + errorMessage = ValidationError.defineMessage`${'problem'}${'fix'}${'id'}${'{problem} {fix} {id}'}`; + expect(errorMessage).toEqual('problem fix id'); }); }) /** - * .prototype.setMessage() + * Instance methods. */ - .describe('[counter] .prototype.setMessage()', () => { + .describe(`Instance methods`, () => { testing - .it(`[counter] set works properly`, () => { - validationError.setMessage({ fix, id, problem, template, value }, (result, checkedValue) => { - expect(checkedValue.fix).toEqual(fix); - expect(checkedValue.id).toEqual(id); - expect(checkedValue.problem).toEqual(problem); - expect(checkedValue.value).toEqual(value); - toBe.true(result).object(checkedValue); - return result; - }); - expect(validationError.message).toContain(fix); - expect(validationError.message).toContain(String(id)); - expect(validationError.message).toContain(problem); - expect(validationError.message).toContain(String(value)); - }) - .it(`[counter] set works properly with the provided \`parser\``, () => { - validationError.setMessage( - { fix, id, problem, template, value }, - (result, checkedValue) => { - expect(checkedValue.fix).toEqual(fix); - expect(checkedValue.id).toEqual(id); - expect(checkedValue.problem).toEqual(problem); - expect(checkedValue.value).toEqual(value); - toBe.true(result).object(checkedValue); - return result; - }, valueToConvert => String(12345)); - expect(validationError.message).toContain(fix); - expect(validationError.message).toContain(String(id)); - expect(validationError.message).toContain(problem); - expect(validationError.message).toContain(String(12345)); - }); - }) - - /** - * .prototype.setProblem() - */ - .describe('[counter] .prototype.setProblem()', () => { - testing.it(`[counter] set ${problem}`, () => { - validationError.setProblem(problem, (result, checkedValue) => { - expect(checkedValue).toEqual(problem); - toBe.boolean(result).string(checkedValue); - return result; - }); - expect(validationError.problem).toEqual(problem); - }); - }) + /** + * ValidationError.prototype.setFix() + */ + .it(`ValidationError.prototype.setFix()`, () => { + validationError.setFix('my new fix'); + expect(validationError.fix).toEqual('my new fix'); + }) - /** - * .prototype.setTemplate() - */ - .describe('[counter] .prototype.setTemplate()', () => { - template = `[problem] must be fixed by using [fix]`; - testing.it(`[counter] set ${template}`, () => { - validationError.setTemplate(template, (result, checkedValue, payload) => { - expect(checkedValue).toEqual(template); - toBe.boolean(result).string(checkedValue).object(payload); - return result; - }); - expect(validationError.template).toEqual(template); - }); - }) + /** + * ValidationError.prototype.setId() + */ + .it(`ValidationError.prototype.setId()`, () => { + validationError.setId('my new id'); + expect(validationError.id).toEqual('my new id'); + }) - /** - * .prototype.setValue() - */ - .describe('[counter] .prototype.setValue()', () => { - testing.it(`[counter] set ${String(value)}`, () => { - validationError.setValue(value); - expect(validationError.value).toEqual(value); - }); - }) + // .it(`ValidationError.prototype.setMessage()`, () => { + // validationError.setMessage('my new id'); + // expect(validationError.id).toEqual('my new id'); + // }) + + /** + * ValidationError.prototype.setProblem() + */ + .it(`ValidationError.prototype.setProblem()`, () => { + validationError.setProblem('my new problem'); + expect(validationError.problem).toEqual('my new problem'); + }) - /** - * .prototype.updateMessage() - */ - .describe('[counter] .prototype.updateMessage()', () => { - testing.it(`[counter] works properly`, () => { - validationError - .setFix('') - .setId(0) - .setProblem('') - .setTemplate(template) - .setValue('') - .setValueParser(valueToConvert => String(12345678910)) - .updateMessage(); + /** + * ValidationError.prototype.setTemplate() + */ + .it(`ValidationError.prototype.setTemplate()`, () => { + validationError.setTemplate('my new template'); + expect(validationError.template).toEqual('my new template'); + }) - expect(validationError.fix).toEqual(''); - expect(validationError.id).toEqual(0); - expect(validationError.problem).toEqual(''); - expect(validationError.value).toEqual(''); - expect(validationError.message).toContain(String(12345678910)); + /** + * ValidationError.prototype.throw() + */ + .it(`ValidationError.prototype.throw()`, () => { + try { + validationError.throw(); + } catch (e: any) { + toBe.instance(e, ValidationError); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + expect(e.template).toEqual(template); + expect(e.message).toEqual(ValidationError.defineMessage`${problem}${fix}${id}${template}`); + } + try { + validationError + .setFix('new FIX') + .setId('new ID') + .setProblem('new PROBLEM') + .setTemplate('{problem}{fix}{id}'); + validationError.throw(); + } catch (e: any) { + toBe.instance(e, ValidationError); + expect(e.fix).toEqual('new FIX'); + expect(e.id).toEqual('new ID'); + expect(e.problem).toEqual('new PROBLEM'); + expect(e.template).toEqual('{problem}{fix}{id}'); + expect(e.message).toEqual(ValidationError.defineMessage`${'new PROBLEM'}${'new FIX'}${'new ID'}${'{problem}{fix}{id}'}`); + } + }) + /** + * ValidationError.prototype.updateMessage() + */ + .it(`ValidationError.prototype.updateMessage()`, () => { validationError - .setFix(fix) - .setId(id) - .setProblem(problem) - .setTemplate(template) - .setValue(value) - .setValueParser(valueToConvert => String(valueToConvert)) - .updateMessage((result, checkedValue) => { - toBe.true(result).object(checkedValue); - expect(checkedValue).toEqual({ - fix, id, problem, template, value - }); - return result; - }); - - expect(validationError.fix).toEqual(fix); - expect(validationError.id).toEqual(id); - expect(validationError.problem).toEqual(problem); - expect(validationError.value).toEqual(value); - expect(validationError.message).toContain(String(value)); + .setFix('new FIX') + .setId('new ID') + .setProblem('new PROBLEM') + .setTemplate('{problem}{fix}{id}') + .updateMessage(); + expect(validationError.fix).toEqual('new FIX'); + expect(validationError.id).toEqual('new ID'); + expect(validationError.problem).toEqual('new PROBLEM'); + expect(validationError.template).toEqual('{problem}{fix}{id}'); + expect(validationError.message).toEqual(ValidationError.defineMessage`${'new PROBLEM'}${'new FIX'}${'new ID'}${'{problem}{fix}{id}'}`); }); - }) - - /** - * .prototype.throw() - */ - .describe('[counter] throw', () => { - try { - throw new ValidationError({ fix, id, problem, template, value }); - } catch (e: any) { - testing - .toBeClass(ValidationError) - .toBe('instanceof ValidationError', e instanceof ValidationError, true) - .toBeStringType(e.fix, undefined, '`e.fix` must be of a `string` type') - .toBeNumberType(e.id, undefined, '`e.fix` must be of a `string` type') - .toBeStringType(e.problem, undefined, '`e.problem` must be of a `string` type'); - } - }) - - .describe('[counter] .prototype.throw()', () => { - testing - .it(`[counter] with object of ErrorMessage`, () => { - try { - validationError - .setFix(fix) - .setProblem(problem) - .setTemplate(`[problem], [fix]`); - validationError.throw({ - problem: 'new problem', - fix: 'new fix', - }); - } catch (e) { - expect(e.message).toEqual(`new problem, new fix`); - expect(e.problem).toEqual('new problem'); - expect(e.fix).toEqual('new fix'); - } - }) - .it( - `[counter] with the actual \`problem\`, \`fix\` and \`value\``, - () => { - value = '27'; - try { - validationError - .setProblem(problem) - .setFix(fix) - .setValue(value) - .setTemplate(`[problem], [fix], [value]`) - .throw(); - } catch (e) { - expect(e.message).toEqual(`${problem}, ${fix}, ${String(value)}`); - expect(e.fix).toEqual(fix); - expect(e.problem).toEqual(problem); - expect(e.value).toEqual(value); - } - - try { - validationError - .setMessage({ - fix, - problem, - value, - template: `[problem] [fix] [value]`, - }) - .throw(); - } catch (e) { - expect(e.message).toEqual(`${problem} ${fix} ${String(value)}`); - expect(e.fix).toEqual(fix); - expect(e.problem).toEqual(problem); - expect(e.value).toEqual(value); - } - - try { - validationError - .setMessage({ - fix, - problem, - template: `[problem] [fix] [value]`, - }) - .throw(); - } catch (e) { - expect(e.message).toEqual(`${problem} ${fix} ${String(value)}`); - expect(e.fix).toEqual(fix); - expect(e.problem).toEqual(problem); - expect(e.value).toEqual(value); - } - } - ); }); }); From 908937597024576ad5d47fd1f1af652c1a2cc265 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 15:11:40 +0100 Subject: [PATCH 083/179] feat(CommonError): add abstract class with common features for other type of errors. --- src/lib/common-error.class.ts | 125 ++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/lib/common-error.class.ts diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts new file mode 100644 index 0000000..cdf4719 --- /dev/null +++ b/src/lib/common-error.class.ts @@ -0,0 +1,125 @@ +/** + * The `CommonError` abstract object to throw an identified error with a solution to the described problem. + */ +export abstract class CommonError extends Error { + /** + * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}` words. + * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. It can be set directly or by the `setTemplate()`. + */ + public static template = `Problem{id}: {problem} => Fix: {fix}`; + + //#region public instance accessors. + /** + * The get `accessor` obtains a possible solution to the described problem by returning the `#fix` property of a specified object. + * @returns The return value is the fix of a `string` type. + * @angularpackage + */ + public get fix(): string { + return this.#fix; + } + + /** + * The `get` accessor gets the error identification by returning the `#id` property of a specified object. + * @returns The return value is the error identification of the generic type variable `Id`. + * @angularpackage + */ + public get id(): Id { + return this.#id; + } + + /** + * The `get` accessor gets the error message by returning the parent `message` property of a specified object. + * @returns The return value is the error message of a `string` type. + * @angularpackage + */ + public get message(): string { + return super.message; + } + + /** + * The `get` accessor gets the problem by returning the `#problem` property of a specified object. + * @returns The return value is the problem of a `string` type. + * @angularpackage + */ + public get problem(): string { + return this.#problem; + } + + /** + * The `get` accessor gets the template of the error message by returning the `#template` property of a specified object. + * @returns The return value is the template of a `string` type. + * @angularpackage + */ + public get template(): string { + return this.#template; + } + //#endregion public instance accessors. + + //#region private instance properties. + /** + * A privately stored possible solution to the described problem of a `string` type. + */ + #fix: string; + + /** + * A privately stored unique identification to the described problem of generic type variable `Id`. + */ + #id: Id = '' as Id; + + /** + * A privately stored problem of a `string` type. + */ + #problem: string; + + /** + * A string-type privately stored template of the error message that contains replaceable `{fix}`, `{problem}` and optional `{id}` words. + */ + #template: string; + //#endregion private instance properties. + + //#region public static methods. + /** + * The static "tag" method builds from the given `values` the error message of a string type on the template. + * @param templateStringsArray - + * @param values A rest parameter of expressions in order the `problem`, `fix`, `id` and `template`. + * @returns The return value is the error message of a `string` type created from the expressions given in the `values`. + * @angularpackage + */ + public static defineMessage( + templateStringsArray: TemplateStringsArray, + ...values: any[] + ): string { + let problem: string, fix: string, id: string, template: string; + [problem, fix, id, template] = values; + return template + .replace('{problem}', problem) + .replace('{fix}', fix) + .replace('{id}', id); + } + //#endregion public static methods. + + //#region constructor. + /** + * Creates an instance that represents an error with the described problem and its solution, optionally marked with an explicit + * identification. + * @param problem Description of the problem of a string type. + * @param fix A solution to the given `problem` of a string type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @angularpackage + */ + constructor( + problem: string, + fix: string, + id?: Id, + template = CommonError.template + ) { + super(CommonError.defineMessage`${problem}${fix}${id}${template}`); + this.#fix = fix; + this.#problem = problem; + this.#id = id as Id; + this.#template = template; + } + //#endregion constructor. +} From 789083e5c79d6ee0f1f098bcc5a352a8dccf939b Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 15:46:43 +0100 Subject: [PATCH 084/179] feat(Error): add `Error` object extension for the `CommonError` class. --- src/lib/error.class.ts | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/lib/error.class.ts diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts new file mode 100644 index 0000000..2afb3b6 --- /dev/null +++ b/src/lib/error.class.ts @@ -0,0 +1,60 @@ +import { CommonError } from './common-error.class'; +/** + * The `Error` object is an extension for javascript `Error` which means it's thrown when a runtime error occurs but with additional + * identification and a solution to the described problem. + */ +export class Error extends CommonError { + /** + * Error name of a `string` type, set to `Error` that is being thrown. + * @angularpackage + */ + public get name(): string { + return 'Error'; + } + + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'Error'` for an instance of `Error`. It can be read + * by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'Error` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'Error'; + } + + /** + * Checks whether the value of any type is an instance of `Error` of any or the given identification. + * @param value The value of any type to check against the `Error` instance. + * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `Error` of any or the given `id`. + */ + public static isError( + value: any, + id?: Id + ): value is Error { + return typeof value === 'object' && + value instanceof this && + typeof id === 'string' + ? value.id === id + : true; + } + + /** + * Creates the `Error` instance that represents an error with the described problem and its solution, optionally marked with an explicit + * identification. + * @param problem Description of the problem of a string type. + * @param fix A solution to the given `problem` of a string type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @angularpackage + */ + constructor( + problem: string, + fix: string, + id?: Id, + template = Error.template, + ) { + super(problem, fix, id, template); + } +} From eb2d8e243ff6ee5f44fd00e4d462d2b2c175702a Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 15:58:50 +0100 Subject: [PATCH 085/179] refactor(ValidationError): complete simplify the object. --- src/lib/validation-error.class.ts | 228 +++++++----------------------- 1 file changed, 52 insertions(+), 176 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 8ae96f4..c0b5b8e 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,48 +1,55 @@ +import { CommonError } from './common-error.class'; /** * The `ValidationError` object to throw an error with a solution to the described problem. */ -export class ValidationError extends Error { - /** - * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}` words. - * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. It can be set directly or by the `setTemplate()`. - */ - public static template = `Problem{id}: {problem} => Fix: {fix}`; - - //#region public instance accessors. - /** - * The get `accessor` obtains a possible solution to the described problem by returning the `#fix` property of a specified object. - * @returns The return value is the fix of a `string` type. - * @angularpackage - */ - public get fix(): string { - return this.#fix; - } - +export class ValidationError extends CommonError { + //#region public static methods. /** - * The `get` accessor gets the `id` by returning the `#id` property of a specified object. - * @returns The return value is the id of the generic type variable `Id`. + * Defines the `ValidationError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @returns The return value is a new instance of the `ValidationError` with the given required `problem`, `fix` and optional `id` and + * `template`. * @angularpackage */ - public get id(): Id { - return this.#id; + public static define( + problem: string, + fix: string, + id?: Id, + template = ValidationError.template + ): ValidationError { + return new this(problem, fix, id, template); } /** - * The `get` accessor gets the problem by returning the `#problem` property of a specified object. - * @returns The return value is the problem of a `string` type. + * Throws the `ValidationError` with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. * @angularpackage */ - public get problem(): string { - return this.#problem; + public static throw( + problem: string, + fix: string, + id?: string, + template?: string + ): void { + throw this.define(problem, fix, id, template); } + //#endregion public static methods. + //#region public instance accessors. /** - * The `get` accessor gets the template by returning the `#template` property of a specified object. - * @returns The return value is the template of a `string` type. + * Error name of a `string` type, set to `ValidationError` that is being thrown. * @angularpackage */ - public get template(): string { - return this.#template; + public get name(): string { + return 'ValidationError'; } /** @@ -56,72 +63,33 @@ export class ValidationError extends Error { } //#endregion public instance accessors. - //#region instance public properties. - /** - * Error name of a `string` type that is being thrown. By default, it's `ValidationError`. - */ - public name = ValidationError.name; - //#endregion instance public properties. - - //#region private instance properties. - /** - * A privately stored possible solution to the described problem of a `string` type. - */ - #fix: string; - - /** - * A privately stored unique identification to the described problem of generic type variable `Id`. - */ - #id: Id = '' as Id; - - /** - * A privately stored validation problem of a `string` type. - */ - #problem: string; - - /** - * A string-type privately stored template of the error message that contains replaceable `{fix}`, `{problem}` and optional `{id}` words. - */ - #template: string; - //#endregion private instance properties. - //#region public static methods. /** * Checks whether the value of any type is an instance of `ValidationError`. * @param value The value of any type to check against the `ValidationError` instance. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `ValidationError`. */ - public static isValidationError(value: any): value is ValidationError { - return typeof value === 'object' && value instanceof this; - } - - /** - * The static "tag" method builds from the given `values` the validation error message of a string type on the template. - * @param templateStringsArray - - * @param values A rest parameter of expressions in order the `problem`, `fix`, `id` and `template`. - * @returns The return value is the validation error message of a `string` type created from the expressions given in the `values`. - * @angularpackage - */ - public static defineMessage( - templateStringsArray: TemplateStringsArray, - ...values: any[] - ): string { - let problem: string, fix: string, id: string, template: string; - [problem, fix, id, template] = values; - return template - .replace('{problem}', problem) - .replace('{fix}', fix) - .replace('{id}', id); + public static isValidationError( + value: any, + id?: Id + ): value is ValidationError { + return typeof value === 'object' && + value instanceof this && + typeof id === 'number' + ? value.id === id + : true; } //#endregion public static methods. //#region constructor. /** - * Creates a `ValidationError` instance that represents validation problem with solution and optional unique identification. - * @param problem Description of the validation problem of a string type. - * @param fix A solution to the given `problem` of a string type. + * Creates a `ValidationError` instance that represents an error with the described problem and its solution, optionally marked with an + * explicit identification. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of validation error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. * @angularpackage */ constructor( @@ -130,99 +98,7 @@ export class ValidationError extends Error { id?: Id, template = ValidationError.template ) { - super(ValidationError.defineMessage`${problem}${fix}${id}${template}`); - this.#fix = fix; - this.#problem = problem; - this.#id = id as Id; - this.#template = template; + super(problem, fix, id, template); } //#endregion constructor. - - //#region instance public methods. - /** - * Sets the fix a possible solution to the described problem. - * @param fix A possible solution to the described problem of a `string` type. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setFix(fix: string): this { - this.#fix = fix; - return this; - } - - /** - * Sets the unique identification to the described problem. - * @param id Unique identification of a string type of described problem. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setId(id: CustomId): this { - this.#id = id; - return this; - } - - /** - * Sets the validation error message of a `string` type from the provided `message` of the `ErrorMessage` interface. - * @param message An object of an `ErrorMessage` interface to build the message of a `string` type. The value is checked against - * the proper `object`. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setMessage( - problem: string, - fix: string, - id?: CustomId - ): this { - this.message = ValidationError.defineMessage`${problem}${fix}${id}${ - this.#template - }`; - return this; - } - - /** - * Sets description problem of a validation. - * @param problem Description of validation problem of a `string` type. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setProblem(problem: string): this { - this.#problem = problem; - return this; - } - - /** - * Sets the template of validation error message. - * @param template A message template of `string` type with replaceable `{problem}`, `{fix}` and optional `{id}` words. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setTemplate(template: string): this { - this.#template = template; - return this; - } - - /** - * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem`, and optional `id` on the stored - * `template`. - * @angularpackage - */ - public throw(): void { - throw new ValidationError( - this.#problem, - this.#fix, - this.#id, - this.#template - ); - } - - /** - * Updates the message with a stored `fix`, `problem`, and optional `id` on the stored `template`. - * @angularpackage - */ - public updateMessage(): void { - this.message = ValidationError.defineMessage`${this.#problem}${this.#fix}${ - this.#id - }${this.#template}`; - } - //#endregion instance public methods. } From 89bba4a055b66cbeb2b64537bae6b6aec4f916d0 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 16:36:12 +0100 Subject: [PATCH 086/179] docs(ValidationError): update. --- src/lib/validation-error.class.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index c0b5b8e..10cb66e 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,6 +1,7 @@ import { CommonError } from './common-error.class'; /** - * The `ValidationError` object to throw an error with a solution to the described problem. + * The `ValidationError` object is thrown when an operation could not be performed despite proper type and proper expected value, along with + * additional identification and a solution to the described problem. */ export class ValidationError extends CommonError { //#region public static methods. @@ -83,13 +84,13 @@ export class ValidationError extends CommonError { //#region constructor. /** - * Creates a `ValidationError` instance that represents an error with the described problem and its solution, optionally marked with an - * explicit identification. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. + * Creates a `ValidationError` instance that represents validation error with the described problem and its solution, optionally marked + * with an explicit identification. + * @param problem Description of the validation problem of a `string` type. + * @param fix A solution to the given validation issue of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. + * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the + * value is picked from the static property `template`. * @angularpackage */ constructor( From 798964e6c53475e5890be24620bd58b48813ddc3 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 16:37:08 +0100 Subject: [PATCH 087/179] refactor(CommonError): set the empty string as the default value for `id` parameter. --- src/lib/common-error.class.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index cdf4719..4b39dd4 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -64,7 +64,7 @@ export abstract class CommonError extends Error { /** * A privately stored unique identification to the described problem of generic type variable `Id`. */ - #id: Id = '' as Id; + #id: Id; /** * A privately stored problem of a `string` type. @@ -112,13 +112,13 @@ export abstract class CommonError extends Error { constructor( problem: string, fix: string, - id?: Id, + id: Id = '' as Id, template = CommonError.template ) { super(CommonError.defineMessage`${problem}${fix}${id}${template}`); this.#fix = fix; + this.#id = id; this.#problem = problem; - this.#id = id as Id; this.#template = template; } //#endregion constructor. From 30716b22970218bb4745d0482908e55138467833 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 16:43:33 +0100 Subject: [PATCH 088/179] chore(API): remove `ErrorMessage` and `MessageBuilder` `MessageFunctionBuilder` and `MessageBuilderTemplate` exports. --- src/public-api.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/public-api.ts b/src/public-api.ts index 83f99bb..c1326b8 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -2,16 +2,16 @@ * Public API Surface of error */ // Class. -export { ValidationError } from './lib/validation-error.class'; -// Interface. -export { ErrorMessage } from './interface/error-message.interface'; - -/* - * Experimental - */ export { - // Class. - MessageBuilder, - MessageFunctionBuilder, - MessageBuilderTemplate, -} from './message-builder/'; + CommonError, + Error, + Errors, + EvalError, + InternalError, + RangeError, + ReferenceError, + SyntaxError, + TypeError, + URIError, + ValidationError +} from './lib'; From 527f163a9eaabcc5f3e1437800bf1e25070e4cc2 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 16:45:31 +0100 Subject: [PATCH 089/179] chore(lib/index): exports all errors. --- src/lib/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/lib/index.ts diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..678fc34 --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1,11 @@ +export { CommonError } from './common-error.class'; +export { Error } from './error.class'; +export { Errors } from './errors.class'; +export { EvalError } from './eval-error.class'; +export { InternalError } from './internal-error.class'; +export { RangeError } from './range-error.class'; +export { ReferenceError } from './reference-error.class'; +export { SyntaxError } from './syntax-error.class'; +export { TypeError } from './type-error.class'; +export { URIError } from './uri-error.class'; +export { ValidationError } from './validation-error.class'; From 77e4e0a3760150a515f3a59b5efd5c779221427e Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 17:24:12 +0100 Subject: [PATCH 090/179] feat(RangeError): add new error. --- src/lib/range-error.class.ts | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/lib/range-error.class.ts diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts new file mode 100644 index 0000000..49f6c9a --- /dev/null +++ b/src/lib/range-error.class.ts @@ -0,0 +1,86 @@ +import { CommonError } from './common-error.class'; +/** + * The `RangeError` object is thrown when a value is not in the set or range of allowed values, along with additional identification and + * a solution to the described problem. + */ +export class RangeError extends CommonError { + //#region public static methods. + /** + * Defines the `RangeError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @returns The return value is a new instance of the `RangeError` with the given required `problem`, `fix` and optional `id` and + * `template`. + * @angularpackage + */ + public static define( + problem: string, + fix: string, + id?: Id, + template = RangeError.template + ): RangeError { + return new this(problem, fix, id, template); + } + + /** + * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @angularpackage + */ + public static throw( + problem: string, + fix: string, + id?: string, + template?: string + ): void { + throw this.define(problem, fix, id, template); + } + //#endregion public static methods. + + //#region public instance accessors. + /** + * Error name of a `string` type, set to `RangeError` that is being thrown. + */ + public get name(): string { + return 'RangeError'; + } + + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'RangeError'` for an instance of + * `RangeError`. It can be read by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'RangeError` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'RangeError'; + } + //#endregion public instance accessors. + + //#region constructor. + /** + * Creates a `RangeError` instance that represents range error with the described problem and its solution, optionally marked + * with an explicit identification. + * @param problem Description of the range problem of a `string` type. + * @param fix A solution to the given range issue of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the + * value is picked from the static property `template`. + * @angularpackage + */ + constructor( + problem: string, + fix: string, + id?: Id, + template = RangeError.template + ) { + super(problem, fix, id, template); + } + //#endregion constructor. +} From 8112c166a2a7848b166bd4a45996f6e24b42862e Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 17:36:21 +0100 Subject: [PATCH 091/179] feat(TypeError): add new error object. --- src/lib/type-error.class.ts | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/lib/type-error.class.ts diff --git a/src/lib/type-error.class.ts b/src/lib/type-error.class.ts new file mode 100644 index 0000000..5633b3d --- /dev/null +++ b/src/lib/type-error.class.ts @@ -0,0 +1,86 @@ +import { CommonError } from './common-error.class'; +/** + * The `TypeError` object is thrown when an operation could not be performed, typically(but not exclusively) when a value is not of the + * expected type, along with additional identification and a solution to the described problem. + */ +export class TypeError extends CommonError { + //#region public static methods. + /** + * Defines the `TypeError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @returns The return value is a new instance of the `TypeError` with the given required `problem`, `fix` and optional `id` and + * `template`. + * @angularpackage + */ + public static define( + problem: string, + fix: string, + id?: Id, + template = TypeError.template + ): TypeError { + return new this(problem, fix, id, template); + } + + /** + * Throws the `TypeError` with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @angularpackage + */ + public static throw( + problem: string, + fix: string, + id?: string, + template?: string + ): void { + throw this.define(problem, fix, id, template); + } + //#endregion public static methods. + + //#region public instance accessors. + /** + * Error name of a `string` type that is being thrown. By default, it's `TypeError`. + */ + public get name(): string { + return 'TypeError'; + } + + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'TypeError'` for an instance of + * `TypeError`. It can be read by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'TypeError` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'TypeError'; + } + //#endregion public instance accessors. + + //#region constructor. + /** + * Creates a `TypeError` instance that represents type error with the described problem and its solution, optionally marked with an + * explicit identification. + * @param problem Description of the range problem of a `string` type. + * @param fix A solution to the given range issue of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the + * value is picked from the static property `template`. + * @angularpackage + */ + constructor( + problem: string, + fix: string, + id?: Id, + template = TypeError.template + ) { + super(problem, fix, id, template); + } + //#endregion constructor. +} From f67f99dbc77140c4f4d891073d45baef273c9ebc Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 20:16:54 +0100 Subject: [PATCH 092/179] refactor(Error): add `define()` and `throw()` public static methods, add regions. --- src/lib/error.class.ts | 49 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index 2afb3b6..519289f 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -4,6 +4,7 @@ import { CommonError } from './common-error.class'; * identification and a solution to the described problem. */ export class Error extends CommonError { + //#region public instance accessors. /** * Error name of a `string` type, set to `Error` that is being thrown. * @angularpackage @@ -21,6 +22,28 @@ export class Error extends CommonError { public get [Symbol.toStringTag](): string { return 'Error'; } + //#endregion public instance accessors. + + //#region public static methods. + /** + * Defines the `RangeError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @returns The return value is a new instance of the `RangeError` with the given required `problem`, `fix` and optional `id` and + * `template`. + * @angularpackage + */ + public static define( + problem: string, + fix: string, + id?: Id, + template = Error.template + ): Error { + return new this(problem, fix, id, template); + } /** * Checks whether the value of any type is an instance of `Error` of any or the given identification. @@ -28,7 +51,7 @@ export class Error extends CommonError { * @param id Optional identification of generic type variable `Id` that the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `Error` of any or the given `id`. */ - public static isError( + public static isError( value: any, id?: Id ): value is Error { @@ -39,6 +62,27 @@ export class Error extends CommonError { : true; } + /** + * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @angularpackage + */ + public static throw( + problem: string, + fix: string, + id?: string, + template?: string + ): void { + throw this.define(problem, fix, id, template); + } + + //#endregion public static methods. + + //#region constructor. /** * Creates the `Error` instance that represents an error with the described problem and its solution, optionally marked with an explicit * identification. @@ -53,8 +97,9 @@ export class Error extends CommonError { problem: string, fix: string, id?: Id, - template = Error.template, + template = Error.template ) { super(problem, fix, id, template); } + //#endregion constructor. } From acd3c065b2f1821761e9ed7e6e09f70a68973b17 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 20:20:34 +0100 Subject: [PATCH 093/179] refactor(RangeError): add `isRangeError()` public static method, reorder class members. --- src/lib/range-error.class.ts | 62 +++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts index 49f6c9a..44572e9 100644 --- a/src/lib/range-error.class.ts +++ b/src/lib/range-error.class.ts @@ -4,6 +4,25 @@ import { CommonError } from './common-error.class'; * a solution to the described problem. */ export class RangeError extends CommonError { + //#region public instance accessors. + /** + * Error name of a `string` type, set to `RangeError` that is being thrown. + */ + public get name(): string { + return 'RangeError'; + } + + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'RangeError'` for an instance of + * `RangeError`. It can be read by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'RangeError` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'RangeError'; + } + //#endregion public instance accessors. + //#region public static methods. /** * Defines the `RangeError` instance with the given required `problem`, `fix` and optional `id` and `template`. @@ -16,7 +35,7 @@ export class RangeError extends CommonError { * `template`. * @angularpackage */ - public static define( + public static define( problem: string, fix: string, id?: Id, @@ -25,6 +44,24 @@ export class RangeError extends CommonError { return new this(problem, fix, id, template); } + /** + * Checks whether the value of any type is an instance of `RangeError` of any or the given identification. + * @param value The value of any type to check against the `RangeError` instance. + * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `RangeError` of any or the given + * `id`. + */ + public static isRangeError( + value: any, + id?: Id + ): value is RangeError { + return typeof value === 'object' && + value instanceof this && + typeof id === 'string' + ? value.id === id + : true; + } + /** * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. * @param problem Description of the problem of a `string` type. @@ -44,29 +81,10 @@ export class RangeError extends CommonError { } //#endregion public static methods. - //#region public instance accessors. - /** - * Error name of a `string` type, set to `RangeError` that is being thrown. - */ - public get name(): string { - return 'RangeError'; - } - - /** - * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'RangeError'` for an instance of - * `RangeError`. It can be read by the `typeOf()` function of `@angular-package/type`. - * @returns The return value is the word 'RangeError` of a `string`. - * @angularpackage - */ - public get [Symbol.toStringTag](): string { - return 'RangeError'; - } - //#endregion public instance accessors. - //#region constructor. /** - * Creates a `RangeError` instance that represents range error with the described problem and its solution, optionally marked - * with an explicit identification. + * Creates a `RangeError` instance that represents range error with the described problem and its solution, optionally marked with an + * explicit identification. * @param problem Description of the range problem of a `string` type. * @param fix A solution to the given range issue of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. From 9a426298540d7f7e2b7c932ac1326630947920d0 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 20:23:17 +0100 Subject: [PATCH 094/179] style(CommonError): add public static properties region. --- src/lib/common-error.class.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index 4b39dd4..b951c9b 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -2,11 +2,13 @@ * The `CommonError` abstract object to throw an identified error with a solution to the described problem. */ export abstract class CommonError extends Error { + //#region public static properties. /** * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}` words. * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. It can be set directly or by the `setTemplate()`. */ public static template = `Problem{id}: {problem} => Fix: {fix}`; + //#endregion public static properties. //#region public instance accessors. /** From 678b46351296d6276d6bc0d7ca9794a2b57a63f0 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 3 Feb 2022 23:58:54 +0100 Subject: [PATCH 095/179] refactor(CommonError): add common static protected `define()`, `iserror()` `throw()` methods, set `constructor()` as protected. --- src/lib/common-error.class.ts | 59 ++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index b951c9b..c76ac3e 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -98,6 +98,63 @@ export abstract class CommonError extends Error { .replace('{fix}', fix) .replace('{id}', id); } + + /** + * Defines the instance from the given `constructor` with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @param constructor + * @returns The return value is a new instance from the given `constructor` with the given required `problem`, `fix` and optional `id` and + * `template`. + * @angularpackage + */ + protected static define( + problem: string, + fix: string, + id?: Id, + template = CommonError.template, + constructor?: any + ): any { + return new constructor(problem, fix, id, template); + } + + /** + * Checks whether the value of any type is an instance of `Error` of any or the given identification. + * @param value The value of any type to check against the `Error` instance. + * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `Error` of any or the given `id`. + */ + protected static isError( + value: any, + id?: Id + ): value is CommonError { + return typeof value === 'object' && + value instanceof this && + typeof id === 'string' + ? value.id === id + : true; + } + + /** + * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. + * @param id Optional unique identification to the given `problem` of generic type variable `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * is picked from the static property `template`. + * @angularpackage + */ + protected static throw( + problem: string, + fix: string, + id?: string, + template?: string + ): void { + throw this.define(problem, fix, id, template, this); + } //#endregion public static methods. //#region constructor. @@ -111,7 +168,7 @@ export abstract class CommonError extends Error { * is picked from the static property `template`. * @angularpackage */ - constructor( + protected constructor( problem: string, fix: string, id: Id = '' as Id, From 350f8b4fd6028f3b8ab0cdb2fcb22aa95c9f2a6a Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 00:02:14 +0100 Subject: [PATCH 096/179] refactor(Error): change the static public `define()` `isError()` `throw()` methods to use parent methods by using super, and update jsdoc. --- src/lib/error.class.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index 519289f..b9c9ec9 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -1,7 +1,6 @@ import { CommonError } from './common-error.class'; /** - * The `Error` object is an extension for javascript `Error` which means it's thrown when a runtime error occurs but with additional - * identification and a solution to the described problem. + * The `Error` object is thrown when a runtime error occurs, but with additional identification and a solution to the described problem. */ export class Error extends CommonError { //#region public instance accessors. @@ -26,14 +25,13 @@ export class Error extends CommonError { //#region public static methods. /** - * Defines the `RangeError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * Defines the `Error` instance with the given required `problem`, `fix` and optional `id` and `template`. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value * is picked from the static property `template`. - * @returns The return value is a new instance of the `RangeError` with the given required `problem`, `fix` and optional `id` and - * `template`. + * @returns The return value is a new instance of the `Error` with the given required `problem`, `fix` and optional `id` and `template`. * @angularpackage */ public static define( @@ -42,7 +40,7 @@ export class Error extends CommonError { id?: Id, template = Error.template ): Error { - return new this(problem, fix, id, template); + return super.define(problem, fix, id, template, this); } /** @@ -50,16 +48,13 @@ export class Error extends CommonError { * @param value The value of any type to check against the `Error` instance. * @param id Optional identification of generic type variable `Id` that the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `Error` of any or the given `id`. + * @angularpackage */ public static isError( value: any, id?: Id ): value is Error { - return typeof value === 'object' && - value instanceof this && - typeof id === 'string' - ? value.id === id - : true; + return super.isError(value, id); } /** @@ -77,9 +72,8 @@ export class Error extends CommonError { id?: string, template?: string ): void { - throw this.define(problem, fix, id, template); + super.throw(problem, fix, id, template); } - //#endregion public static methods. //#region constructor. From 723f1e15350f05cfcc48d91ef0c7495a8da37a06 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 00:03:57 +0100 Subject: [PATCH 097/179] refactor(ValidationError): reorder class members, change the static public `define()` `isValidationError()` `throw()` methods to use parent methods by using super and updatej jsdoc. --- src/lib/validation-error.class.ts | 76 ++++++++++++++----------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 10cb66e..3b9c329 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,9 +1,29 @@ import { CommonError } from './common-error.class'; /** - * The `ValidationError` object is thrown when an operation could not be performed despite proper type and proper expected value, along with + * The `ValidationError` object is thrown when an operation could not be performed despite proper type(but not exclusively), along with * additional identification and a solution to the described problem. */ export class ValidationError extends CommonError { + //#region public instance accessors. + /** + * Error name of a `string` type, set to `ValidationError` that is being thrown. + * @angularpackage + */ + public get name(): string { + return 'ValidationError'; + } + + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'ValidationError'` for an instance of + * `ValidationError`. It can be read by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'ValidationError` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'ValidationError'; + } + //#endregion public instance accessors. + //#region public static methods. /** * Defines the `ValidationError` instance with the given required `problem`, `fix` and optional `id` and `template`. @@ -22,7 +42,19 @@ export class ValidationError extends CommonError { id?: Id, template = ValidationError.template ): ValidationError { - return new this(problem, fix, id, template); + return super.define(problem, fix, id, template, this); + } + + /** + * Checks whether the value of any type is an instance of `ValidationError`. + * @param value The value of any type to check against the `ValidationError` instance. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `ValidationError`. + */ + public static isValidationError( + value: any, + id?: Id + ): value is ValidationError { + return super.isError(value, id); } /** @@ -40,45 +72,7 @@ export class ValidationError extends CommonError { id?: string, template?: string ): void { - throw this.define(problem, fix, id, template); - } - //#endregion public static methods. - - //#region public instance accessors. - /** - * Error name of a `string` type, set to `ValidationError` that is being thrown. - * @angularpackage - */ - public get name(): string { - return 'ValidationError'; - } - - /** - * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'ValidationError'` for an instance of - * `ValidationError`. It can be read by the `typeOf()` function of `@angular-package/type`. - * @returns The return value is the word 'ValidationError` of a `string`. - * @angularpackage - */ - public get [Symbol.toStringTag](): string { - return 'ValidationError'; - } - //#endregion public instance accessors. - - //#region public static methods. - /** - * Checks whether the value of any type is an instance of `ValidationError`. - * @param value The value of any type to check against the `ValidationError` instance. - * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `ValidationError`. - */ - public static isValidationError( - value: any, - id?: Id - ): value is ValidationError { - return typeof value === 'object' && - value instanceof this && - typeof id === 'number' - ? value.id === id - : true; + super.throw(problem, fix, id, template); } //#endregion public static methods. From fb82bf3e823f4c102331be44a6a02eff16e1f50b Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 00:06:35 +0100 Subject: [PATCH 098/179] refactor(TypeError): reorder class members, changed the static public `define()` `throw()` to use parent methods by using super, add static public method `isTypeError()` and update jsdoc. --- src/lib/type-error.class.ts | 56 +++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/lib/type-error.class.ts b/src/lib/type-error.class.ts index 5633b3d..2299ed6 100644 --- a/src/lib/type-error.class.ts +++ b/src/lib/type-error.class.ts @@ -4,6 +4,25 @@ import { CommonError } from './common-error.class'; * expected type, along with additional identification and a solution to the described problem. */ export class TypeError extends CommonError { + //#region public instance accessors. + /** + * Error name of a `string` type that is being thrown. By default, it's `TypeError`. + */ + public get name(): string { + return 'TypeError'; + } + + /** + * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'TypeError'` for an instance of + * `TypeError`. It can be read by the `typeOf()` function of `@angular-package/type`. + * @returns The return value is the word 'TypeError` of a `string`. + * @angularpackage + */ + public get [Symbol.toStringTag](): string { + return 'TypeError'; + } + //#endregion public instance accessors. + //#region public static methods. /** * Defines the `TypeError` instance with the given required `problem`, `fix` and optional `id` and `template`. @@ -22,7 +41,21 @@ export class TypeError extends CommonError { id?: Id, template = TypeError.template ): TypeError { - return new this(problem, fix, id, template); + return super.define(problem, fix, id, template, this); + } + + /** + * Checks whether the value of any type is an instance of `TypeError` of any or the given identification. + * @param value The value of any type to check against the `TypeError` instance. + * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `TypeError` of any or the given + * `id`. + */ + public static isTypeError( + value: any, + id?: Id + ): value is TypeError { + return super.isError(value, id); } /** @@ -40,29 +73,10 @@ export class TypeError extends CommonError { id?: string, template?: string ): void { - throw this.define(problem, fix, id, template); + super.throw(problem, fix, id, template); } //#endregion public static methods. - //#region public instance accessors. - /** - * Error name of a `string` type that is being thrown. By default, it's `TypeError`. - */ - public get name(): string { - return 'TypeError'; - } - - /** - * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'TypeError'` for an instance of - * `TypeError`. It can be read by the `typeOf()` function of `@angular-package/type`. - * @returns The return value is the word 'TypeError` of a `string`. - * @angularpackage - */ - public get [Symbol.toStringTag](): string { - return 'TypeError'; - } - //#endregion public instance accessors. - //#region constructor. /** * Creates a `TypeError` instance that represents type error with the described problem and its solution, optionally marked with an From 1d16feed45cf479b8d346de04360976e92ab3f84 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 00:08:36 +0100 Subject: [PATCH 099/179] refactor(RangeError): change the static public `define()` `isRangeError()` and `throw()` methods to use parent methods by using super. --- src/lib/range-error.class.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts index 44572e9..2bef225 100644 --- a/src/lib/range-error.class.ts +++ b/src/lib/range-error.class.ts @@ -7,6 +7,7 @@ export class RangeError extends CommonError { //#region public instance accessors. /** * Error name of a `string` type, set to `RangeError` that is being thrown. + * @angularpackage */ public get name(): string { return 'RangeError'; @@ -41,7 +42,7 @@ export class RangeError extends CommonError { id?: Id, template = RangeError.template ): RangeError { - return new this(problem, fix, id, template); + return super.define(problem, fix, id, template, this); } /** @@ -50,16 +51,13 @@ export class RangeError extends CommonError { * @param id Optional identification of generic type variable `Id` that the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `RangeError` of any or the given * `id`. + * @angularpackage */ public static isRangeError( value: any, id?: Id ): value is RangeError { - return typeof value === 'object' && - value instanceof this && - typeof id === 'string' - ? value.id === id - : true; + return super.isError(value, id); } /** @@ -77,7 +75,7 @@ export class RangeError extends CommonError { id?: string, template?: string ): void { - throw this.define(problem, fix, id, template); + super.throw(problem, fix, id, template); } //#endregion public static methods. From e83cc30b73a67b51a70a5099f08396820f45d3d8 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 00:09:21 +0100 Subject: [PATCH 100/179] chore(index): remove unnecessary objects `EvalError` `InternalError` `ReferenceError` `SyntaxError` `URIError`. --- src/lib/index.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 678fc34..006d890 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,11 +1,6 @@ export { CommonError } from './common-error.class'; export { Error } from './error.class'; export { Errors } from './errors.class'; -export { EvalError } from './eval-error.class'; -export { InternalError } from './internal-error.class'; export { RangeError } from './range-error.class'; -export { ReferenceError } from './reference-error.class'; -export { SyntaxError } from './syntax-error.class'; export { TypeError } from './type-error.class'; -export { URIError } from './uri-error.class'; export { ValidationError } from './validation-error.class'; From bbbcbe44817f9dc5617f4b5d8bfd575b1ce5737c Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 00:09:42 +0100 Subject: [PATCH 101/179] chore(API): remove unnecessary objects `EvalError` `InternalError` `ReferenceError` `SyntaxError` `URIError`. --- src/public-api.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/public-api.ts b/src/public-api.ts index c1326b8..dad9b1b 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -1,17 +1,12 @@ /* * Public API Surface of error */ -// Class. export { + // Class. CommonError, Error, Errors, - EvalError, - InternalError, RangeError, - ReferenceError, - SyntaxError, TypeError, - URIError, ValidationError } from './lib'; From 5c5626c660b9d14b6767dcf4888d2860fc9c6724 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 17:57:46 +0100 Subject: [PATCH 102/179] test(Error): add. --- src/test/error.spec.ts | 178 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/test/error.spec.ts diff --git a/src/test/error.spec.ts b/src/test/error.spec.ts new file mode 100644 index 0000000..ef13e9b --- /dev/null +++ b/src/test/error.spec.ts @@ -0,0 +1,178 @@ +// External class. +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +import { typeOf } from '@angular-package/type'; +// Class. +import { Error } from '../lib/error.class'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] Error', () => { + let fix: string; + let id: string; + let problem: string; + let template: string; + let value: any; + let error: Error; + + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = '427'; + problem = 'The value must be a string type.'; + template = `Problem(VE{id}): {problem}\nFix: {fix}`; + value = Symbol(123); + + beforeEach(() => { + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = '427'; + problem = 'The value must be a string type.'; + template = `Problem(VE{id}): {problem}\nFix: {fix}`; + value = Symbol(123); + error = new Error(problem, fix, id, template); + }); + + testing + + /** + * Static properties. + */ + .describe(`Static properties`, () => { + testing.it(`Error.template`, () => { + expect(Error.template).toEqual(`Problem{id}: {problem} => Fix: {fix}`); + Error.template = `{problem} => Fix: {fix} of {id}`; + expect(Error.template).toEqual(`{problem} => Fix: {fix} of {id}`); + Error.template = `Problem{id}: {problem} => Fix: {fix}`; + }); + }) + + /** + * Instance accessors. + */ + .describe(`Instance accessors`, () => { + testing + + /** + * Error.prototype.fix + */ + .it(`Error.prototype.fix`, () => { + expect(error.fix).toEqual(fix); + toBe.string(fix); + }) + + /** + * Error.prototype.id + */ + .it(`Error.prototype.id`, () => { + expect(error.id).toEqual(id); + }) + + /** + * Error.prototype.name + */ + .it(`Error.prototype.name`, () => { + expect(error.name).toEqual('Error'); + }) + + /** + * Error.prototype.problem + */ + .it(`Error.prototype.problem`, () => { + expect(error.problem).toEqual(problem); + }) + + /** + * Error.prototype.template + */ + .it(`Error.prototype.template`, () => { + expect(error.template).toEqual(template); + }) + + /** + * [Symbol.toStringTag] + */ + .it(`[Symbol.toStringTag]`, () => { + expect(typeOf(error)).toEqual('error'); + expect(Object.prototype.toString.call(error)).toEqual('[object Error]'); + }); + }) + + /** + * Static methods. + */ + .describe(`Static methods`, () => { + testing + + /** + * Error.define() + */ + .it(`Error.define()`, () => { + const e = Error.define(problem, fix, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + }) + + /** + * Error.isError() + */ + .it(`Error.isError()`, () => { + expect(Error.isError(error)).toBeTrue(); + expect(Error.isError(error, id)).toBeTrue(); + }) + + /** + * Error.throw() + */ + .it(`Error.throw()`, () => { + try { + Error.throw(problem, fix, id); + } catch (e: any) { + expect(e.message).toEqual(`Problem${e.id}: ${e.problem} => Fix: ${e.fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + } + try { + Error.throw(problem, fix, id, template); + } catch (e: any) { + expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + } + }); + }) + + /** + * Constructor. + */ + .describe(`constructor()`, () => { + testing + .it(`without with id`, () => { + const e = new Error(problem, fix, id); + expect(e.message).toEqual(`Problem${id}: ${e.problem} => Fix: ${e.fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + }) + .it(`without with id with template`, () => { + const e = new Error(problem, fix, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + }) + .it(`without template`, () => { + const e = new Error(problem, fix); + expect(e.message).toEqual(`Problem: ${e.problem} => Fix: ${e.fix}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + }); + }); +}); From 7258e202d3143440a9240cc36ddbe1967a483c02 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 21:17:47 +0100 Subject: [PATCH 103/179] refactor(CommonError): remove `define()` and `throw()` static methods, add `range` parameter to the constructor and static `defineMessage()` method to handle `RangeError`. --- src/lib/common-error.class.ts | 67 +++++++++-------------------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index c76ac3e..cb95fcd 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -79,7 +79,7 @@ export abstract class CommonError extends Error { #template: string; //#endregion private instance properties. - //#region public static methods. + //#region protected static methods. /** * The static "tag" method builds from the given `values` the error message of a string type on the template. * @param templateStringsArray - @@ -87,45 +87,29 @@ export abstract class CommonError extends Error { * @returns The return value is the error message of a `string` type created from the expressions given in the `values`. * @angularpackage */ - public static defineMessage( + protected static defineMessage( templateStringsArray: TemplateStringsArray, ...values: any[] ): string { - let problem: string, fix: string, id: string, template: string; - [problem, fix, id, template] = values; + let problem: string, + fix: string, + range: { min?: number; max?: number }, + id: string, + template: string; + [problem, fix, range, id, template] = values; return template .replace('{problem}', problem) .replace('{fix}', fix) - .replace('{id}', id); + .replace('{id}', id) + .replace(`{max}`, range?.max ? String(range?.max) : '') + .replace(`{min}`, range?.min ? String(range?.min) : ''); } /** - * Defines the instance from the given `constructor` with the given required `problem`, `fix` and optional `id` and `template`. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. - * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @param constructor - * @returns The return value is a new instance from the given `constructor` with the given required `problem`, `fix` and optional `id` and - * `template`. - * @angularpackage - */ - protected static define( - problem: string, - fix: string, - id?: Id, - template = CommonError.template, - constructor?: any - ): any { - return new constructor(problem, fix, id, template); - } - - /** - * Checks whether the value of any type is an instance of `Error` of any or the given identification. - * @param value The value of any type to check against the `Error` instance. + * Checks whether the value of any type is a `this` instance of any or the given identification. + * @param value The value of any type to check against the `this` instance. * @param id Optional identification of generic type variable `Id` that the given `value` contains. - * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `Error` of any or the given `id`. + * @returns The return value is a `boolean` type indicating whether the given `value` is a `this` instance of any or the given `id`. */ protected static isError( value: any, @@ -137,25 +121,7 @@ export abstract class CommonError extends Error { ? value.id === id : true; } - - /** - * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. - * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @angularpackage - */ - protected static throw( - problem: string, - fix: string, - id?: string, - template?: string - ): void { - throw this.define(problem, fix, id, template, this); - } - //#endregion public static methods. + //#endregion protected static methods. //#region constructor. /** @@ -171,10 +137,11 @@ export abstract class CommonError extends Error { protected constructor( problem: string, fix: string, + range?: { min?: number; max?: number }, id: Id = '' as Id, template = CommonError.template ) { - super(CommonError.defineMessage`${problem}${fix}${id}${template}`); + super(CommonError.defineMessage`${problem}${fix}${range}${id}${template}`); this.#fix = fix; this.#id = id; this.#problem = problem; From b78c620efec41cb967ba31d53f0d53847feb659f Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 21:19:31 +0100 Subject: [PATCH 104/179] refactor(): remove static `throw()` method, change the `define()` static method to not use removed parent `define()` method, update `constructor` to include `range` parameter. --- src/lib/error.class.ts | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index b9c9ec9..ad6390b 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -40,7 +40,7 @@ export class Error extends CommonError { id?: Id, template = Error.template ): Error { - return super.define(problem, fix, id, template, this); + return new this(problem, fix, id, template); } /** @@ -56,24 +56,6 @@ export class Error extends CommonError { ): value is Error { return super.isError(value, id); } - - /** - * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. - * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @angularpackage - */ - public static throw( - problem: string, - fix: string, - id?: string, - template?: string - ): void { - super.throw(problem, fix, id, template); - } //#endregion public static methods. //#region constructor. @@ -93,7 +75,7 @@ export class Error extends CommonError { id?: Id, template = Error.template ) { - super(problem, fix, id, template); + super(problem, fix, undefined, id, template); } //#endregion constructor. } From cfcb0ea620fa989394c114b18ad9015e89e3a02f Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 21:20:32 +0100 Subject: [PATCH 105/179] test(Error): remove `throw()`. --- src/test/error.spec.ts | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/test/error.spec.ts b/src/test/error.spec.ts index ef13e9b..52d66b8 100644 --- a/src/test/error.spec.ts +++ b/src/test/error.spec.ts @@ -42,11 +42,12 @@ testing.describe('[counter] Error', () => { * Static properties. */ .describe(`Static properties`, () => { - testing.it(`Error.template`, () => { - expect(Error.template).toEqual(`Problem{id}: {problem} => Fix: {fix}`); - Error.template = `{problem} => Fix: {fix} of {id}`; - expect(Error.template).toEqual(`{problem} => Fix: {fix} of {id}`); - Error.template = `Problem{id}: {problem} => Fix: {fix}`; + testing + .it(`Error.template`, () => { + expect(Error.template).toEqual(`Problem{id}: {problem} => Fix: {fix}`); + Error.template = `{problem} => Fix: {fix} of {id}`; + expect(Error.template).toEqual(`{problem} => Fix: {fix} of {id}`); + Error.template = `Problem{id}: {problem} => Fix: {fix}`; }); }) @@ -124,28 +125,6 @@ testing.describe('[counter] Error', () => { .it(`Error.isError()`, () => { expect(Error.isError(error)).toBeTrue(); expect(Error.isError(error, id)).toBeTrue(); - }) - - /** - * Error.throw() - */ - .it(`Error.throw()`, () => { - try { - Error.throw(problem, fix, id); - } catch (e: any) { - expect(e.message).toEqual(`Problem${e.id}: ${e.problem} => Fix: ${e.fix}`); - expect(e.fix).toEqual(fix); - expect(e.id).toEqual(id); - expect(e.problem).toEqual(problem); - } - try { - Error.throw(problem, fix, id, template); - } catch (e: any) { - expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); - expect(e.fix).toEqual(fix); - expect(e.id).toEqual(id); - expect(e.problem).toEqual(problem); - } }); }) @@ -154,21 +133,21 @@ testing.describe('[counter] Error', () => { */ .describe(`constructor()`, () => { testing - .it(`without with id`, () => { + .it(`(problem, fix, id)`, () => { const e = new Error(problem, fix, id); expect(e.message).toEqual(`Problem${id}: ${e.problem} => Fix: ${e.fix}`); expect(e.fix).toEqual(fix); expect(e.id).toEqual(id); expect(e.problem).toEqual(problem); }) - .it(`without with id with template`, () => { + .it(`(problem, fix, id, template)`, () => { const e = new Error(problem, fix, id, template); expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); expect(e.fix).toEqual(fix); expect(e.id).toEqual(id); expect(e.problem).toEqual(problem); }) - .it(`without template`, () => { + .it(`(problem, fix)`, () => { const e = new Error(problem, fix); expect(e.message).toEqual(`Problem: ${e.problem} => Fix: ${e.fix}`); expect(e.fix).toEqual(fix); From 8aa82260c1bd4607c70edced1d11e68d3c39cb70 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 21:26:14 +0100 Subject: [PATCH 106/179] test(RangeError): add. --- src/test/range-error.spec.ts | 208 +++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 src/test/range-error.spec.ts diff --git a/src/test/range-error.spec.ts b/src/test/range-error.spec.ts new file mode 100644 index 0000000..450c7ec --- /dev/null +++ b/src/test/range-error.spec.ts @@ -0,0 +1,208 @@ +// External class. +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +import { typeOf } from '@angular-package/type'; +// Class. +import { RangeError } from '../lib/range-error.class'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] RangeError', () => { + let fix: string; + let id: string; + let min: number; + let max: number; + let problem: string; + let template: string; + let value: any; + let rangeError: RangeError; + + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = '427'; + min = 9; + max = 27; + problem = 'The value'; + template = `Problem(VE{id}): {problem} {min} and {max}.\nFix: {fix}.`; + value = Symbol(123); + + beforeEach(() => { + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = '427'; + min = 9; + max = 27; + problem = 'The value'; + template = `Problem(VE{id}): {problem} {min} and {max}.\nFix: {fix}.`; + value = Symbol(123); + rangeError = new RangeError(problem, fix, min, max, id, template); + }); + + testing + + /** + * Static properties. + */ + .describe(`Static properties`, () => { + testing.it(`RangeError.template`, () => { + expect(RangeError.template).toEqual(`Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`); + RangeError.template = `{problem} => Fix: {fix} of {id}`; + expect(RangeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); + RangeError.template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`; + }); + }) + + /** + * Instance accessors. + */ + .describe(`Instance accessors`, () => { + testing + + /** + * RangeError.prototype.fix + */ + .it(`RangeError.prototype.fix`, () => { + expect(rangeError.fix).toEqual(fix); + toBe.string(fix); + }) + + /** + * RangeError.prototype.id + */ + .it(`RangeError.prototype.id`, () => { + expect(rangeError.id).toEqual(id); + }) + + /** + * RangeError.prototype.max + */ + .it(`RangeError.prototype.max`, () => { + expect(rangeError.max).toEqual(max); + toBe.string(fix); + }) + + /** + * RangeError.prototype.min + */ + .it(`RangeError.prototype.min`, () => { + expect(rangeError.max).toEqual(max); + toBe.string(fix); + }) + + /** + * RangeError.prototype.name + */ + .it(`RangeError.prototype.name`, () => { + expect(rangeError.name).toEqual('RangeError'); + }) + + /** + * RangeError.prototype.problem + */ + .it(`RangeError.prototype.problem`, () => { + expect(rangeError.problem).toEqual(problem); + }) + + /** + * RangeError.prototype.range + */ + .it(`RangeError.prototype.range`, () => { + expect(rangeError.range).toEqual({ min, max }); + }) + + /** + * RangeError.prototype.template + */ + .it(`RangeError.prototype.template`, () => { + expect(rangeError.template).toEqual(template); + }) + + /** + * [Symbol.toStringTag] + */ + .it(`[Symbol.toStringTag]`, () => { + expect(typeOf(rangeError)).toEqual('rangeerror'); + expect(Object.prototype.toString.call(rangeError)).toEqual( + '[object RangeError]' + ); + }); + }) + + /** + * Static methods. + */ + .describe(`Static methods`, () => { + testing + + /** + * RangeError.define() + */ + .it(`RangeError.define()`, () => { + const e = RangeError.define(problem, fix, min, max, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${min} and ${max}.\nFix: ${fix}.`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + }) + + /** + * RangeError.isRangeError() + */ + .it(`RangeError.isRangeError()`, () => { + expect(RangeError.isRangeError(rangeError)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, id)).toBeTrue(); + }); + }) + + /** + * Constructor. + */ + .describe(`constructor()`, () => { + testing + .it(`(problem, fix, min, undefined, id)`, () => { + const e = new RangeError(problem, fix, min, undefined, id); + expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${''} => Fix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.min).toEqual(min); + expect(e.problem).toEqual(problem); + }) + .it(`(problem, fix, undefined, max, id)`, () => { + const e = new RangeError(problem, fix, undefined, max, id); + expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${''} and ${max} => Fix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.max).toEqual(max); + expect(e.problem).toEqual(problem); + }) + .it(`(problem, fix, min, max, id)`, () => { + const e = new RangeError(problem, fix, min, max, id); + expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${max} => Fix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.max).toEqual(max); + expect(e.min).toEqual(min); + expect(e.problem).toEqual(problem); + }) + .it(`(problem, fix, min, max, id, template)`, () => { + const e = new RangeError( + `The age`, + `Try to change minimum or maximum`, + min, + max, + id, + template + ); + expect(e.message).toEqual(`Problem(VE${id}): ${'The age'} ${min} and ${max}.\nFix: ${'Try to change minimum or maximum'}.`); + expect(e.fix).toEqual(`Try to change minimum or maximum`); + expect(e.max).toEqual(max); + expect(e.min).toEqual(min); + expect(e.problem).toEqual(`The age`); + expect(e.template).toEqual(template); + }); + }); +}); From 18d24d94a392e6cc4cf585f11cd16060129f83fb Mon Sep 17 00:00:00 2001 From: angularpackage Date: Fri, 4 Feb 2022 21:42:37 +0100 Subject: [PATCH 107/179] docs(CommonError): update template static property. --- src/lib/common-error.class.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index cb95fcd..b54283e 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -5,7 +5,7 @@ export abstract class CommonError extends Error { //#region public static properties. /** * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}` words. - * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. It can be set directly or by the `setTemplate()`. + * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. */ public static template = `Problem{id}: {problem} => Fix: {fix}`; //#endregion public static properties. From 1e8723e81656e4c26cba753eaa48041ff8186928 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 5 Feb 2022 00:41:14 +0100 Subject: [PATCH 108/179] test(RangeError): update. --- src/test/range-error.spec.ts | 85 ++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/src/test/range-error.spec.ts b/src/test/range-error.spec.ts index 450c7ec..bab8a27 100644 --- a/src/test/range-error.spec.ts +++ b/src/test/range-error.spec.ts @@ -65,61 +65,42 @@ testing.describe('[counter] RangeError', () => { /** * RangeError.prototype.fix */ - .it(`RangeError.prototype.fix`, () => { - expect(rangeError.fix).toEqual(fix); - toBe.string(fix); - }) + .it(`RangeError.prototype.fix`, () => expect(rangeError.fix).toEqual(fix)) /** * RangeError.prototype.id */ - .it(`RangeError.prototype.id`, () => { - expect(rangeError.id).toEqual(id); - }) + .it(`RangeError.prototype.id`, () => expect(rangeError.id).toEqual(id)) /** * RangeError.prototype.max */ - .it(`RangeError.prototype.max`, () => { - expect(rangeError.max).toEqual(max); - toBe.string(fix); - }) + .it(`RangeError.prototype.max`, () => expect(rangeError.max).toEqual(max)) /** * RangeError.prototype.min */ - .it(`RangeError.prototype.min`, () => { - expect(rangeError.max).toEqual(max); - toBe.string(fix); - }) + .it(`RangeError.prototype.min`, () => expect(rangeError.min).toEqual(min)) /** * RangeError.prototype.name */ - .it(`RangeError.prototype.name`, () => { - expect(rangeError.name).toEqual('RangeError'); - }) + .it(`RangeError.prototype.name`, () => expect(rangeError.name).toEqual('RangeError')) /** * RangeError.prototype.problem */ - .it(`RangeError.prototype.problem`, () => { - expect(rangeError.problem).toEqual(problem); - }) + .it(`RangeError.prototype.problem`, () => expect(rangeError.problem).toEqual(problem)) /** * RangeError.prototype.range */ - .it(`RangeError.prototype.range`, () => { - expect(rangeError.range).toEqual({ min, max }); - }) + .it(`RangeError.prototype.range`, () => expect(rangeError.range).toEqual({ min, max })) /** * RangeError.prototype.template */ - .it(`RangeError.prototype.template`, () => { - expect(rangeError.template).toEqual(template); - }) + .it(`RangeError.prototype.template`, () => expect(rangeError.template).toEqual(template)) /** * [Symbol.toStringTag] @@ -144,9 +125,14 @@ testing.describe('[counter] RangeError', () => { .it(`RangeError.define()`, () => { const e = RangeError.define(problem, fix, min, max, id, template); expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${min} and ${max}.\nFix: ${fix}.`); + // Required. expect(e.fix).toEqual(fix); - expect(e.id).toEqual(id); expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toEqual(id); + expect(e.max).toEqual(max); + expect(e.min).toEqual(min); + expect(e.template).toEqual(template); }) /** @@ -154,7 +140,13 @@ testing.describe('[counter] RangeError', () => { */ .it(`RangeError.isRangeError()`, () => { expect(RangeError.isRangeError(rangeError)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, id)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, undefined, undefined, id)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, min)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, undefined, max)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, min, max)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, min, undefined, id)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, undefined, max, id)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, min, max, id)).toBeTrue(); }); }) @@ -163,30 +155,54 @@ testing.describe('[counter] RangeError', () => { */ .describe(`constructor()`, () => { testing + .it(`(problem, fix)`, () => { + const e = new RangeError(problem, fix); + expect(e.message).toEqual(`Problem${''}: ${problem} must be between ${''} and ${''} => Fix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toBeUndefined(); + expect(e.max).toBeUndefined(); + expect(e.min).toBeUndefined(); + expect(e.template).toEqual(RangeError.template); + }) + .it(`(problem, fix, min, undefined, id)`, () => { const e = new RangeError(problem, fix, min, undefined, id); expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${''} => Fix: ${fix}`); + // Required. expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. expect(e.id).toEqual(id); + expect(e.max).toBeUndefined(); expect(e.min).toEqual(min); - expect(e.problem).toEqual(problem); + expect(e.template).toEqual(RangeError.template); }) .it(`(problem, fix, undefined, max, id)`, () => { const e = new RangeError(problem, fix, undefined, max, id); expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${''} and ${max} => Fix: ${fix}`); + // Required. expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. expect(e.id).toEqual(id); expect(e.max).toEqual(max); - expect(e.problem).toEqual(problem); + expect(e.min).toBeUndefined(); + expect(e.template).toEqual(RangeError.template); }) .it(`(problem, fix, min, max, id)`, () => { const e = new RangeError(problem, fix, min, max, id); expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${max} => Fix: ${fix}`); + // Required. expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. expect(e.id).toEqual(id); expect(e.max).toEqual(max); expect(e.min).toEqual(min); - expect(e.problem).toEqual(problem); + expect(e.template).toEqual(RangeError.template); }) .it(`(problem, fix, min, max, id, template)`, () => { const e = new RangeError( @@ -198,10 +214,13 @@ testing.describe('[counter] RangeError', () => { template ); expect(e.message).toEqual(`Problem(VE${id}): ${'The age'} ${min} and ${max}.\nFix: ${'Try to change minimum or maximum'}.`); + // Required. expect(e.fix).toEqual(`Try to change minimum or maximum`); + expect(e.problem).toEqual(`The age`); + // Optional. + expect(e.id).toEqual(id); expect(e.max).toEqual(max); expect(e.min).toEqual(min); - expect(e.problem).toEqual(`The age`); expect(e.template).toEqual(template); }); }); From 384b7f67f5133b5200a80122a4a74b2f4b3b7a20 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 5 Feb 2022 01:58:00 +0100 Subject: [PATCH 109/179] test(TypeError): add. --- src/test/type-error.spec.ts | 193 ++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 src/test/type-error.spec.ts diff --git a/src/test/type-error.spec.ts b/src/test/type-error.spec.ts new file mode 100644 index 0000000..b7d3841 --- /dev/null +++ b/src/test/type-error.spec.ts @@ -0,0 +1,193 @@ +// External class. +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +import { typeOf } from '@angular-package/type'; +// Class. +import { TypeError } from '../lib/type-error.class'; + +const et = new TypeError('problem', 'fix', 'string'); + +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] TypeError', () => { + let fix: string; + let id: string; + let problem: string; + let template: string; + let expectedType: string; + let typeError: TypeError; + + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = '427'; + problem = 'The value must be a string type.'; + template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; + expectedType = 'Symbol'; + + beforeEach(() => { + // Prepare the values. + fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + id = '427'; + problem = 'The value must be a string type.'; + template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; + expectedType = 'Symbol'; + typeError = new TypeError(problem, fix, expectedType, id, template); + }); + + testing + + /** + * Static properties. + */ + .describe(`Static properties`, () => { + testing + .it(`TypeError.template`, () => { + expect(TypeError.template).toEqual(`Problem{id}: {problem} must be of {type} => Fix: {fix}`); + TypeError.template = `{problem} => Fix: {fix} of {id}`; + expect(TypeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); + TypeError.template = `Problem{id}: {problem} must be of {type} => Fix: {fix}`; + }); + }) + + /** + * Instance accessors. + */ + .describe(`Instance accessors`, () => { + testing + + /** + * TypeError.prototype.expectedType + */ + .it(`TypeError.prototype.expectedType`, () => expect(typeError.expectedType).toEqual(expectedType)) + + /** + * TypeError.prototype.fix + */ + .it(`TypeError.prototype.fix`, () => expect(typeError.fix).toEqual(fix)) + + /** + * TypeError.prototype.id + */ + .it(`TypeError.prototype.id`, () => expect(typeError.id).toEqual(id)) + + /** + * TypeError.prototype.name + */ + .it(`TypeError.prototype.name`, () => expect(typeError.name).toEqual('TypeError')) + + /** + * TypeError.prototype.problem + */ + .it(`TypeError.prototype.problem`, () => expect(typeError.problem).toEqual(problem)) + + /** + * TypeError.prototype.template + */ + .it(`TypeError.prototype.template`, () => expect(typeError.template).toEqual(template)) + + /** + * [Symbol.toStringTag] + */ + .it(`[Symbol.toStringTag]`, () => { + expect(typeOf(typeError)).toEqual('typeerror'); + expect(Object.prototype.toString.call(typeError)).toEqual('[object TypeError]'); + }); + }) + + /** + * Static methods. + */ + .describe(`Static methods`, () => { + testing + + /** + * TypeError.define() + */ + .it(`TypeError.define()`, () => { + const e = TypeError.define(problem, fix, expectedType, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${expectedType}\nFix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.expectedType).toEqual(expectedType); + expect(e.id).toEqual(id); + expect(e.template).toEqual(template); + }) + + /** + * TypeError.isTypeError() + */ + .it(`TypeError.isTypeError()`, () => { + expect(TypeError.isTypeError(typeError)).toBeTrue(); + expect(TypeError.isTypeError(typeError, expectedType, id)).toBeTrue(); + }); + }) + + /** + * Constructor. + */ + .describe(`constructor()`, () => { + testing + .it(`(problem, fix)`, () => { + const e = new TypeError(problem, fix); + expect(e.message).toEqual(`Problem${''}: ${problem} must be of ${''} => Fix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toBeUndefined(); + expect(e.expectedType).toBeUndefined(); + expect(e.template).toEqual(TypeError.template); + }) + .it(`(problem, fix, type)`, () => { + const e = new TypeError(problem, fix, expectedType); + expect(e.message).toEqual(`Problem${''}: ${problem} must be of ${expectedType} => Fix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toBeUndefined(); + expect(e.expectedType).toEqual(expectedType); + expect(e.template).toEqual(TypeError.template); + }) + .it(`(problem, fix, undefined, id)`, () => { + const e = new TypeError(problem, fix, undefined, id); + expect(e.message).toEqual(`Problem${id}: ${problem} must be of ${''} => Fix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toEqual(id); + expect(e.expectedType).toBeUndefined(); + expect(e.template).toEqual(TypeError.template); + }) + .it(`(problem, fix, type, id)`, () => { + const e = new TypeError(problem, fix, expectedType, id); + expect(e.message).toEqual(`Problem${id}: ${problem} must be of ${expectedType} => Fix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toEqual(id); + expect(e.expectedType).toEqual(expectedType); + expect(e.template).toEqual(TypeError.template); + }) + .it(`(problem, fix, type, id, template)`, () => { + const e = new TypeError(problem, fix, expectedType, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${expectedType}\nFix: ${fix}`); + // Required. + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + // Optional. + expect(e.id).toEqual(id); + expect(e.expectedType).toEqual(expectedType); + expect(e.template).toEqual(template); + }); + }); +}); From 66a7c5de30d6e4658ae293b49d89a329603e08c4 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 5 Feb 2022 02:01:41 +0100 Subject: [PATCH 110/179] refactor(ValidationError): update to the new version. --- src/test/validation-error.spec.ts | 179 +++++++++--------------------- 1 file changed, 55 insertions(+), 124 deletions(-) diff --git a/src/test/validation-error.spec.ts b/src/test/validation-error.spec.ts index ba262ba..c8c8732 100644 --- a/src/test/validation-error.spec.ts +++ b/src/test/validation-error.spec.ts @@ -44,33 +44,25 @@ testing.describe('[counter] ValidationError', () => { .describe(`accessors`, () => { testing - /** - * ValidationError.prototype.fix - */ - .it(`ValidationError.prototype.fix`, () => { - expect(validationError.fix).toEqual(fix); - }) - - /** - * ValidationError.prototype.id - */ - .it(`ValidationError.prototype.id`, () => { - expect(validationError.id).toEqual(id); - }) - - /** - * ValidationError.prototype.problem - */ - .it(`ValidationError.prototype.problem`, () => { - expect(validationError.problem).toEqual(problem); - }) - - /** - * ValidationError.prototype.template - */ - .it(`ValidationError.prototype.template`, () => { - expect(validationError.template).toEqual(template); - }) + /** + * ValidationError.prototype.fix + */ + .it(`ValidationError.prototype.fix`, () => expect(validationError.fix).toEqual(fix)) + + /** + * ValidationError.prototype.id + */ + .it(`ValidationError.prototype.id`, () => expect(validationError.id).toEqual(id)) + + /** + * ValidationError.prototype.problem + */ + .it(`ValidationError.prototype.problem`, () => expect(validationError.problem).toEqual(problem)) + + /** + * ValidationError.prototype.template + */ + .it(`ValidationError.prototype.template`, () => expect(validationError.template).toEqual(template)); // .it(`[Symbol.toStringTag]`, () => {}); }) @@ -79,110 +71,49 @@ testing.describe('[counter] ValidationError', () => { */ .describe(`Static methods`, () => { testing - /** - * isValidationError() - */ - .it(`ValidationError.isValidationError()`, () => { - expect(validationError.id).toEqual(id); - }) - - /** - * defineMessage() - */ - .it(`ValidationError.defineMessage`, () => { - errorMessage = ValidationError.defineMessage`${'problem'}${'fix'}${'id'}${'{problem} {fix} {id}'}`; - expect(errorMessage).toEqual('problem fix id'); - }); + /** + * ValidationError.define() + */ + .it(`ValidationError.define()`, () => { + const e = ValidationError.define(problem, fix, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + }) + + /** + * ValidationError.isValidationError() + */ + .it(`ValidationError.isValidationError()`, () => { + expect(validationError.id).toEqual(id); + }); }) /** - * Instance methods. + * Constructor. */ - .describe(`Instance methods`, () => { + .describe(`constructor()`, () => { testing - /** - * ValidationError.prototype.setFix() - */ - .it(`ValidationError.prototype.setFix()`, () => { - validationError.setFix('my new fix'); - expect(validationError.fix).toEqual('my new fix'); - }) - - /** - * ValidationError.prototype.setId() - */ - .it(`ValidationError.prototype.setId()`, () => { - validationError.setId('my new id'); - expect(validationError.id).toEqual('my new id'); - }) - - // .it(`ValidationError.prototype.setMessage()`, () => { - // validationError.setMessage('my new id'); - // expect(validationError.id).toEqual('my new id'); - // }) - - /** - * ValidationError.prototype.setProblem() - */ - .it(`ValidationError.prototype.setProblem()`, () => { - validationError.setProblem('my new problem'); - expect(validationError.problem).toEqual('my new problem'); - }) - - /** - * ValidationError.prototype.setTemplate() - */ - .it(`ValidationError.prototype.setTemplate()`, () => { - validationError.setTemplate('my new template'); - expect(validationError.template).toEqual('my new template'); - }) - - /** - * ValidationError.prototype.throw() - */ - .it(`ValidationError.prototype.throw()`, () => { - try { - validationError.throw(); - } catch (e: any) { - toBe.instance(e, ValidationError); + .it(`(problem, fix, id)`, () => { + const e = new ValidationError(problem, fix, id); + expect(e.message).toEqual(`Problem${id}: ${e.problem} => Fix: ${e.fix}`); expect(e.fix).toEqual(fix); expect(e.id).toEqual(id); expect(e.problem).toEqual(problem); - expect(e.template).toEqual(template); - expect(e.message).toEqual(ValidationError.defineMessage`${problem}${fix}${id}${template}`); - } - try { - validationError - .setFix('new FIX') - .setId('new ID') - .setProblem('new PROBLEM') - .setTemplate('{problem}{fix}{id}'); - validationError.throw(); - } catch (e: any) { - toBe.instance(e, ValidationError); - expect(e.fix).toEqual('new FIX'); - expect(e.id).toEqual('new ID'); - expect(e.problem).toEqual('new PROBLEM'); - expect(e.template).toEqual('{problem}{fix}{id}'); - expect(e.message).toEqual(ValidationError.defineMessage`${'new PROBLEM'}${'new FIX'}${'new ID'}${'{problem}{fix}{id}'}`); - } - }) - - /** - * ValidationError.prototype.updateMessage() - */ - .it(`ValidationError.prototype.updateMessage()`, () => { - validationError - .setFix('new FIX') - .setId('new ID') - .setProblem('new PROBLEM') - .setTemplate('{problem}{fix}{id}') - .updateMessage(); - expect(validationError.fix).toEqual('new FIX'); - expect(validationError.id).toEqual('new ID'); - expect(validationError.problem).toEqual('new PROBLEM'); - expect(validationError.template).toEqual('{problem}{fix}{id}'); - expect(validationError.message).toEqual(ValidationError.defineMessage`${'new PROBLEM'}${'new FIX'}${'new ID'}${'{problem}{fix}{id}'}`); - }); + }) + .it(`(problem, fix, id, template)`, () => { + const e = new ValidationError(problem, fix, id, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem}\nFix: ${fix}`); + expect(e.fix).toEqual(fix); + expect(e.id).toEqual(id); + expect(e.problem).toEqual(problem); + }) + .it(`(problem, fix)`, () => { + const e = new ValidationError(problem, fix); + expect(e.message).toEqual(`Problem: ${e.problem} => Fix: ${e.fix}`); + expect(e.fix).toEqual(fix); + expect(e.problem).toEqual(problem); + }); }); }); From 5841c857ee366cecdfa6e0fee62a0ffee72c59b9 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 5 Feb 2022 02:02:07 +0100 Subject: [PATCH 111/179] chore(tsconfig): remove unnecessary. --- tsconfig.spec.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/tsconfig.spec.json b/tsconfig.spec.json index bc7fbe9..715dd0a 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -3,9 +3,6 @@ "extends": "../../tsconfig.json", "compilerOptions": { "outDir": "../../out-tsc/spec", - "paths": { - "@angular/*": [ "./node_modules/@angular/*" ] - }, "types": [ "jasmine" ] From 1cf62c2336da9925ef6ed3d990e70e05314ba009 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 5 Feb 2022 02:02:28 +0100 Subject: [PATCH 112/179] chore(package): update `homepage`. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e1a859d..ed0b60b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "3.0.0", "description": "Manages an error.", "author": "Angular Package (https://angular-package.dev)", - "homepage": "https://github.com/angular-package/error#readme", + "homepage": "https://error.angular-package.dev", "dependencies": { "tslib": "^2.3.0" }, From 748eab6730fe6875427849e4865a0656e1ee4e22 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 6 Feb 2022 19:03:33 +0100 Subject: [PATCH 113/179] refactor(CommonError): general update. Changed: - `get` accessor id return type to `Id | undefined`; - `#id` property to be optional; - static `defineMessage()` "tag" method by adding default template if not set, by adding `type` property to the `additional`; - static `isError()` method to check properly; - `constructor()` parameter `range` to `additional` extending its properties by `type`; - `constructor()` order parameters to have `additional` as last; --- src/lib/common-error.class.ts | 81 +++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index b54283e..7d3210f 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -4,15 +4,15 @@ export abstract class CommonError extends Error { //#region public static properties. /** - * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}` words. - * By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. + * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}`, + * `{type}` tags. By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. */ public static template = `Problem{id}: {problem} => Fix: {fix}`; //#endregion public static properties. //#region public instance accessors. /** - * The get `accessor` obtains a possible solution to the described problem by returning the `#fix` property of a specified object. + * The `get` accessor obtains a possible solution to the described problem by returning the `#fix` property of a specified object. * @returns The return value is the fix of a `string` type. * @angularpackage */ @@ -22,15 +22,15 @@ export abstract class CommonError extends Error { /** * The `get` accessor gets the error identification by returning the `#id` property of a specified object. - * @returns The return value is the error identification of the generic type variable `Id`. + * @returns The return value is the error identification of the generic type variable `Id` or `undefined`. * @angularpackage */ - public get id(): Id { + public get id(): Id | undefined { return this.#id; } /** - * The `get` accessor gets the error message by returning the parent `message` property of a specified object. + * The `get` accessor gets the error message by returning the parent `message` property of the `Error` object. * @returns The return value is the error message of a `string` type. * @angularpackage */ @@ -66,7 +66,7 @@ export abstract class CommonError extends Error { /** * A privately stored unique identification to the described problem of generic type variable `Id`. */ - #id: Id; + #id?: Id; /** * A privately stored problem of a `string` type. @@ -74,7 +74,8 @@ export abstract class CommonError extends Error { #problem: string; /** - * A string-type privately stored template of the error message that contains replaceable `{fix}`, `{problem}` and optional `{id}` words. + * A string-type privately stored template of the error message that contains replaceable required `{fix}`, `{problem}` and optional + * `{id}`, `{max}`, `{min}`, `{type}` tags. */ #template: string; //#endregion private instance properties. @@ -83,7 +84,7 @@ export abstract class CommonError extends Error { /** * The static "tag" method builds from the given `values` the error message of a string type on the template. * @param templateStringsArray - - * @param values A rest parameter of expressions in order the `problem`, `fix`, `id` and `template`. + * @param values A rest parameter of expressions in order the `problem`, `fix`, `id`, `template` and `additional`. * @returns The return value is the error message of a `string` type created from the expressions given in the `values`. * @angularpackage */ @@ -93,55 +94,61 @@ export abstract class CommonError extends Error { ): string { let problem: string, fix: string, - range: { min?: number; max?: number }, - id: string, - template: string; - [problem, fix, range, id, template] = values; - return template - .replace('{problem}', problem) - .replace('{fix}', fix) - .replace('{id}', id) - .replace(`{max}`, range?.max ? String(range?.max) : '') - .replace(`{min}`, range?.min ? String(range?.min) : ''); + id: string | undefined, + template: string, + additional: { min?: number; max?: number; type?: string; }; + [problem, fix, id, template, additional] = values; + template = (template || CommonError.template) + .replace('{fix}', fix || '') + .replace(/{id}/g, id || '') + .replace('{problem}', problem || '') + .replace(/{max}/g, additional?.max ? String(additional?.max) : '') + .replace(/{min}/g, additional?.min ? String(additional?.min) : '') + .replace(/{type}/g, additional?.type ? additional?.type : ''); + return template; } /** * Checks whether the value of any type is a `this` instance of any or the given identification. * @param value The value of any type to check against the `this` instance. - * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @param id Optional identification of generic type variable `Id` to check whether the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is a `this` instance of any or the given `id`. + * @angularpackage */ - protected static isError( + protected static isError( value: any, id?: Id ): value is CommonError { - return typeof value === 'object' && - value instanceof this && - typeof id === 'string' - ? value.id === id - : true; + return typeof value === 'object' && value instanceof this + ? typeof id === 'string' + ? value.id === id + : true + : false; } //#endregion protected static methods. //#region constructor. /** - * Creates an instance that represents an error with the described problem and its solution, optionally marked with an explicit - * identification. - * @param problem Description of the problem of a string type. - * @param fix A solution to the given `problem` of a string type. + * Creates an instance that represents an error with the described problem and its solution, optionally with expected type, range, an + * explicit identification, and an error message template. + * @param problem Description of the problem of a `string` type. + * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is equal to the static property `template`. + * @param additional Optional object consists of optional `max`, `min`, and `type` properties to define the error message. * @angularpackage */ - protected constructor( + constructor( problem: string, fix: string, - range?: { min?: number; max?: number }, - id: Id = '' as Id, - template = CommonError.template + id?: Id, + template = CommonError.template, + additional?: { max?: number; min?: number; type?: string } ) { - super(CommonError.defineMessage`${problem}${fix}${range}${id}${template}`); + super( + CommonError.defineMessage`${problem}${fix}${id}${template}${additional}` + ); this.#fix = fix; this.#id = id; this.#problem = problem; From 98f0b1e2f88a81535e2f7eed2869e7e39e02e18a Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 6 Feb 2022 19:04:30 +0100 Subject: [PATCH 114/179] refactor(Error): update `constructor()` to the new version of `CommonError`. --- src/lib/error.class.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index ad6390b..8525bf1 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -6,6 +6,7 @@ export class Error extends CommonError { //#region public instance accessors. /** * Error name of a `string` type, set to `Error` that is being thrown. + * @returns The return value is the error instance name of string type. * @angularpackage */ public get name(): string { @@ -29,7 +30,7 @@ export class Error extends CommonError { * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` tags. By default, the value * is picked from the static property `template`. * @returns The return value is a new instance of the `Error` with the given required `problem`, `fix` and optional `id` and `template`. * @angularpackage @@ -65,8 +66,8 @@ export class Error extends CommonError { * @param problem Description of the problem of a string type. * @param fix A solution to the given `problem` of a string type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is equal to the static property `template`. * @angularpackage */ constructor( @@ -75,7 +76,7 @@ export class Error extends CommonError { id?: Id, template = Error.template ) { - super(problem, fix, undefined, id, template); + super(problem, fix, id, template); } //#endregion constructor. } From 4d40a4d80b71fcaac05a63d8d76d181f50baf485 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 6 Feb 2022 22:32:26 +0100 Subject: [PATCH 115/179] docs(README.md): update. --- README.md | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 244f126..283909d 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,12 @@ Manages an [`Error`][js-error].
+## Documentation + +For the detailed documentation go to [**https://error.angular-package.dev/**](https://error.angular-package.dev/) + +
+ ## Table of contents * [Skeleton](#skeleton) @@ -108,31 +114,6 @@ npm i @angular-package/error --save
-## Api - -```typescript -import { - // Class. - ValidationError, - // Interface. - ErrorMessage, -} from '@angular-package/error'; -``` - -```typescript -/* - * Experimental. - */ -import { - // Class. - MessageBuilder, - MessageBuilderTemplate, - MessageFunctionBuilder, -} from '@angular-package/error'; -``` - -
- ## Changelog The **changelog** of this package is based on [*keep a changelog*](https://keepachangelog.com/en/1.0.0/). To read it, click on the [CHANGELOG.md](https://github.com/angular-package/error/blob/main/CHANGELOG.md) link. From 5f5630232bfb6c57a7335306f8a75564ebeb88f1 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 6 Feb 2022 22:56:30 +0100 Subject: [PATCH 116/179] test(CommonError): add. --- src/test/common-error.spec.ts | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/test/common-error.spec.ts diff --git a/src/test/common-error.spec.ts b/src/test/common-error.spec.ts new file mode 100644 index 0000000..c97a2c1 --- /dev/null +++ b/src/test/common-error.spec.ts @@ -0,0 +1,79 @@ +// External class. +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +// Class. +import { CommonError } from '../lib/common-error.class'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] CommonError', () => { + class TestError extends CommonError { + public static isError( + value: any, + id?: Id + ): value is CommonError { + return super.isError(value, id); + } + + public static defineMessage( + templateStringsArray: TemplateStringsArray, + ...values: any[] + ): string { + return super.defineMessage(templateStringsArray, ...values); + } + } + + + const problem = 'The value must be string type.'; + const fix = 'Provide the string type instead of number.'; + const id = 'AE: 427'; + const template = `{problem} {fix} {id} {max} {min} {type}`; + const additional = { max: 427, min: 9, type: 'string' }; + const testError = new TestError(problem, fix, id, template, additional); + + testing + .describe(`Accessors`, () => { + testing + .it(`TestError.prototype.fix`, () => expect(testError.fix).toEqual(fix)) + .it(`TestError.prototype.id`, () => expect(testError.id).toEqual(id)) + .it(`TestError.prototype.message`, () => + expect(testError.message).toEqual( + `${problem} ${fix} ${id} ${additional.max} ${additional.min} ${additional.type}` + ) + ) + .it(`TestError.prototype.problem`, () => + expect(testError.problem).toEqual(problem) + ) + .it(`TestError.prototype.template`, () => + expect(testError.template).toEqual(template) + ); + }).describe(`Properties`, () => { + testing.it(`TestError.template`, () => expect(TestError.template).toEqual(`Problem{id}: {problem} => Fix: {fix}`)); + }).describe(`Methods`, () => { + testing + .it('TestError.defineMessage`${problem}${fix}${id}${template}${additional}`', + () => expect(TestError.defineMessage`${problem}${fix}${id}${template}${additional}`).toEqual(`${problem} ${fix} ${id} ${additional.max} ${additional.min} ${additional.type}`)) + .it('TestError.defineMessage`${problem}${fix}${id}${template}${additional}`', + () => expect(TestError.defineMessage`${problem}${fix}${id}${template}${{ min: 1 }}`).toEqual(`${problem} ${fix} ${id} ${''} ${1} ${''}`)) + .it('TestError.defineMessage`${problem}${fix}${id}${template}`', + () => expect(TestError.defineMessage`${problem}${fix}${id}${template}`).toEqual(`${problem} ${fix} ${id} `)) + .it('TestError.defineMessage`${problem}${fix}`', + () => expect(TestError.defineMessage`${problem}${fix}`).toEqual(`Problem${''}: ${problem} => Fix: ${fix}`)) + .it('TestError.defineMessage`${problem}`', + () => expect(TestError.defineMessage`${problem}`).toEqual(`Problem${''}: ${problem} => Fix: ${''}`)) + .it('TestError.defineMessage`${problem}`', + () => expect(TestError.defineMessage`${problem}`).toEqual(`Problem${''}: ${problem} => Fix: ${''}`)) + .it('TestError.defineMessage``', + () => expect(TestError.defineMessage``).toEqual(`Problem${''}: ${''} => Fix: ${''}`)) + + .it(`TestError.isError()`, () => { + expect(TestError.isError(testError)).toBeTrue(); + expect(TestError.isError(testError, id)).toBeTrue(); + expect(TestError.isError(null, id)).toBeFalse(); + }); + }); +}); From 61894b78d175ea9bf735f3378a8c8fa49b81f9ff Mon Sep 17 00:00:00 2001 From: angularpackage Date: Mon, 7 Feb 2022 16:07:28 +0100 Subject: [PATCH 117/179] refactor(Error): remove default value of the generic type variable `Id` in the `define()` static method. Update jsdoc. --- src/lib/error.class.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index 8525bf1..7fe34e0 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -1,12 +1,13 @@ import { CommonError } from './common-error.class'; /** + * The `Error` class is an extension of the` CommonError` class * The `Error` object is thrown when a runtime error occurs, but with additional identification and a solution to the described problem. */ export class Error extends CommonError { //#region public instance accessors. /** * Error name of a `string` type, set to `Error` that is being thrown. - * @returns The return value is the error instance name of string type. + * @returns The return value is the error instance name of `string` type. * @angularpackage */ public get name(): string { @@ -16,7 +17,7 @@ export class Error extends CommonError { /** * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'Error'` for an instance of `Error`. It can be read * by the `typeOf()` function of `@angular-package/type`. - * @returns The return value is the word 'Error` of a `string`. + * @returns The return value is the word `Error` of a `string`. * @angularpackage */ public get [Symbol.toStringTag](): string { @@ -26,16 +27,17 @@ export class Error extends CommonError { //#region public static methods. /** - * Defines the `Error` instance with the given required `problem`, `fix` and optional `id` and `template`. + * Defines the `Error` instance with the message built of the given required `problem`, `fix` and optional `id` on the `template`. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` tags. By default, the value - * is picked from the static property `template`. - * @returns The return value is a new instance of the `Error` with the given required `problem`, `fix` and optional `id` and `template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is picked from the static property `template`. + * @returns The return value is a new instance of the `Error` with the message built from the given required `problem`, `fix` and optional + * `id` on the `template`. * @angularpackage */ - public static define( + public static define( problem: string, fix: string, id?: Id, @@ -61,8 +63,8 @@ export class Error extends CommonError { //#region constructor. /** - * Creates the `Error` instance that represents an error with the described problem and its solution, optionally marked with an explicit - * identification. + * Creates the `Error` instance that represents an error with the message built of the described `problem` and its solution, optionally + * marked with an explicit identification on the given or stored `template`. * @param problem Description of the problem of a string type. * @param fix A solution to the given `problem` of a string type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. From 428b73bd12da854b875f1df0e4b3964c701d61e8 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Mon, 7 Feb 2022 16:18:29 +0100 Subject: [PATCH 118/179] docs(Error): fix main description. --- src/lib/error.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index 7fe34e0..5461a57 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -1,7 +1,7 @@ import { CommonError } from './common-error.class'; /** - * The `Error` class is an extension of the` CommonError` class - * The `Error` object is thrown when a runtime error occurs, but with additional identification and a solution to the described problem. + * The `Error` object is an extension of the `CommonError` class and is thrown when a runtime error occurs with a message built from a + * solution to the described problem but with additional identification, on the template. */ export class Error extends CommonError { //#region public instance accessors. From d3a1c98f12f8d53a5ca4e63802477c0054cc7ade Mon Sep 17 00:00:00 2001 From: angularpackage Date: Mon, 7 Feb 2022 17:05:25 +0100 Subject: [PATCH 119/179] docs(CommonError): update constructor and main description. --- src/lib/common-error.class.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index 7d3210f..c12d95c 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -1,5 +1,6 @@ /** - * The `CommonError` abstract object to throw an identified error with a solution to the described problem. + * The `CommonError` abstract object to throw an identified error with a solution to the described problem, additional type, and range built + * on the template. */ export abstract class CommonError extends Error { //#region public static properties. @@ -129,8 +130,8 @@ export abstract class CommonError extends Error { //#region constructor. /** - * Creates an instance that represents an error with the described problem and its solution, optionally with expected type, range, an - * explicit identification, and an error message template. + * Creates an error instance with the message built from the given problem, its solution, optional type, range, an explicit identification + * on the error message template. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. From 8e2b0982488041b6e53d0d31c698e9eddb9b2783 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 8 Feb 2022 16:51:21 +0100 Subject: [PATCH 120/179] docs(CommonError): update `constructor()` main description and `additional` parameter. --- src/lib/common-error.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index c12d95c..ce2a44e 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -131,13 +131,13 @@ export abstract class CommonError extends Error { //#region constructor. /** * Creates an error instance with the message built from the given problem, its solution, optional type, range, an explicit identification - * on the error message template. + * on the supplied or stored template. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and * `{type}` tags. By default, the value is equal to the static property `template`. - * @param additional Optional object consists of optional `max`, `min`, and `type` properties to define the error message. + * @param additional An optional object consists of optional `min`, `max`, and `type` properties to define the error message. * @angularpackage */ constructor( From 579c485019fc9befd9a0d7e9250c50dbfd3e6a94 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 8 Feb 2022 16:53:08 +0100 Subject: [PATCH 121/179] refactor(RangeError): add static `template` property with the default value, add `max`, `min` and `range` get accessors, add `min` and `max` parameters to static `define()`, `isRange()` methods and `constructor()`, remove static `throw()` method. Update jsdoc. --- src/lib/range-error.class.ts | 132 +++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 38 deletions(-) diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts index 2bef225..fe72f28 100644 --- a/src/lib/range-error.class.ts +++ b/src/lib/range-error.class.ts @@ -1,18 +1,59 @@ import { CommonError } from './common-error.class'; /** - * The `RangeError` object is thrown when a value is not in the set or range of allowed values, along with additional identification and - * a solution to the described problem. + * The `RangeError` object is an extension of the `CommonError` class and is thrown when a value is not in the set or range of allowed + * values with the message built from the described problem and its solution, optional explicit identification on the given or stored + * template. */ export class RangeError extends CommonError { + /** + * A template of the error message of `string` type with the replaceable required `{problem}`, `{fix}` and optional `{id}`, `{max}`, + * `{min}`, `{type}` tags. By default, it's set to `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`. + */ + public static template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`; + //#region public instance accessors. /** - * Error name of a `string` type, set to `RangeError` that is being thrown. + * The `get` accessor obtains the maximum range of `number` type that causes an error to be thrown(or not thrown), if set, otherwise + * returns `undefined`. + * @returns The return value is the maximum range of a `number` type or `undefined`. + * @angularpackage + */ + public get max(): number | undefined { + return this.#max; + } + + /** + * The `get` accessor obtains the minimum range of `number` type that causes an error to be thrown(or not thrown), if set, otherwise + * returns `undefined`. + * @returns The return value is the minimum range of a `number` type or undefined. + * @angularpackage + */ + public get min(): number | undefined { + return this.#min; + } + + /** + * The `get` accessor obtains error name of a `string` type, set to `RangeError` that is being thrown. + * @returns The return value is the error instance name of `string` type. * @angularpackage */ public get name(): string { return 'RangeError'; } + /** + * The `get` accessor obtains the minimum and maximum range in the form of an `object`. + * @returns The return value is an `object` that consists of the minimum range under the `min` property and the maximum under the + * `max` property. + * @angularpackage + */ + public get range(): { min?: number; max?: number } { + return { + min: this.#min, + max: this.#max, + }; + } + /** * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'RangeError'` for an instance of * `RangeError`. It can be read by the `typeOf()` function of `@angular-package/type`. @@ -24,79 +65,94 @@ export class RangeError extends CommonError { } //#endregion public instance accessors. + //#region private instance properties. + /** + * Private property of the maximum range of a `number` type that causes an error to be thrown(or not thrown). + */ + #max?: number; + + /** + * Private property of the minimum range of a `number` type that causes an error to be thrown(or not thrown). + */ + #min?: number; + //#endregion private instance properties. + //#region public static methods. /** - * Defines the `RangeError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * Defines the `RangeError` instance with the message built from the given required `problem`, `fix` and optional `id`, `max`, `min` on + * the given or stored `template`. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @returns The return value is a new instance of the `RangeError` with the given required `problem`, `fix` and optional `id` and - * `template`. + * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown). + * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown). + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` tags. By default, the value + * is picked from the static property `RangeError.template`. + * @returns The return value is a new instance of the `RangeError` with the message built from the given required `problem`, `fix` and + * optional `id`, `min`, `max` on the given or stored `template`. * @angularpackage */ - public static define( + public static define( problem: string, fix: string, id?: Id, + min?: number, + max?: number, template = RangeError.template ): RangeError { - return super.define(problem, fix, id, template, this); + return new this(problem, fix, id, min, max, template); } /** - * Checks whether the value of any type is an instance of `RangeError` of any or the given identification. + * Checks whether the value of any type is an instance of `RangeError` of any or the given minimum/maximum range and identification. * @param value The value of any type to check against the `RangeError` instance. - * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @param id Optional identification of generic type variable `Id` to check whether the given `value` contains. + * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown) to check whether the given + * `value` contains. + * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown) to check whether the given + * `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `RangeError` of any or the given - * `id`. + * `min`, max` and `id` properties. * @angularpackage */ public static isRangeError( value: any, - id?: Id + id?: Id, + min?: number, + max?: number ): value is RangeError { - return super.isError(value, id); - } - - /** - * Throws the `RangeError` with the given required `problem`, `fix` and optional `id` and `template`. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. - * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @angularpackage - */ - public static throw( - problem: string, - fix: string, - id?: string, - template?: string - ): void { - super.throw(problem, fix, id, template); + return ( + super.isError(value, id) && + (typeof min === 'number' ? (value as any).min === min : true) && + (typeof max === 'number' ? (value as any).max === max : true) + ); } //#endregion public static methods. //#region constructor. /** - * Creates a `RangeError` instance that represents range error with the described problem and its solution, optionally marked with an - * explicit identification. + * Creates the `RangeError` instance that represents range error with the message built of the given described `problem` and its solution, + * optional `min`/`max` range, and an explicit identification on the given or stored error message template. * @param problem Description of the range problem of a `string` type. * @param fix A solution to the given range issue of a `string` type. + * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown). + * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown). * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the - * value is picked from the static property `template`. + * @param template A template of error message with the replaceable required `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` + * tags. By default, the value is equal to the static property `template`. * @angularpackage */ constructor( problem: string, fix: string, id?: Id, + min?: number, + max?: number, template = RangeError.template ) { - super(problem, fix, id, template); + super(problem, fix, id, template, { min, max }); + this.#max = max; + this.#min = min; } //#endregion constructor. } From ca0d950e0cee0cf9d0f574ee05739fa612bf421e Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 8 Feb 2022 16:55:49 +0100 Subject: [PATCH 122/179] docs(Error): update `isError()` parameter id, and constructor main. --- src/lib/error.class.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index 5461a57..5d36509 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -49,7 +49,7 @@ export class Error extends CommonError { /** * Checks whether the value of any type is an instance of `Error` of any or the given identification. * @param value The value of any type to check against the `Error` instance. - * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @param id Optional unique identification of generic type variable `Id` that the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `Error` of any or the given `id`. * @angularpackage */ @@ -63,8 +63,8 @@ export class Error extends CommonError { //#region constructor. /** - * Creates the `Error` instance that represents an error with the message built of the described `problem` and its solution, optionally - * marked with an explicit identification on the given or stored `template`. + * Creates the `Error` instance with the message built from the given described `problem` and its solution, optional explicit + * identification on the given or stored `template`. * @param problem Description of the problem of a string type. * @param fix A solution to the given `problem` of a string type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. From 3b579632ced4dbf6de02e9b0fab634e66fe3bb7a Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 8 Feb 2022 21:35:15 +0100 Subject: [PATCH 123/179] refactor(TypeError): add static `template` property, add `get` accessor `type`, add property `#type`, change the static `define()` and `isTypeError()` method, add optional `type` parameter to the `constructor`. Update jsdoc. --- src/lib/type-error.class.ts | 93 ++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/src/lib/type-error.class.ts b/src/lib/type-error.class.ts index 2299ed6..aafb1b3 100644 --- a/src/lib/type-error.class.ts +++ b/src/lib/type-error.class.ts @@ -1,17 +1,36 @@ import { CommonError } from './common-error.class'; /** - * The `TypeError` object is thrown when an operation could not be performed, typically(but not exclusively) when a value is not of the - * expected type, along with additional identification and a solution to the described problem. + * The `TypeError` object is an extension of the `CommonError` class and is thrown when an operation could not be performed, + * typically(but not exclusively) when a value is not of the expected type, with the message built from the described problem and its + * solution, optional an explicit identification and type, on the given or stored template. */ export class TypeError extends CommonError { + /** + * A template of the error message of `string` type with the replaceable required `{fix}`,`{problem}` and optional `{id}`, `{max}`, + * `{min}`, `{type}` tags. By default, it's set to `Problem{id}: {problem} => Fix: {fix} must be of the {type}`. + */ + public static template = `Problem{id}: {problem} => Fix: {fix} must be of the {type}`; + //#region public instance accessors. /** - * Error name of a `string` type that is being thrown. By default, it's `TypeError`. + * The `get` accessor obtains error name of a `string` type, set to 'TypeError' that is being thrown. + * @returns The return value is the error instance name of `string` type. + * @angularpackage */ public get name(): string { return 'TypeError'; } + /** + * The `get` accessor obtains the type of a `string` that causes an error to be thrown(or not thrown) if set, otherwise returns + * `undefined`. + * @returns The return value is the type of a `string` type. + * @angularpackage + */ + public get type(): string | undefined { + return this.#type; + } + /** * The `get` accessor, with the help of `toStringTag`, changes the default tag to `'TypeError'` for an instance of * `TypeError`. It can be read by the `typeOf()` function of `@angular-package/type`. @@ -23,78 +42,76 @@ export class TypeError extends CommonError { } //#endregion public instance accessors. + /** + * Private string-type property of the type that causes an error to be thrown(or not thrown). + */ + #type?: string; + //#region public static methods. /** - * Defines the `TypeError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * Defines the `TypeError` instance with the given required `problem`, `fix` and optional `id`, `type` and `template`. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @returns The return value is a new instance of the `TypeError` with the given required `problem`, `fix` and optional `id` and - * `template`. + * @param type The optional type that causes an error to be thrown(or not thrown). + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{min}`, `{max}` and + * `{type}` tags. By default, the value is picked from the static property `TypeError.template`. + * @returns The return value is a new instance of the `TypeError` with the message built from the given required `problem`, `fix` and + * optional `id`, `type` on the given or stored `template`. * @angularpackage */ - public static define( + public static define( problem: string, fix: string, id?: Id, + type?: string, template = TypeError.template ): TypeError { - return super.define(problem, fix, id, template, this); + return new this(problem, fix, id, type, template); } /** - * Checks whether the value of any type is an instance of `TypeError` of any or the given identification. + * Checks whether the value of any type is an instance of `TypeError` of any or the given type and identification. * @param value The value of any type to check against the `TypeError` instance. - * @param id Optional identification of generic type variable `Id` that the given `value` contains. + * @param id Optional unique identification of generic type variable `Id` to check whether the given `value` contains. + * @param type The optional type that causes an error to be thrown(or not thrown) to check whether the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `TypeError` of any or the given - * `id`. + * `type` and `id` properties. + * @angularpackage */ public static isTypeError( value: any, - id?: Id + id?: Id, + type?: string ): value is TypeError { - return super.isError(value, id); - } - - /** - * Throws the `TypeError` with the given required `problem`, `fix` and optional `id` and `template`. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. - * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @angularpackage - */ - public static throw( - problem: string, - fix: string, - id?: string, - template?: string - ): void { - super.throw(problem, fix, id, template); + return ( + super.isError(value, id) && + (typeof type === 'string' ? (value as any).type === type : true) + ); } //#endregion public static methods. //#region constructor. /** - * Creates a `TypeError` instance that represents type error with the described problem and its solution, optionally marked with an - * explicit identification. + * Creates a `TypeError` instance that represents type error with the message built of the given described problem and its solution, + * optional type, and an explicit identification on the supplied or stored error message template. * @param problem Description of the range problem of a `string` type. * @param fix A solution to the given range issue of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the - * value is picked from the static property `template`. + * @param type The optional type that causes an error to be thrown(or not thrown). + * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is picked from the static property `TypeError.template`. * @angularpackage */ constructor( problem: string, fix: string, id?: Id, + type?: string, template = TypeError.template ) { - super(problem, fix, id, template); + super(problem, fix, id, template, { type }); + this.#type = type; } //#endregion constructor. } From dad8252ff8b19a6f9e6e2f22d01850d62c9c451d Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 9 Feb 2022 00:41:28 +0100 Subject: [PATCH 124/179] refactor(ValidationError): remove static `throw()` method, change the static `define()` method to not use `super.define()`. Update jsdoc. --- src/lib/validation-error.class.ts | 57 +++++++++++++------------------ 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 3b9c329..d336178 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -1,12 +1,14 @@ import { CommonError } from './common-error.class'; /** - * The `ValidationError` object is thrown when an operation could not be performed despite proper type(but not exclusively), along with - * additional identification and a solution to the described problem. + * The `ValidationError` object is an extension of the `CommonError` class and is thrown when an operation could not be performed despite + * proper type(but not exclusively) with the message built from the described problem and its solution, along with additional identification + * on the given or stored template. */ export class ValidationError extends CommonError { //#region public instance accessors. /** - * Error name of a `string` type, set to `ValidationError` that is being thrown. + * The `get` accessor obtains error name of a `string` type, set to 'ValidationError' that is being thrown. + * @returns The return value is the error instance name of `string` type. * @angularpackage */ public get name(): string { @@ -26,29 +28,33 @@ export class ValidationError extends CommonError { //#region public static methods. /** - * Defines the `ValidationError` instance with the given required `problem`, `fix` and optional `id` and `template`. + * Defines the `ValidationError` instance with the message built of the given required `problem`, `fix`, and optional `id` on the supplied + * or stored `template`. * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @returns The return value is a new instance of the `ValidationError` with the given required `problem`, `fix` and optional `id` and - * `template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{min}`, `{max}` and + * `{type}` tags. By default, the value is picked from the static property `ValidationError.template`. + * @returns The return value is a new instance of the `ValidationError` with the message built from the given required `problem`, `fix` + * and optional `id` on the given or stored `template`. * @angularpackage */ - public static define( + public static define( problem: string, fix: string, id?: Id, template = ValidationError.template ): ValidationError { - return super.define(problem, fix, id, template, this); + return new this(problem, fix, id, template); } /** - * Checks whether the value of any type is an instance of `ValidationError`. + * Checks whether the value of any type is the `ValidationError` instance of any or the given identification. * @param value The value of any type to check against the `ValidationError` instance. - * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `ValidationError`. + * @param id Optional unique identification of generic type variable `Id` to check whether the given `value` contains. + * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `ValidationError` of any or the + * supplied optional id properties. + * @angularpackage */ public static isValidationError( value: any, @@ -56,41 +62,24 @@ export class ValidationError extends CommonError { ): value is ValidationError { return super.isError(value, id); } - - /** - * Throws the `ValidationError` with the given required `problem`, `fix` and optional `id` and `template`. - * @param problem Description of the problem of a `string` type. - * @param fix A solution to the given `problem` of a `string` type. - * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the value - * is picked from the static property `template`. - * @angularpackage - */ - public static throw( - problem: string, - fix: string, - id?: string, - template?: string - ): void { - super.throw(problem, fix, id, template); - } //#endregion public static methods. //#region constructor. /** - * Creates a `ValidationError` instance that represents validation error with the described problem and its solution, optionally marked - * with an explicit identification. + * Creates the `ValidationError` instance that represents validation error with the message built of the given described problem, its + * solution, and optional explicit identification, on the supplied or stored error message template. * @param problem Description of the validation problem of a `string` type. * @param fix A solution to the given validation issue of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` words. By default, the - * value is picked from the static property `template`. + * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is picked from the static property `template`. * @angularpackage */ constructor( problem: string, fix: string, id?: Id, + // field?: string, template = ValidationError.template ) { super(problem, fix, id, template); From 3306c843dd4dfb3165a588c254b3207b1048aab3 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 9 Feb 2022 00:42:07 +0100 Subject: [PATCH 125/179] docs(RangeError): change the main description and static `define()` method description. --- src/lib/range-error.class.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts index fe72f28..1f7437f 100644 --- a/src/lib/range-error.class.ts +++ b/src/lib/range-error.class.ts @@ -1,8 +1,8 @@ import { CommonError } from './common-error.class'; /** * The `RangeError` object is an extension of the `CommonError` class and is thrown when a value is not in the set or range of allowed - * values with the message built from the described problem and its solution, optional explicit identification on the given or stored - * template. + * values with the message built from the described problem and its solution, optional explicit identification and minimum/maximum range + * on the given or stored template. */ export class RangeError extends CommonError { /** @@ -86,8 +86,9 @@ export class RangeError extends CommonError { * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown). * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown). - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` tags. By default, the value - * is picked from the static property `RangeError.template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is equal to the static property `template`. By default, the value is picked from the static + * property `RangeError.template`. * @returns The return value is a new instance of the `RangeError` with the message built from the given required `problem`, `fix` and * optional `id`, `min`, `max` on the given or stored `template`. * @angularpackage From fb6aabc360a9096b225351c633d84c78797483be Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 9 Feb 2022 00:44:45 +0100 Subject: [PATCH 126/179] test(Error): format. --- src/test/error.spec.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/test/error.spec.ts b/src/test/error.spec.ts index 52d66b8..1018233 100644 --- a/src/test/error.spec.ts +++ b/src/test/error.spec.ts @@ -68,30 +68,22 @@ testing.describe('[counter] Error', () => { /** * Error.prototype.id */ - .it(`Error.prototype.id`, () => { - expect(error.id).toEqual(id); - }) + .it(`Error.prototype.id`, () => expect(error.id).toEqual(id)) /** * Error.prototype.name */ - .it(`Error.prototype.name`, () => { - expect(error.name).toEqual('Error'); - }) + .it(`Error.prototype.name`, () => expect(error.name).toEqual('Error')) /** * Error.prototype.problem */ - .it(`Error.prototype.problem`, () => { - expect(error.problem).toEqual(problem); - }) + .it(`Error.prototype.problem`, () => expect(error.problem).toEqual(problem)) /** * Error.prototype.template */ - .it(`Error.prototype.template`, () => { - expect(error.template).toEqual(template); - }) + .it(`Error.prototype.template`, () => expect(error.template).toEqual(template)) /** * [Symbol.toStringTag] From cb061026cf52f2b37054c74928da996184765b74 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 9 Feb 2022 00:46:56 +0100 Subject: [PATCH 127/179] test(RangeError): update tests to new order of the `id` parameter. --- src/test/range-error.spec.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/test/range-error.spec.ts b/src/test/range-error.spec.ts index bab8a27..7abfb43 100644 --- a/src/test/range-error.spec.ts +++ b/src/test/range-error.spec.ts @@ -39,7 +39,7 @@ testing.describe('[counter] RangeError', () => { problem = 'The value'; template = `Problem(VE{id}): {problem} {min} and {max}.\nFix: {fix}.`; value = Symbol(123); - rangeError = new RangeError(problem, fix, min, max, id, template); + rangeError = new RangeError(problem, fix, id, min, max, template); }); testing @@ -122,8 +122,8 @@ testing.describe('[counter] RangeError', () => { /** * RangeError.define() */ - .it(`RangeError.define()`, () => { - const e = RangeError.define(problem, fix, min, max, id, template); + .it(`RangeError.define(problem, fix, id, min, max, template)`, () => { + const e = RangeError.define(problem, fix, id, min, max, template); expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${min} and ${max}.\nFix: ${fix}.`); // Required. expect(e.fix).toEqual(fix); @@ -140,13 +140,13 @@ testing.describe('[counter] RangeError', () => { */ .it(`RangeError.isRangeError()`, () => { expect(RangeError.isRangeError(rangeError)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, undefined, undefined, id)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, min)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, undefined, max)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, min, max)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, min, undefined, id)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, undefined, max, id)).toBeTrue(); - expect(RangeError.isRangeError(rangeError, min, max, id)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, id)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, undefined, min)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, undefined, undefined, max)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, undefined, min, max)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, id, min, undefined)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, id, undefined, max)).toBeTrue(); + expect(RangeError.isRangeError(rangeError, id, min, max)).toBeTrue(); }); }) @@ -168,8 +168,8 @@ testing.describe('[counter] RangeError', () => { expect(e.template).toEqual(RangeError.template); }) - .it(`(problem, fix, min, undefined, id)`, () => { - const e = new RangeError(problem, fix, min, undefined, id); + .it(`(problem, fix, id, min, undefined)`, () => { + const e = new RangeError(problem, fix, id, min, undefined); expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${''} => Fix: ${fix}`); // Required. expect(e.fix).toEqual(fix); @@ -180,8 +180,8 @@ testing.describe('[counter] RangeError', () => { expect(e.min).toEqual(min); expect(e.template).toEqual(RangeError.template); }) - .it(`(problem, fix, undefined, max, id)`, () => { - const e = new RangeError(problem, fix, undefined, max, id); + .it(`(problem, fix, id, undefined, max)`, () => { + const e = new RangeError(problem, fix, id, undefined, max); expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${''} and ${max} => Fix: ${fix}`); // Required. expect(e.fix).toEqual(fix); @@ -192,8 +192,8 @@ testing.describe('[counter] RangeError', () => { expect(e.min).toBeUndefined(); expect(e.template).toEqual(RangeError.template); }) - .it(`(problem, fix, min, max, id)`, () => { - const e = new RangeError(problem, fix, min, max, id); + .it(`(problem, fix, id, min, max)`, () => { + const e = new RangeError(problem, fix, id, min, max); expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${max} => Fix: ${fix}`); // Required. expect(e.fix).toEqual(fix); @@ -204,13 +204,13 @@ testing.describe('[counter] RangeError', () => { expect(e.min).toEqual(min); expect(e.template).toEqual(RangeError.template); }) - .it(`(problem, fix, min, max, id, template)`, () => { + .it(`('The age', 'Try to change minimum or maximum', id, min, max, template)`, () => { const e = new RangeError( `The age`, `Try to change minimum or maximum`, + id, min, max, - id, template ); expect(e.message).toEqual(`Problem(VE${id}): ${'The age'} ${min} and ${max}.\nFix: ${'Try to change minimum or maximum'}.`); From 0a90a64617e0a37aeb2e911afaa0c0755ba7c326 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 9 Feb 2022 00:51:13 +0100 Subject: [PATCH 128/179] test(TypeError): update to the new order of the `id` parameter, and change expectedType to just type. --- src/test/type-error.spec.ts | 63 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/src/test/type-error.spec.ts b/src/test/type-error.spec.ts index b7d3841..15b30e3 100644 --- a/src/test/type-error.spec.ts +++ b/src/test/type-error.spec.ts @@ -3,9 +3,6 @@ import { Testing, TestingToBeMatchers } from '@angular-package/testing'; import { typeOf } from '@angular-package/type'; // Class. import { TypeError } from '../lib/type-error.class'; - -const et = new TypeError('problem', 'fix', 'string'); - /** * Initialize `Testing`. */ @@ -19,7 +16,7 @@ testing.describe('[counter] TypeError', () => { let id: string; let problem: string; let template: string; - let expectedType: string; + let type: string; let typeError: TypeError; // Prepare the values. @@ -27,7 +24,7 @@ testing.describe('[counter] TypeError', () => { id = '427'; problem = 'The value must be a string type.'; template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; - expectedType = 'Symbol'; + type = 'Symbol'; beforeEach(() => { // Prepare the values. @@ -35,8 +32,8 @@ testing.describe('[counter] TypeError', () => { id = '427'; problem = 'The value must be a string type.'; template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; - expectedType = 'Symbol'; - typeError = new TypeError(problem, fix, expectedType, id, template); + type = 'Symbol'; + typeError = new TypeError(problem, fix, id, type, template); }); testing @@ -47,10 +44,10 @@ testing.describe('[counter] TypeError', () => { .describe(`Static properties`, () => { testing .it(`TypeError.template`, () => { - expect(TypeError.template).toEqual(`Problem{id}: {problem} must be of {type} => Fix: {fix}`); + expect(TypeError.template).toEqual(`Problem{id}: {problem} => Fix: {fix} must be of the {type}`); TypeError.template = `{problem} => Fix: {fix} of {id}`; expect(TypeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); - TypeError.template = `Problem{id}: {problem} must be of {type} => Fix: {fix}`; + TypeError.template = `Problem{id}: {problem} => Fix: {fix} must be of the {type}`; }); }) @@ -63,7 +60,7 @@ testing.describe('[counter] TypeError', () => { /** * TypeError.prototype.expectedType */ - .it(`TypeError.prototype.expectedType`, () => expect(typeError.expectedType).toEqual(expectedType)) + .it(`TypeError.prototype.expectedType`, () => expect(typeError.type).toEqual(type)) /** * TypeError.prototype.fix @@ -108,14 +105,14 @@ testing.describe('[counter] TypeError', () => { /** * TypeError.define() */ - .it(`TypeError.define()`, () => { - const e = TypeError.define(problem, fix, expectedType, id, template); - expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${expectedType}\nFix: ${fix}`); + .it(`TypeError.define(problem, fix, id, type, template)`, () => { + const e = TypeError.define(problem, fix, id, type, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${type}\nFix: ${fix}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); // Optional. - expect(e.expectedType).toEqual(expectedType); + expect(e.type).toEqual(type); expect(e.id).toEqual(id); expect(e.template).toEqual(template); }) @@ -125,7 +122,7 @@ testing.describe('[counter] TypeError', () => { */ .it(`TypeError.isTypeError()`, () => { expect(TypeError.isTypeError(typeError)).toBeTrue(); - expect(TypeError.isTypeError(typeError, expectedType, id)).toBeTrue(); + expect(TypeError.isTypeError(typeError, id, type)).toBeTrue(); }); }) @@ -136,57 +133,57 @@ testing.describe('[counter] TypeError', () => { testing .it(`(problem, fix)`, () => { const e = new TypeError(problem, fix); - expect(e.message).toEqual(`Problem${''}: ${problem} must be of ${''} => Fix: ${fix}`); + expect(e.message).toEqual(`Problem${''}: ${problem} => Fix: ${fix} must be of the ${''}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); // Optional. expect(e.id).toBeUndefined(); - expect(e.expectedType).toBeUndefined(); + expect(e.type).toBeUndefined(); expect(e.template).toEqual(TypeError.template); }) - .it(`(problem, fix, type)`, () => { - const e = new TypeError(problem, fix, expectedType); - expect(e.message).toEqual(`Problem${''}: ${problem} must be of ${expectedType} => Fix: ${fix}`); + .it(`(problem, fix, undefined, type)`, () => { + const e = new TypeError(problem, fix, undefined, type); + expect(e.message).toEqual(`Problem${''}: ${problem} => Fix: ${fix} must be of the ${type}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); // Optional. expect(e.id).toBeUndefined(); - expect(e.expectedType).toEqual(expectedType); + expect(e.type).toEqual(type); expect(e.template).toEqual(TypeError.template); }) - .it(`(problem, fix, undefined, id)`, () => { - const e = new TypeError(problem, fix, undefined, id); - expect(e.message).toEqual(`Problem${id}: ${problem} must be of ${''} => Fix: ${fix}`); + .it(`(problem, fix, id)`, () => { + const e = new TypeError(problem, fix, id); + expect(e.message).toEqual(`Problem${id}: ${problem} => Fix: ${fix} must be of the ${''}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); // Optional. expect(e.id).toEqual(id); - expect(e.expectedType).toBeUndefined(); + expect(e.type).toBeUndefined(); expect(e.template).toEqual(TypeError.template); }) - .it(`(problem, fix, type, id)`, () => { - const e = new TypeError(problem, fix, expectedType, id); - expect(e.message).toEqual(`Problem${id}: ${problem} must be of ${expectedType} => Fix: ${fix}`); + .it(`(problem, fix, id, type)`, () => { + const e = new TypeError(problem, fix, id, type); + expect(e.message).toEqual(`Problem${id}: ${problem} => Fix: ${fix} must be of the ${type}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); // Optional. expect(e.id).toEqual(id); - expect(e.expectedType).toEqual(expectedType); + expect(e.type).toEqual(type); expect(e.template).toEqual(TypeError.template); }) - .it(`(problem, fix, type, id, template)`, () => { - const e = new TypeError(problem, fix, expectedType, id, template); - expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${expectedType}\nFix: ${fix}`); + .it(`(problem, fix, id, type, template)`, () => { + const e = new TypeError(problem, fix, id, type, template); + expect(e.message).toEqual(`Problem(VE${id}): ${problem} ${type}\nFix: ${fix}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); // Optional. expect(e.id).toEqual(id); - expect(e.expectedType).toEqual(expectedType); + expect(e.type).toEqual(type); expect(e.template).toEqual(template); }); }); From a5f391cbdb3a9a756b0f730bdc3c63232889ce0b Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 9 Feb 2022 22:14:29 +0100 Subject: [PATCH 129/179] feat(Errors): add class to handle all the errors in one object. --- src/lib/errors.class.ts | 389 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 389 insertions(+) create mode 100644 src/lib/errors.class.ts diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts new file mode 100644 index 0000000..46d6c87 --- /dev/null +++ b/src/lib/errors.class.ts @@ -0,0 +1,389 @@ +import { Error } from './error.class'; +import { RangeError } from './range-error.class'; +import { TypeError } from './type-error.class'; +import { ValidationError } from './validation-error.class'; +/** + * The `Errors` object represents the errors of the type, range, type, validation and simply error with multiple identification numbers. + * It holds properties and use them to define new error or throw it. + */ +export class Errors { + //#region public instance accessors. + /** + * + */ + public get define(): { + error: typeof errors.defineError; + rangeError: typeof errors.defineRangeError; + typeError: typeof errors.defineTypeError; + validationError: typeof errors.defineValidationError; + } { + const errors = this; + return Object.freeze({ + error: (id: DefineId): Error => + errors.defineError(id), + rangeError: ( + id: DefineId, + min?: number, + max?: number + ): RangeError => errors.defineRangeError(id, min, max), + typeError: ( + id: DefineId, + type?: string + ): TypeError => errors.defineTypeError(id, type), + validationError: ( + id: DefineId + ): ValidationError => errors.defineValidationError(id), + }); + } + + /** + * The get `accessor` obtains a possible solution to the described problem by returning the `#fix` property of a specified object. + * @returns The return value is the fix of a `string` type. + * @angularpackage + */ + public get fix(): string { + return this.#fix; + } + + /** + * The `get` accessor obtains the maximum range of number type if set, otherwise undefined. + * @returns The return value is the maximum range of a number type or undefined. + * @angularpackage + */ + public get max(): number | undefined { + return this.#max; + } + + /** + * The `get` accessor obtains the minimum range of number type if set, otherwise undefined. + * @returns The return value is the minimum range of a number type or undefined. + * @angularpackage + */ + public get min(): number | undefined { + return this.#min; + } + + /** + * The `get` accessor gets the problem by returning the `#problem` property of a specified object. + * @returns The return value is the problem of a `string` type. + * @angularpackage + */ + public get problem(): string { + return this.#problem; + } + + /** + * The `get` accessor obtains the maximum and maximum range in the form of an object. + * @returns The return value is an object that consists of the minimum and maximum range. + * @angularpackage + */ + public get range(): { min?: number; max?: number } { + return { + min: this.#min, + max: this.#max, + }; + } + + /** + * The `get` accessor gets the template of the error message by returning the `#template` property of a specified object. + * @returns The return value is the template of a `string` type. + * @angularpackage + */ + public get template(): string | undefined { + return this.#template; + } + + /** + * + */ + public get throw(): { + error: typeof t.throwError; + rangeError: typeof t.throwRangeError; + typeError: typeof t.throwTypeError; + validationError: typeof t.throwValidationError; + } { + const t = this; + return { + error: (id: ThrowId): void => t.throwError(id), + rangeError: ( + id: ThrowId, + min?: number, + max?: number + ): void => t.throwRangeError(id, min, max), + typeError: (id: ThrowId, type?: string): void => + t.throwTypeError(id, type), + validationError: (id: ThrowId): void => + t.throwValidationError(id), + }; + } + + /** + * The `get` accessor obtains the type of a `string` that causes an error to be thrown(or not thrown) if set, otherwise returns + * `undefined`. + * @returns The return value is the type of a `string` type. + * @angularpackage + */ + public get type(): string | undefined { + return this.#type; + } + //#endregion public instance accessors. + + //#region private instance properties. + /** + * The collection of unique allowed identification numbers of generic type variable `Id`. + */ + #allowedId?: Set; + + /** + * A privately stored possible solution to the described problem of a `string` type. + */ + #fix = ''; + + /** + * Private property of the maximum range of a `number` type that causes an error to be thrown(or not thrown). + */ + #max?: number; + + /** + * Private property of the minimum range of a `number` type that causes an error to be thrown(or not thrown). + */ + #min?: number; + + /** + * A privately stored problem of a `string` type. + */ + #problem = ''; + + /** + * A string-type privately stored template of the error message that contains replaceable required `{fix}`, `{problem}` and optional + * `{id}`, `{max}`, `{min}`, `{type}` tags. + */ + #template?: string; + + /** + * Private string-type property of the type that causes an error to be thrown(or not thrown). + */ + #type?: string; + //#endregion private instance properties. + + //#region constructor. + /** + * + * @param problem + * @param fix + * @param allowedId + * @angularpackage + */ + constructor( + problem: string, + fix: string, + allowedId: Id[], + additional?: { max?: number; min?: number; type?: string }, + template?: string, + ) { + Array.isArray(allowedId) && (this.#allowedId = new Set(allowedId)); + this.#fix = fix; + this.#problem = problem; + this.#max = additional?.max; + this.#min = additional?.min; + this.#template = template; + this.#type = additional?.type; + } + //#endregion constructor. + + //#region instance public methods. + /** + * + * @param id + * @returns + * @angularpackage + */ + public defineError(id: DefineId, template = this.#template): Error { + return ( + this.isAllowedId(id), + new Error(this.#problem, this.#fix, id, template) + ); + } + + /** + * + * @param id + * @returns + * @angularpackage + */ + public defineRangeError( + id: DefineId, + min: number | undefined = this.#min, + max: number | undefined = this.#max, + template = this.#template + ): RangeError { + return ( + this.isAllowedId(id), + new RangeError(this.#problem, this.#fix, id, min, max, template) + ); + } + + /** + * + * @param id + * @returns + * @angularpackage + */ + public defineTypeError( + id: DefineId, + type: string | undefined = this.#type, + template = this.#template + ): TypeError { + return ( + this.isAllowedId(id), + new TypeError(this.#problem, this.#fix, id, type, template) + ); + } + + /** + * + * @param id + * @returns + * @angularpackage + */ + public defineValidationError( + id: DefineId, + template = this.#template + ): ValidationError { + return ( + this.isAllowedId(id), + new ValidationError(this.#problem, this.#fix, id, template) + ); + } + + /** + * Sets the fix a possible solution to the described problem. + * @param fix A possible solution to the described problem of a `string` type. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setFix(fix: string): this { + this.#fix = fix; + return this; + } + + public setMax(max?: number): this { + this.#max = max; + return this; + } + + public setMin(min?: number): this { + this.#min = min; + return this; + } + + /** + * Sets description problem of a validation. + * @param problem Description of validation problem of a `string` type. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setProblem(problem: string): this { + this.#problem = problem; + return this; + } + + /** + * Sets the template of validation error message. + * @param template A message template of `string` type with replaceable `{problem}`, `{fix}` and optional `{id}` tags. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public setTemplate(template?: string): this { + this.#template = template; + return this; + } + + /** + * + * @param type + * @returns + * @angularpackage + */ + public setType(type?: string): this { + this.#type = type; + return this; + } + + /** + * + * @param id + * @angularpackage + */ + public throwError( + id: ThrowId, + template = this.#template + ): void { + throw ( + (this.isAllowedId(id), new Error(this.#problem, this.#fix, id, template)) + ); + } + + /** + * + * @param id + * @angularpackage + */ + public throwRangeError( + id: ThrowId, + min: number | undefined = this.#min, + max: number | undefined = this.#max, + template = this.#template + ): void { + throw ( + (this.isAllowedId(id), + new RangeError(this.#problem, this.#fix, id, min, max, template)) + ); + } + + /** + * + * @param id + * @angularpackage + */ + public throwTypeError( + id: ThrowId, + type: string | undefined = this.#type, + template = this.#template + ): void { + throw ( + (this.isAllowedId(id), + new TypeError(this.#problem, this.#fix, type, id, template)) + ); + } + + /** + * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem`, and optional `id` on the stored + * `template`. + * @param id + * @angularpackage + */ + public throwValidationError(id: ThrowId, template = this.#template): void { + throw ( + (this.isAllowedId(id), + new ValidationError(this.#problem, this.#fix, id, template)) + ); + } + //#endregion instance public methods. + + //#region instance private methods. + /** + * + * @param id + * @angularpackage + */ + private isAllowedId(id: CheckId): void { + if (this.#allowedId && this.#allowedId.has(id) === false) { + throw new RangeError( + `The provided identification ${id} is not allowed.`, + `Set allowed id: ${id} in the allowedId parameter of the constructor.` + ); + } + } + //#endregion instance private methods. +} From 86cfe6ac990bd5166d4cc84e0141dc1b0541daa3 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 00:51:17 +0100 Subject: [PATCH 130/179] refactor(CommonError): add `get` accessor `link`, private `#link` property, replaceable tag `{link}`, add property `link` to the parameter `additional` of the constructor. --- src/lib/common-error.class.ts | 39 ++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index ce2a44e..6527515 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -5,8 +5,8 @@ export abstract class CommonError extends Error { //#region public static properties. /** - * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}`, - * `{type}` tags. By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. + * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{link}`, `{max}`, + * `{min}`, `{type}` tags. By default, it's set to `Problem{id}: {problem} => Fix: {fix}`. */ public static template = `Problem{id}: {problem} => Fix: {fix}`; //#endregion public static properties. @@ -30,6 +30,15 @@ export abstract class CommonError extends Error { return this.#id; } + /** + * The `get` accessor gets the link(to read more about the thrown error) by returning the `#link` property of a specified object. + * @returns The return value is the link of a `string` type or `undefined`. + * @angularpackage + */ + public get link(): string | undefined { + return this.#link; + } + /** * The `get` accessor gets the error message by returning the parent `message` property of the `Error` object. * @returns The return value is the error message of a `string` type. @@ -69,6 +78,11 @@ export abstract class CommonError extends Error { */ #id?: Id; + /** + * A privately stored link redirects to read more about the thrown error. + */ + #link?: string; + /** * A privately stored problem of a `string` type. */ @@ -97,15 +111,16 @@ export abstract class CommonError extends Error { fix: string, id: string | undefined, template: string, - additional: { min?: number; max?: number; type?: string; }; + additional: { link?: string; min?: number; max?: number; type?: string }; [problem, fix, id, template, additional] = values; template = (template || CommonError.template) - .replace('{fix}', fix || '') - .replace(/{id}/g, id || '') .replace('{problem}', problem || '') - .replace(/{max}/g, additional?.max ? String(additional?.max) : '') - .replace(/{min}/g, additional?.min ? String(additional?.min) : '') - .replace(/{type}/g, additional?.type ? additional?.type : ''); + .replace(/{id}/g, id || '') + .replace(/{link}/g, additional?.link ? additional.link : '') + .replace(/{max}/g, additional?.max ? String(additional.max) : '') + .replace(/{min}/g, additional?.min ? String(additional.min) : '') + .replace(/{type}/g, additional?.type ? additional.type : '') + .replace('{fix}', fix || ''); return template; } @@ -135,9 +150,9 @@ export abstract class CommonError extends Error { * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is equal to the static property `template`. - * @param additional An optional object consists of optional `min`, `max`, and `type` properties to define the error message. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{link}`, `{max}`, `{min}` + * and `{type}` tags. By default, the value is equal to the static property `template`. + * @param additional An optional object consists of optional `link`, `min`, `max`, and `type` properties to define the error message. * @angularpackage */ constructor( @@ -145,7 +160,7 @@ export abstract class CommonError extends Error { fix: string, id?: Id, template = CommonError.template, - additional?: { max?: number; min?: number; type?: string } + additional?: { link?: string; max?: number; min?: number; type?: string } ) { super( CommonError.defineMessage`${problem}${fix}${id}${template}${additional}` From 08f76e4bc9778ca6afc3e710783e8f2442b17d77 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 11:38:34 +0100 Subject: [PATCH 131/179] refactor(RangeError): add generic type variables `Min` and `Max` to parameters. properties and accessors. Update jsdoc. --- src/lib/range-error.class.ts | 78 +++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts index 1f7437f..cc0807b 100644 --- a/src/lib/range-error.class.ts +++ b/src/lib/range-error.class.ts @@ -4,7 +4,11 @@ import { CommonError } from './common-error.class'; * values with the message built from the described problem and its solution, optional explicit identification and minimum/maximum range * on the given or stored template. */ -export class RangeError extends CommonError { +export class RangeError< + Id extends string, + Min extends number | undefined = undefined, + Max extends number | undefined = undefined +> extends CommonError { /** * A template of the error message of `string` type with the replaceable required `{problem}`, `{fix}` and optional `{id}`, `{max}`, * `{min}`, `{type}` tags. By default, it's set to `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`. @@ -13,22 +17,22 @@ export class RangeError extends CommonError { //#region public instance accessors. /** - * The `get` accessor obtains the maximum range of `number` type that causes an error to be thrown(or not thrown), if set, otherwise - * returns `undefined`. - * @returns The return value is the maximum range of a `number` type or `undefined`. + * The `get` accessor obtains the maximum range of generic type variable `Max` that causes an error to be thrown(or not thrown), if set, + * otherwise returns `undefined`. + * @returns The return value is the maximum range of generic type variable `Max` or `undefined`. * @angularpackage */ - public get max(): number | undefined { + public get max(): Max | undefined { return this.#max; } /** - * The `get` accessor obtains the minimum range of `number` type that causes an error to be thrown(or not thrown), if set, otherwise - * returns `undefined`. - * @returns The return value is the minimum range of a `number` type or undefined. + * The `get` accessor obtains the minimum range of generic type variable `Min` that causes an error to be thrown(or not thrown), if set, + * otherwise returns `undefined`. + * @returns The return value is the minimum range of generic type variable `Min` or undefined. * @angularpackage */ - public get min(): number | undefined { + public get min(): Min | undefined { return this.#min; } @@ -47,7 +51,7 @@ export class RangeError extends CommonError { * `max` property. * @angularpackage */ - public get range(): { min?: number; max?: number } { + public get range(): { min?: Min; max?: Max } { return { min: this.#min, max: this.#max, @@ -67,14 +71,14 @@ export class RangeError extends CommonError { //#region private instance properties. /** - * Private property of the maximum range of a `number` type that causes an error to be thrown(or not thrown). + * Private property of the maximum range of generic type variable `Max` that causes an error to be thrown(or not thrown). */ - #max?: number; + #max?: Max; /** - * Private property of the minimum range of a `number` type that causes an error to be thrown(or not thrown). + * Private property of the minimum range of generic type variable `Min` that causes an error to be thrown(or not thrown). */ - #min?: number; + #min?: Min; //#endregion private instance properties. //#region public static methods. @@ -84,23 +88,27 @@ export class RangeError extends CommonError { * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown). - * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown). + * @param min The optional minimum range of generic type variable `Min` that causes an error to be thrown(or not thrown). + * @param max The optional maximum range of generic type variable `Max` that causes an error to be thrown(or not thrown). * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is equal to the static property `template`. By default, the value is picked from the static + * `{type}` tags. By default, the value is picked from the static * property `RangeError.template`. * @returns The return value is a new instance of the `RangeError` with the message built from the given required `problem`, `fix` and * optional `id`, `min`, `max` on the given or stored `template`. * @angularpackage */ - public static define( + public static define< + Id extends string, + Min extends number | undefined = undefined, + Max extends number | undefined = undefined + >( problem: string, fix: string, id?: Id, - min?: number, - max?: number, + min?: Min, + max?: Max, template = RangeError.template - ): RangeError { + ): RangeError { return new this(problem, fix, id, min, max, template); } @@ -108,20 +116,24 @@ export class RangeError extends CommonError { * Checks whether the value of any type is an instance of `RangeError` of any or the given minimum/maximum range and identification. * @param value The value of any type to check against the `RangeError` instance. * @param id Optional identification of generic type variable `Id` to check whether the given `value` contains. - * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown) to check whether the given - * `value` contains. - * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown) to check whether the given - * `value` contains. + * @param min The optional minimum range of generic type variable `Min` that causes an error to be thrown(or not thrown) to check whether + * the given `value` contains. + * @param max The optional maximum range of generic type variable `Max` that causes an error to be thrown(or not thrown) to check whether + * the given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `RangeError` of any or the given * `min`, max` and `id` properties. * @angularpackage */ - public static isRangeError( + public static isRangeError< + Id extends string, + Min extends number | undefined = undefined, + Max extends number | undefined = undefined + >( value: any, id?: Id, - min?: number, - max?: number - ): value is RangeError { + min?: Min, + max?: Max + ): value is RangeError { return ( super.isError(value, id) && (typeof min === 'number' ? (value as any).min === min : true) && @@ -136,8 +148,8 @@ export class RangeError extends CommonError { * optional `min`/`max` range, and an explicit identification on the given or stored error message template. * @param problem Description of the range problem of a `string` type. * @param fix A solution to the given range issue of a `string` type. - * @param min The optional minimum range of a `number` type that causes an error to be thrown(or not thrown). - * @param max The optional maximum range of a `number` type that causes an error to be thrown(or not thrown). + * @param min The optional minimum range of generic type variable `Min` that causes an error to be thrown(or not thrown). + * @param max The optional maximum range of generic type variable `Max` that causes an error to be thrown(or not thrown). * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param template A template of error message with the replaceable required `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` * tags. By default, the value is equal to the static property `template`. @@ -147,8 +159,8 @@ export class RangeError extends CommonError { problem: string, fix: string, id?: Id, - min?: number, - max?: number, + min?: Min, + max?: Max, template = RangeError.template ) { super(problem, fix, id, template, { min, max }); From 0cf542a74ac94f4ab2c48a2a0ec2349043fa40d0 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 11:39:23 +0100 Subject: [PATCH 132/179] test(RangeError): declare constants instead of variables. --- src/test/range-error.spec.ts | 42 +++++++++++------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/src/test/range-error.spec.ts b/src/test/range-error.spec.ts index 7abfb43..6297904 100644 --- a/src/test/range-error.spec.ts +++ b/src/test/range-error.spec.ts @@ -12,35 +12,20 @@ const toBe = new TestingToBeMatchers(); * Tests. */ testing.describe('[counter] RangeError', () => { - let fix: string; - let id: string; - let min: number; - let max: number; - let problem: string; - let template: string; - let value: any; - let rangeError: RangeError; - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - min = 9; - max = 27; - problem = 'The value'; - template = `Problem(VE{id}): {problem} {min} and {max}.\nFix: {fix}.`; - value = Symbol(123); - - beforeEach(() => { - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - min = 9; - max = 27; - problem = 'The value'; - template = `Problem(VE{id}): {problem} {min} and {max}.\nFix: {fix}.`; - value = Symbol(123); - rangeError = new RangeError(problem, fix, id, min, max, template); - }); + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const id = '427'; + const min = 9; + const max = 27; + const problem = 'The value'; + const template = `Problem(VE{id}): {problem} {min} and {max}.\nFix: {fix}.`; + + let rangeError = new RangeError(problem, fix, id, min, max, template); + + beforeEach(() => ( + RangeError.template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`, + rangeError = new RangeError(problem, fix, id, min, max, template) + )); testing @@ -52,7 +37,6 @@ testing.describe('[counter] RangeError', () => { expect(RangeError.template).toEqual(`Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`); RangeError.template = `{problem} => Fix: {fix} of {id}`; expect(RangeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); - RangeError.template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`; }); }) From bcd7ae6bf77b085d163333f0f5f8519f57a4a7ef Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 11:39:39 +0100 Subject: [PATCH 133/179] test(Error): declare constants instead of variables. --- src/test/error.spec.ts | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/test/error.spec.ts b/src/test/error.spec.ts index 1018233..f246482 100644 --- a/src/test/error.spec.ts +++ b/src/test/error.spec.ts @@ -12,29 +12,17 @@ const toBe = new TestingToBeMatchers(); * Tests. */ testing.describe('[counter] Error', () => { - let fix: string; - let id: string; - let problem: string; - let template: string; - let value: any; - let error: Error; - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - problem = 'The value must be a string type.'; - template = `Problem(VE{id}): {problem}\nFix: {fix}`; - value = Symbol(123); + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const id = '427'; + const problem = 'The value must be a string type.'; + const template = `Problem(VE{id}): {problem}\nFix: {fix}`; + let error = new Error(problem, fix, id, template); - beforeEach(() => { - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - problem = 'The value must be a string type.'; - template = `Problem(VE{id}): {problem}\nFix: {fix}`; - value = Symbol(123); - error = new Error(problem, fix, id, template); - }); + beforeEach(() => ( + Error.template = `Problem{id}: {problem} => Fix: {fix}`, + error = new Error(problem, fix, id, template) + )); testing @@ -47,7 +35,6 @@ testing.describe('[counter] Error', () => { expect(Error.template).toEqual(`Problem{id}: {problem} => Fix: {fix}`); Error.template = `{problem} => Fix: {fix} of {id}`; expect(Error.template).toEqual(`{problem} => Fix: {fix} of {id}`); - Error.template = `Problem{id}: {problem} => Fix: {fix}`; }); }) From c2f23a40e1170418bf675c896249509e472d0155 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 11:49:20 +0100 Subject: [PATCH 134/179] test(CommonError): add beforeEach to set new instance and test link accessor. --- src/test/common-error.spec.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/test/common-error.spec.ts b/src/test/common-error.spec.ts index c97a2c1..ec464c2 100644 --- a/src/test/common-error.spec.ts +++ b/src/test/common-error.spec.ts @@ -31,15 +31,22 @@ testing.describe('[counter] CommonError', () => { const problem = 'The value must be string type.'; const fix = 'Provide the string type instead of number.'; const id = 'AE: 427'; + const max = 427; + const min = 9; + const link = 'http://duckduckgo.com'; const template = `{problem} {fix} {id} {max} {min} {type}`; - const additional = { max: 427, min: 9, type: 'string' }; - const testError = new TestError(problem, fix, id, template, additional); + const type = 'string'; + const additional = { link, max, min, type }; + let testError = new TestError(problem, fix, id, template, additional); + + beforeEach(() => testError = new TestError(problem, fix, id, template, additional)); testing .describe(`Accessors`, () => { testing .it(`TestError.prototype.fix`, () => expect(testError.fix).toEqual(fix)) .it(`TestError.prototype.id`, () => expect(testError.id).toEqual(id)) + .it(`TestError.prototype.link`, () => expect(testError.link).toEqual(link)) .it(`TestError.prototype.message`, () => expect(testError.message).toEqual( `${problem} ${fix} ${id} ${additional.max} ${additional.min} ${additional.type}` From 3902f0a9a7d1a2b3e238ce8921754102b0f84ad5 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 11:49:54 +0100 Subject: [PATCH 135/179] refactor(CommonError): assign link from the supplied additional in the constructor to the private #link. --- src/lib/common-error.class.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index 6527515..edf4f88 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -167,6 +167,7 @@ export abstract class CommonError extends Error { ); this.#fix = fix; this.#id = id; + this.#link = additional?.link; this.#problem = problem; this.#template = template; } From 2edb0986338f928ecc22efad4114ccdb496f0d94 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 12:38:00 +0100 Subject: [PATCH 136/179] refactor(TypeError): add generic type variable `Type` for the parameter, accessor, property`type`. Update jsdoc. --- src/lib/type-error.class.ts | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/lib/type-error.class.ts b/src/lib/type-error.class.ts index aafb1b3..5b45f6e 100644 --- a/src/lib/type-error.class.ts +++ b/src/lib/type-error.class.ts @@ -4,7 +4,10 @@ import { CommonError } from './common-error.class'; * typically(but not exclusively) when a value is not of the expected type, with the message built from the described problem and its * solution, optional an explicit identification and type, on the given or stored template. */ -export class TypeError extends CommonError { +export class TypeError< + Id extends string, + Type extends string | undefined = undefined +> extends CommonError { /** * A template of the error message of `string` type with the replaceable required `{fix}`,`{problem}` and optional `{id}`, `{max}`, * `{min}`, `{type}` tags. By default, it's set to `Problem{id}: {problem} => Fix: {fix} must be of the {type}`. @@ -22,12 +25,12 @@ export class TypeError extends CommonError { } /** - * The `get` accessor obtains the type of a `string` that causes an error to be thrown(or not thrown) if set, otherwise returns - * `undefined`. - * @returns The return value is the type of a `string` type. + * The `get` accessor obtains the type of generic type variable `Type` that causes an error to be thrown(or not thrown) if set, otherwise + * returns `undefined`. + * @returns The return value is the type of generic type variable `Type` or `undefined`. * @angularpackage */ - public get type(): string | undefined { + public get type(): Type | undefined { return this.#type; } @@ -45,7 +48,7 @@ export class TypeError extends CommonError { /** * Private string-type property of the type that causes an error to be thrown(or not thrown). */ - #type?: string; + #type?: Type; //#region public static methods. /** @@ -53,20 +56,23 @@ export class TypeError extends CommonError { * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param type The optional type that causes an error to be thrown(or not thrown). + * @param type The optional type of generic type variable `Type` that causes an error to be thrown(or not thrown). * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{min}`, `{max}` and * `{type}` tags. By default, the value is picked from the static property `TypeError.template`. * @returns The return value is a new instance of the `TypeError` with the message built from the given required `problem`, `fix` and * optional `id`, `type` on the given or stored `template`. * @angularpackage */ - public static define( + public static define< + Id extends string, + Type extends string | undefined = undefined + >( problem: string, fix: string, id?: Id, - type?: string, + type?: Type, template = TypeError.template - ): TypeError { + ): TypeError { return new this(problem, fix, id, type, template); } @@ -74,16 +80,16 @@ export class TypeError extends CommonError { * Checks whether the value of any type is an instance of `TypeError` of any or the given type and identification. * @param value The value of any type to check against the `TypeError` instance. * @param id Optional unique identification of generic type variable `Id` to check whether the given `value` contains. - * @param type The optional type that causes an error to be thrown(or not thrown) to check whether the given `value` contains. + * @param type The optional type of generic type variable `Type` that causes an error to be thrown(or not thrown) to check whether the + * given `value` contains. * @returns The return value is a `boolean` type indicating whether the given `value` is an instance of `TypeError` of any or the given * `type` and `id` properties. * @angularpackage */ - public static isTypeError( - value: any, - id?: Id, - type?: string - ): value is TypeError { + public static isTypeError< + Id extends string, + Type extends string | undefined = undefined + >(value: any, id?: Id, type?: Type): value is TypeError { return ( super.isError(value, id) && (typeof type === 'string' ? (value as any).type === type : true) @@ -98,7 +104,7 @@ export class TypeError extends CommonError { * @param problem Description of the range problem of a `string` type. * @param fix A solution to the given range issue of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param type The optional type that causes an error to be thrown(or not thrown). + * @param type The optional type of generic type variable `Type` that causes an error to be thrown(or not thrown). * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and * `{type}` tags. By default, the value is picked from the static property `TypeError.template`. * @angularpackage @@ -107,7 +113,7 @@ export class TypeError extends CommonError { problem: string, fix: string, id?: Id, - type?: string, + type?: Type, template = TypeError.template ) { super(problem, fix, id, template, { type }); From 857ded0f9de166d448984d5f01d1d5a4797b267a Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 12:38:39 +0100 Subject: [PATCH 137/179] test(TypeError): change let to const. --- src/test/type-error.spec.ts | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/test/type-error.spec.ts b/src/test/type-error.spec.ts index 15b30e3..09b30e6 100644 --- a/src/test/type-error.spec.ts +++ b/src/test/type-error.spec.ts @@ -12,29 +12,18 @@ const toBe = new TestingToBeMatchers(); * Tests. */ testing.describe('[counter] TypeError', () => { - let fix: string; - let id: string; - let problem: string; - let template: string; - let type: string; - let typeError: TypeError; - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - problem = 'The value must be a string type.'; - template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; - type = 'Symbol'; - - beforeEach(() => { - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - problem = 'The value must be a string type.'; - template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; - type = 'Symbol'; - typeError = new TypeError(problem, fix, id, type, template); - }); + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const id = '427'; + const problem = 'The value must be a string type.'; + const template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; + const type = 'Symbol'; + let typeError = new TypeError(problem, fix, id, type, template); + + beforeEach(() => ( + TypeError.template = `Problem{id}: {problem} => Fix: {fix} must be of the {type}`, + typeError = new TypeError(problem, fix, id, type, template) + )); testing @@ -47,7 +36,6 @@ testing.describe('[counter] TypeError', () => { expect(TypeError.template).toEqual(`Problem{id}: {problem} => Fix: {fix} must be of the {type}`); TypeError.template = `{problem} => Fix: {fix} of {id}`; expect(TypeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); - TypeError.template = `Problem{id}: {problem} => Fix: {fix} must be of the {type}`; }); }) From 2128570b94f6f1f54b25387c83547513f4a2dd83 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 13:58:03 +0100 Subject: [PATCH 138/179] refactor(ValidationError): remove default value for the generic type variable `Id`. --- src/lib/validation-error.class.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index d336178..50edc4c 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -4,7 +4,7 @@ import { CommonError } from './common-error.class'; * proper type(but not exclusively) with the message built from the described problem and its solution, along with additional identification * on the given or stored template. */ -export class ValidationError extends CommonError { +export class ValidationError extends CommonError { //#region public instance accessors. /** * The `get` accessor obtains error name of a `string` type, set to 'ValidationError' that is being thrown. From 86321ed7249861ce7b89de4823d6980ec724be9f Mon Sep 17 00:00:00 2001 From: angularpackage Date: Thu, 10 Feb 2022 14:03:28 +0100 Subject: [PATCH 139/179] test(ValidationError): use const instead of let. --- src/test/validation-error.spec.ts | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/test/validation-error.spec.ts b/src/test/validation-error.spec.ts index c8c8732..80b4274 100644 --- a/src/test/validation-error.spec.ts +++ b/src/test/validation-error.spec.ts @@ -11,30 +11,15 @@ const toBe = new TestingToBeMatchers(); * Tests. */ testing.describe('[counter] ValidationError', () => { - let errorMessage: string; - let fix: string; - let id: string; - let problem: string; - let template: string; - let value: any; - let validationError: ValidationError; - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - problem = 'The value must be a string type.'; - template = `Problem(VE{id}): {problem}\nFix: {fix}`; - value = Symbol(123); + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const id = '427'; + const problem = 'The value must be a string type.'; + const template = `Problem(VE{id}): {problem}\nFix: {fix}`; + const value = Symbol(123); + let validationError = new ValidationError(problem, fix, id, template); - beforeEach(() => { - // Prepare the values. - fix = 'Provide string type value. Read more: https://duckduckgo.com/'; - id = '427'; - problem = 'The value must be a string type.'; - template = `Problem(VE{id}): {problem}\nFix: {fix}`; - value = Symbol(123); - validationError = new ValidationError(problem, fix, id, template); - }); + beforeEach(() => validationError = new ValidationError(problem, fix, id, template)); testing From 15a40397a17461bbd735079c3544c4c44f7b3f45 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 00:52:51 +0100 Subject: [PATCH 140/179] feat(CommonErrors): common object for the errors storage to throw them. --- src/lib/common-errors.class.ts | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/lib/common-errors.class.ts diff --git a/src/lib/common-errors.class.ts b/src/lib/common-errors.class.ts new file mode 100644 index 0000000..321bec3 --- /dev/null +++ b/src/lib/common-errors.class.ts @@ -0,0 +1,87 @@ +/** + * The `CommonErrors` object represents the errors storage of unique identification numbers. + */ +export abstract class CommonErrors { + /** + * Optional template of `string` type. + */ + public static template?: string; + + //#region protected instance accessors. + /** + * The `get` accessor returns the errors of `Map` type by returning the `#errors` property of a specified object. + */ + protected get errors(): Map { + return this.#errors; + } + //#endregion protected instance accessors. + + //#region private instance properties. + /** + * The collection of unique allowed identification numbers of generic type variable `Id`. + */ + #id?: Set; + + /** + * The errors storage of the `Map` type where the `key` is of the generic type variable `Id`. + */ + #errors: Map = new Map(); + //#endregion private instance properties. + + //#region constructor. + /** + * Creates an instance of the storage with unique identification numbers. + * @param id The unique identification numbers of the errors. + * @angularpackage + */ + constructor(...id: Id[]) { + Array.isArray(id) && (this.#id = new Set(id)); + } + //#endregion constructor. + + //#region instance public methods. + /** + * Deletes the error of a specified `id`. + * @param id The unique identification of a generic type variable `DeleteId` to remove the error from the object. + * @returns The return value is an instance of an `ValidationError`. + * @angularpackage + */ + public delete(id: ErrorId): this { + this.#errors.delete(id); + return this; + } + + /** + * The `has()` method checks whether the error of the given `id` exists. + * @param id The error identification number of generic type variable `ErrorId` to test for the presence of the error in the object. + * @returns The return value is a `boolean` indicating whether the error of the given `id` exists in the object. + * @angularpackage + */ + public has(id: ErrorId): boolean { + return this.#errors.has(id); + } + + /** + * Throws an error of the given `id` if the unique id was provided in the constructor and the errors was set. + * @param id The unique identification number of generic type variable `ErrorId` to obtain an error to throw. + * @angularpackage + */ + public throw(id: ErrorId): void { + if (this.isAllowedId(id)) { + throw this.errors.get(id); + } + } + + //#endregion instance public methods. + + //#region instance protected methods. + /** + * Checks whether the given identification number was provided in the constructor. + * @param id The error identification number of generic type variable `ErrorId` to test for its presence in the object. + * @angularpackage + */ + protected isAllowedId(id: ErrorId): boolean { + return this.#id ? this.#id.has(id) : false; + } + //#endregion instance protected methods. +} From 3826256ab2b7875caccebf19f5e6bb2c66a4ba60 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 00:53:29 +0100 Subject: [PATCH 141/179] refactor(Errors): completely changed object to only set and throw under the given identification number. --- src/lib/errors.class.ts | 392 +++------------------------------------- 1 file changed, 26 insertions(+), 366 deletions(-) diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts index 46d6c87..ba17865 100644 --- a/src/lib/errors.class.ts +++ b/src/lib/errors.class.ts @@ -1,389 +1,49 @@ +import { CommonErrors } from './common-errors.class'; import { Error } from './error.class'; -import { RangeError } from './range-error.class'; -import { TypeError } from './type-error.class'; -import { ValidationError } from './validation-error.class'; /** - * The `Errors` object represents the errors of the type, range, type, validation and simply error with multiple identification numbers. - * It holds properties and use them to define new error or throw it. + * The `Errors` object represents multiple identification numbers under which the `Error` object are stored. */ -export class Errors { - //#region public instance accessors. - /** - * - */ - public get define(): { - error: typeof errors.defineError; - rangeError: typeof errors.defineRangeError; - typeError: typeof errors.defineTypeError; - validationError: typeof errors.defineValidationError; - } { - const errors = this; - return Object.freeze({ - error: (id: DefineId): Error => - errors.defineError(id), - rangeError: ( - id: DefineId, - min?: number, - max?: number - ): RangeError => errors.defineRangeError(id, min, max), - typeError: ( - id: DefineId, - type?: string - ): TypeError => errors.defineTypeError(id, type), - validationError: ( - id: DefineId - ): ValidationError => errors.defineValidationError(id), - }); - } - - /** - * The get `accessor` obtains a possible solution to the described problem by returning the `#fix` property of a specified object. - * @returns The return value is the fix of a `string` type. - * @angularpackage - */ - public get fix(): string { - return this.#fix; - } - - /** - * The `get` accessor obtains the maximum range of number type if set, otherwise undefined. - * @returns The return value is the maximum range of a number type or undefined. - * @angularpackage - */ - public get max(): number | undefined { - return this.#max; - } - - /** - * The `get` accessor obtains the minimum range of number type if set, otherwise undefined. - * @returns The return value is the minimum range of a number type or undefined. - * @angularpackage - */ - public get min(): number | undefined { - return this.#min; - } - - /** - * The `get` accessor gets the problem by returning the `#problem` property of a specified object. - * @returns The return value is the problem of a `string` type. - * @angularpackage - */ - public get problem(): string { - return this.#problem; - } - - /** - * The `get` accessor obtains the maximum and maximum range in the form of an object. - * @returns The return value is an object that consists of the minimum and maximum range. - * @angularpackage - */ - public get range(): { min?: number; max?: number } { - return { - min: this.#min, - max: this.#max, - }; - } - - /** - * The `get` accessor gets the template of the error message by returning the `#template` property of a specified object. - * @returns The return value is the template of a `string` type. - * @angularpackage - */ - public get template(): string | undefined { - return this.#template; - } - - /** - * - */ - public get throw(): { - error: typeof t.throwError; - rangeError: typeof t.throwRangeError; - typeError: typeof t.throwTypeError; - validationError: typeof t.throwValidationError; - } { - const t = this; - return { - error: (id: ThrowId): void => t.throwError(id), - rangeError: ( - id: ThrowId, - min?: number, - max?: number - ): void => t.throwRangeError(id, min, max), - typeError: (id: ThrowId, type?: string): void => - t.throwTypeError(id, type), - validationError: (id: ThrowId): void => - t.throwValidationError(id), - }; - } - - /** - * The `get` accessor obtains the type of a `string` that causes an error to be thrown(or not thrown) if set, otherwise returns - * `undefined`. - * @returns The return value is the type of a `string` type. - * @angularpackage - */ - public get type(): string | undefined { - return this.#type; - } - //#endregion public instance accessors. - - //#region private instance properties. - /** - * The collection of unique allowed identification numbers of generic type variable `Id`. - */ - #allowedId?: Set; - - /** - * A privately stored possible solution to the described problem of a `string` type. - */ - #fix = ''; - - /** - * Private property of the maximum range of a `number` type that causes an error to be thrown(or not thrown). - */ - #max?: number; - - /** - * Private property of the minimum range of a `number` type that causes an error to be thrown(or not thrown). - */ - #min?: number; - - /** - * A privately stored problem of a `string` type. - */ - #problem = ''; - - /** - * A string-type privately stored template of the error message that contains replaceable required `{fix}`, `{problem}` and optional - * `{id}`, `{max}`, `{min}`, `{type}` tags. - */ - #template?: string; - - /** - * Private string-type property of the type that causes an error to be thrown(or not thrown). - */ - #type?: string; - //#endregion private instance properties. - +export class Errors extends CommonErrors { //#region constructor. /** - * - * @param problem - * @param fix - * @param allowedId + * Creates the `Errors` instance of unique identification numbers under which the `Error` objects are stored. + * @param id The unique identification numbers of an `array` of generic type variable `Id`. * @angularpackage */ - constructor( - problem: string, - fix: string, - allowedId: Id[], - additional?: { max?: number; min?: number; type?: string }, - template?: string, - ) { - Array.isArray(allowedId) && (this.#allowedId = new Set(allowedId)); - this.#fix = fix; - this.#problem = problem; - this.#max = additional?.max; - this.#min = additional?.min; - this.#template = template; - this.#type = additional?.type; + constructor(...id: Id[]) { + super(...id); } //#endregion constructor. //#region instance public methods. /** - * - * @param id - * @returns + * Sets the `Error` object with the given required `problem`, `fix`, `id` and optional `template` parameters. + * @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 `Id`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is equal to the static property `Errors.template`. + * @returns The return value is an instance of `Errors`. * @angularpackage */ - public defineError(id: DefineId, template = this.#template): Error { - return ( - this.isAllowedId(id), - new Error(this.#problem, this.#fix, id, template) - ); - } - - /** - * - * @param id - * @returns - * @angularpackage - */ - public defineRangeError( - id: DefineId, - min: number | undefined = this.#min, - max: number | undefined = this.#max, - template = this.#template - ): RangeError { - return ( - this.isAllowedId(id), - new RangeError(this.#problem, this.#fix, id, min, max, template) - ); - } - - /** - * - * @param id - * @returns - * @angularpackage - */ - public defineTypeError( - id: DefineId, - type: string | undefined = this.#type, - template = this.#template - ): TypeError { - return ( - this.isAllowedId(id), - new TypeError(this.#problem, this.#fix, id, type, template) - ); - } - - /** - * - * @param id - * @returns - * @angularpackage - */ - public defineValidationError( - id: DefineId, - template = this.#template - ): ValidationError { - return ( - this.isAllowedId(id), - new ValidationError(this.#problem, this.#fix, id, template) - ); - } - - /** - * Sets the fix a possible solution to the described problem. - * @param fix A possible solution to the described problem of a `string` type. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setFix(fix: string): this { - this.#fix = fix; - return this; - } - - public setMax(max?: number): this { - this.#max = max; - return this; - } - - public setMin(min?: number): this { - this.#min = min; - return this; - } - - /** - * Sets description problem of a validation. - * @param problem Description of validation problem of a `string` type. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setProblem(problem: string): this { - this.#problem = problem; - return this; - } - - /** - * Sets the template of validation error message. - * @param template A message template of `string` type with replaceable `{problem}`, `{fix}` and optional `{id}` tags. - * @returns The return value is an instance of an `ValidationError`. - * @angularpackage - */ - public setTemplate(template?: string): this { - this.#template = template; - return this; - } - - /** - * - * @param type - * @returns - * @angularpackage - */ - public setType(type?: string): this { - this.#type = type; + public set( + problem: string, + fix: string, + id: ErrorId, + template = Errors.template + ): this { + this.isAllowedId(id) && + this.errors.set(id, new Error(problem, fix, id, template)); return this; } /** - * - * @param id + * 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 throwError( - id: ThrowId, - template = this.#template - ): void { - throw ( - (this.isAllowedId(id), new Error(this.#problem, this.#fix, id, template)) - ); - } - - /** - * - * @param id - * @angularpackage - */ - public throwRangeError( - id: ThrowId, - min: number | undefined = this.#min, - max: number | undefined = this.#max, - template = this.#template - ): void { - throw ( - (this.isAllowedId(id), - new RangeError(this.#problem, this.#fix, id, min, max, template)) - ); - } - - /** - * - * @param id - * @angularpackage - */ - public throwTypeError( - id: ThrowId, - type: string | undefined = this.#type, - template = this.#template - ): void { - throw ( - (this.isAllowedId(id), - new TypeError(this.#problem, this.#fix, type, id, template)) - ); - } - - /** - * Throws an error of `ValidationError` with the message built from the stored `fix`, `problem`, and optional `id` on the stored - * `template`. - * @param id - * @angularpackage - */ - public throwValidationError(id: ThrowId, template = this.#template): void { - throw ( - (this.isAllowedId(id), - new ValidationError(this.#problem, this.#fix, id, template)) - ); + public toObject(): { [Key in Id]: Error } { + return Object.fromEntries(this.errors.entries()) as any; } //#endregion instance public methods. - - //#region instance private methods. - /** - * - * @param id - * @angularpackage - */ - private isAllowedId(id: CheckId): void { - if (this.#allowedId && this.#allowedId.has(id) === false) { - throw new RangeError( - `The provided identification ${id} is not allowed.`, - `Set allowed id: ${id} in the allowedId parameter of the constructor.` - ); - } - } - //#endregion instance private methods. } From bae824c003190d38feffe6acd2b59f778a111bff Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 00:54:19 +0100 Subject: [PATCH 142/179] fix(ValidationError): add generic type `Id` variable to the `CommonError`. --- src/lib/validation-error.class.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 50edc4c..4c020f1 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -4,7 +4,7 @@ import { CommonError } from './common-error.class'; * proper type(but not exclusively) with the message built from the described problem and its solution, along with additional identification * on the given or stored template. */ -export class ValidationError extends CommonError { +export class ValidationError extends CommonError { //#region public instance accessors. /** * The `get` accessor obtains error name of a `string` type, set to 'ValidationError' that is being thrown. From 7a75c47e7bd5d9d0bebf0dc14cdef2e6579c6978 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 00:54:58 +0100 Subject: [PATCH 143/179] test(Error): fix the static template wasn't back to original properly. --- src/test/error.spec.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/error.spec.ts b/src/test/error.spec.ts index f246482..a32bf01 100644 --- a/src/test/error.spec.ts +++ b/src/test/error.spec.ts @@ -19,10 +19,7 @@ testing.describe('[counter] Error', () => { const template = `Problem(VE{id}): {problem}\nFix: {fix}`; let error = new Error(problem, fix, id, template); - beforeEach(() => ( - Error.template = `Problem{id}: {problem} => Fix: {fix}`, - error = new Error(problem, fix, id, template) - )); + beforeEach(() => error = new Error(problem, fix, id, template)); testing @@ -35,6 +32,7 @@ testing.describe('[counter] Error', () => { expect(Error.template).toEqual(`Problem{id}: {problem} => Fix: {fix}`); Error.template = `{problem} => Fix: {fix} of {id}`; expect(Error.template).toEqual(`{problem} => Fix: {fix} of {id}`); + Error.template = `Problem{id}: {problem} => Fix: {fix}`; }); }) From 261cf1ec8727d76cfe106d52b177c936a4be2940 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 00:55:17 +0100 Subject: [PATCH 144/179] test(RangeError): fix the static template wasn't back to original properly. --- src/test/range-error.spec.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/range-error.spec.ts b/src/test/range-error.spec.ts index 6297904..5e904f4 100644 --- a/src/test/range-error.spec.ts +++ b/src/test/range-error.spec.ts @@ -22,10 +22,7 @@ testing.describe('[counter] RangeError', () => { let rangeError = new RangeError(problem, fix, id, min, max, template); - beforeEach(() => ( - RangeError.template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`, - rangeError = new RangeError(problem, fix, id, min, max, template) - )); + beforeEach(() => rangeError = new RangeError(problem, fix, id, min, max, template)); testing @@ -37,6 +34,7 @@ testing.describe('[counter] RangeError', () => { expect(RangeError.template).toEqual(`Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`); RangeError.template = `{problem} => Fix: {fix} of {id}`; expect(RangeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); + RangeError.template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`; }); }) From da8cee40ef83e7bdc7938028beb62ed7e8b1b7e0 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 00:55:30 +0100 Subject: [PATCH 145/179] test(TypeError): fix the static template wasn't back to original properly. --- src/test/type-error.spec.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/type-error.spec.ts b/src/test/type-error.spec.ts index 09b30e6..7b31d0c 100644 --- a/src/test/type-error.spec.ts +++ b/src/test/type-error.spec.ts @@ -18,12 +18,10 @@ testing.describe('[counter] TypeError', () => { const problem = 'The value must be a string type.'; const template = `Problem(VE{id}): {problem} {type}\nFix: {fix}`; const type = 'Symbol'; + let typeError = new TypeError(problem, fix, id, type, template); - beforeEach(() => ( - TypeError.template = `Problem{id}: {problem} => Fix: {fix} must be of the {type}`, - typeError = new TypeError(problem, fix, id, type, template) - )); + beforeEach(() => typeError = new TypeError(problem, fix, id, type, template)); testing @@ -36,6 +34,7 @@ testing.describe('[counter] TypeError', () => { expect(TypeError.template).toEqual(`Problem{id}: {problem} => Fix: {fix} must be of the {type}`); TypeError.template = `{problem} => Fix: {fix} of {id}`; expect(TypeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); + TypeError.template = `Problem{id}: {problem} => Fix: {fix} must be of the {type}`; }); }) From ef2eca8c0b749c1a31bd7d54907fc35d071a7f87 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 01:15:09 +0100 Subject: [PATCH 146/179] chore(index, api): add export `RangeErrors` `TypeErrors` `ValidationErrors`. --- src/lib/index.ts | 3 +++ src/public-api.ts | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 006d890..9e62f74 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -2,5 +2,8 @@ export { CommonError } from './common-error.class'; export { Error } from './error.class'; export { Errors } from './errors.class'; export { RangeError } from './range-error.class'; +export { RangeErrors } from './range-errors.class'; export { TypeError } from './type-error.class'; +export { TypeErrors } from './type-errors.class'; export { ValidationError } from './validation-error.class'; +export { ValidationErrors } from './validation-errors.class'; diff --git a/src/public-api.ts b/src/public-api.ts index dad9b1b..8804519 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -7,6 +7,9 @@ export { Error, Errors, RangeError, + RangeErrors, TypeError, - ValidationError + TypeErrors, + ValidationError, + ValidationErrors } from './lib'; From f8afe6185bc678149fb232ccc501b372e72245cf Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 18:55:19 +0100 Subject: [PATCH 147/179] docs(Errors): update main description. --- src/lib/errors.class.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts index ba17865..2089d61 100644 --- a/src/lib/errors.class.ts +++ b/src/lib/errors.class.ts @@ -1,7 +1,8 @@ import { CommonErrors } from './common-errors.class'; import { Error } from './error.class'; /** - * The `Errors` object represents multiple identification numbers under which the `Error` object are stored. + * The `Errors` is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the + * `Error` type are prepared to throw. */ export class Errors extends CommonErrors { //#region constructor. From 149e398d50132e3e19105a6c6db4905aec62710c Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 18:55:44 +0100 Subject: [PATCH 148/179] refactor(CommonError): remove the default value for the generic type variable `Id`. --- src/lib/common-error.class.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index edf4f88..da05c32 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -2,7 +2,7 @@ * The `CommonError` abstract object to throw an identified error with a solution to the described problem, additional type, and range built * on the template. */ -export abstract class CommonError extends Error { +export abstract class CommonError extends Error { //#region public static properties. /** * A template of the error message of `string` type with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{link}`, `{max}`, From 95520f3ea5c95159be2f38d7a6080c611b100620 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 18:56:48 +0100 Subject: [PATCH 149/179] docs(CommonErrors): update main description, constructor, `delete()` `has()` `throw()` methods. --- src/lib/common-errors.class.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lib/common-errors.class.ts b/src/lib/common-errors.class.ts index 321bec3..60a5935 100644 --- a/src/lib/common-errors.class.ts +++ b/src/lib/common-errors.class.ts @@ -1,5 +1,5 @@ /** - * The `CommonErrors` object represents the errors storage of unique identification numbers. + * The `CommonErrors` object represents the storage of errors with unique identification numbers. */ export abstract class CommonErrors { /** @@ -10,6 +10,8 @@ export abstract class CommonErrors { //#region protected instance accessors. /** * The `get` accessor returns the errors of `Map` type by returning the `#errors` property of a specified object. + * @returns The return value is the `Map` object of errors. + * @angularpackage */ protected get errors(): Map { return this.#errors; @@ -30,8 +32,9 @@ export abstract class CommonErrors { //#region constructor. /** - * Creates an instance of the storage with unique identification numbers. - * @param id The unique identification numbers of the errors. + * Creates an instance of the errors storage with unique identification numbers. + * @param id A rest parameter of generic type variable `Id` indicates unique identification numbers under which the errors are stored in + * the object. * @angularpackage */ constructor(...id: Id[]) { @@ -41,8 +44,8 @@ export abstract class CommonErrors { //#region instance public methods. /** - * Deletes the error of a specified `id`. - * @param id The unique identification of a generic type variable `DeleteId` to remove the error from the object. + * Deletes the error of a specified `id` from the object. + * @param id The unique identification of a generic type variable `ErrorId` to remove the error from the object. * @returns The return value is an instance of an `ValidationError`. * @angularpackage */ @@ -52,7 +55,7 @@ export abstract class CommonErrors { } /** - * The `has()` method checks whether the error of the given `id` exists. + * The `has()` method checks whether the error of the given `id` exists in a specified object. * @param id The error identification number of generic type variable `ErrorId` to test for the presence of the error in the object. * @returns The return value is a `boolean` indicating whether the error of the given `id` exists in the object. * @angularpackage @@ -62,7 +65,7 @@ export abstract class CommonErrors { } /** - * Throws an error of the given `id` if the unique id was provided in the constructor and the errors was set. + * Throws an error of the given `id` if the unique id was provided in the constructor. * @param id The unique identification number of generic type variable `ErrorId` to obtain an error to throw. * @angularpackage */ From 72b8582f848075c27bd97ae8a05bed29c287ffd9 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sat, 12 Feb 2022 18:57:46 +0100 Subject: [PATCH 150/179] feat(RangeErrors): add feature to store errors of the `RangeError` type and throw on demand. --- src/lib/range-errors.class.ts | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/lib/range-errors.class.ts diff --git a/src/lib/range-errors.class.ts b/src/lib/range-errors.class.ts new file mode 100644 index 0000000..bff0893 --- /dev/null +++ b/src/lib/range-errors.class.ts @@ -0,0 +1,46 @@ +import { CommonErrors } from './common-errors.class'; +import { RangeError } from './range-error.class'; +/** + * The `Errors` is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the + * `RangeError` type are prepared to throw. + */ +export class RangeErrors extends CommonErrors { + //#region constructor. + /** + * Creates the `RangeErrors` instance of unique identification numbers under which the `RangeError` objects are stored. + * @param id The unique identification numbers of an `Array` of generic type variable `Id`. + * @angularpackage + */ + constructor(...id: Id[]) { + super(...id); + } + //#endregion constructor. + + //#region instance public methods. + /** + * Sets the range error with the message built from the given required `problem`, `fix`, `id` and optional `min`, `max` on the given or + * stored `template`. + * @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 `Id`. + * @param min The optional minimum range of `number` type that causes an error to be thrown(or not thrown). + * @param max The optional maximum range of `number` type that causes an error to be thrown(or not thrown). + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and + * `{type}` tags. By default, the value is equal to the static property `RangeErrors.template`. + * @returns The return value is an instance of `RangeErrors`. + * @angularpackage + */ + public set( + problem: string, + fix: string, + id: ErrorId, + min?: number, + max?: number, + template = RangeErrors.template + ): this { + this.isAllowedId(id) && + this.errors.set(id, new RangeError(problem, fix, id, min, max, template)); + return this; + } + //#endregion instance public methods. +} From 576bce479c4a42266b3f217bc071f6c1dda12cea Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 13 Feb 2022 01:50:34 +0100 Subject: [PATCH 151/179] docs(Errors): update `set()` `toObject()` and `constructor()` descriptions. --- src/lib/errors.class.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts index 2089d61..4fb7511 100644 --- a/src/lib/errors.class.ts +++ b/src/lib/errors.class.ts @@ -8,7 +8,8 @@ export class Errors extends CommonErrors { //#region constructor. /** * Creates the `Errors` instance of unique identification numbers under which the `Error` objects are stored. - * @param id The unique identification numbers of an `array` of generic type variable `Id`. + * @param id A rest parameter of generic type variable `Id` indicates unique identification numbers under which the errors are stored in + * the object. * @angularpackage */ constructor(...id: Id[]) { @@ -18,9 +19,10 @@ export class Errors extends CommonErrors { //#region instance public methods. /** - * Sets the `Error` object with the given required `problem`, `fix`, `id` and optional `template` parameters. - * @param problem Description of the problem of a string type. - * @param fix A solution to the given `problem` of a string type. + * Sets the `Error` object with the message built from the given required `problem`, `id`, `fix` 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 `Id`. * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and * `{type}` tags. By default, the value is equal to the static property `Errors.template`. @@ -40,7 +42,7 @@ export class Errors extends CommonErrors { /** * 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. + * @returns The return value is an `object` of set errors. * @angularpackage */ public toObject(): { [Key in Id]: Error } { From d4ae81d4b6ec379768c84d21f85dbff4a9275e15 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 13 Feb 2022 01:51:44 +0100 Subject: [PATCH 152/179] refactor(RangeErrors): add `toObject()` instance method and update jsdoc of the `set()` `constructor()`. --- src/lib/range-errors.class.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/lib/range-errors.class.ts b/src/lib/range-errors.class.ts index bff0893..fd7c75e 100644 --- a/src/lib/range-errors.class.ts +++ b/src/lib/range-errors.class.ts @@ -1,14 +1,15 @@ import { CommonErrors } from './common-errors.class'; import { RangeError } from './range-error.class'; /** - * The `Errors` is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the - * `RangeError` type are prepared to throw. + * The `RangeErrors` is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of + * the `RangeError` type are prepared to throw. */ export class RangeErrors extends CommonErrors { //#region constructor. /** * Creates the `RangeErrors` instance of unique identification numbers under which the `RangeError` objects are stored. - * @param id The unique identification numbers of an `Array` of generic type variable `Id`. + * @param id A rest parameter of generic type variable `Id` indicates unique identification numbers under which the `RangeError` objects + * are stored. * @angularpackage */ constructor(...id: Id[]) { @@ -18,8 +19,8 @@ export class RangeErrors extends CommonErrors { //#region instance public methods. /** - * Sets the range error with the message built from the given required `problem`, `fix`, `id` and optional `min`, `max` on the given or - * stored `template`. + * Sets the `RangeError` object with the message built from the given required `problem`, `fix`, `id` and optional `min`, `max` on the + * given or stored `template`. * @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 `Id`. @@ -42,5 +43,14 @@ export class RangeErrors extends CommonErrors { this.errors.set(id, new RangeError(problem, fix, id, min, max, 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]: RangeError } { + return Object.fromEntries(this.errors.entries()) as any; + } //#endregion instance public methods. } From ff2feebe48fdb1b3f8bfe3c58cedc09c8b6402df Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 13 Feb 2022 02:57:45 +0100 Subject: [PATCH 153/179] feat(TypeErrors): add feature to store errors of the `TypeError` type and throw on demand. --- src/lib/type-errors.class.ts | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/lib/type-errors.class.ts diff --git a/src/lib/type-errors.class.ts b/src/lib/type-errors.class.ts new file mode 100644 index 0000000..672c1ec --- /dev/null +++ b/src/lib/type-errors.class.ts @@ -0,0 +1,55 @@ +import { CommonErrors } from './common-errors.class'; +import { TypeError } from './type-error.class'; +/** + * The `TypeErrors` is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of + * the `TypeError` type are prepared to throw. + */ +export class TypeErrors extends CommonErrors { + //#region constructor. + /** + * Creates the `TypeErrors` instance of unique identification numbers under which the `TypeError` objects are stored. + * @param id A rest parameter of generic type variable `Id` indicates unique identification numbers under which the `TypeError` objects + * are stored. + * @angularpackage + */ + constructor(...id: Id[]) { + super(...id); + } + //#endregion constructor. + + //#region instance public methods. + /** + * Sets the `TypeError` object with the message built from the given required `problem`, `fix`, `id` and optional `type` 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 type The optional type of `string` type that causes an error to be thrown(or not thrown). + * @param template A template of error message with the replaceable `{problem}`, `{fix}`, `{id}`, and optional `{type}` tags. By default, + * the value is equal to the static property `RangeErrors.template`. + * @returns The return value is an instance of `TypeErrors`. + * @angularpackage + */ + public set( + problem: string, + fix: string, + id: ErrorId, + type?: string, + template = TypeErrors.template + ): this { + this.isAllowedId(id) && + this.errors.set(id, new TypeError(problem, fix, id, type, 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]: TypeError } { + return Object.fromEntries(this.errors.entries()) as any; + } + + //#endregion instance public methods. +} From 43a01628af2a73aa428d7d6bcb48e9c3a1c755f3 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 13 Feb 2022 19:23:33 +0100 Subject: [PATCH 154/179] feat(ValidationErrors): add feature to store errors of the `ValidationError` type and throw on demand. --- src/lib/validation-errors.class.ts | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/lib/validation-errors.class.ts diff --git a/src/lib/validation-errors.class.ts b/src/lib/validation-errors.class.ts new file mode 100644 index 0000000..b2c03f1 --- /dev/null +++ b/src/lib/validation-errors.class.ts @@ -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 extends CommonErrors { + //#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( + 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 } { + return Object.fromEntries(this.errors.entries()) as any; + } + + //#endregion instance public methods. +} From 85da86e2ec391183e0265569262c41ced37636bb Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 13 Feb 2022 19:25:43 +0100 Subject: [PATCH 155/179] docs(RangeErrors): update `set()` method decription. --- src/lib/range-errors.class.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/range-errors.class.ts b/src/lib/range-errors.class.ts index fd7c75e..4917d9a 100644 --- a/src/lib/range-errors.class.ts +++ b/src/lib/range-errors.class.ts @@ -20,14 +20,14 @@ export class RangeErrors extends CommonErrors { //#region instance public methods. /** * Sets the `RangeError` object with the message built from the given required `problem`, `fix`, `id` and optional `min`, `max` on the - * given or stored `template`. + * 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 `Id`. + * @param id The unique identification to the given `problem` of generic type variable `ErrorId`. * @param min The optional minimum range of `number` type that causes an error to be thrown(or not thrown). * @param max The optional maximum range of `number` type that causes an error to be thrown(or not thrown). - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is equal to the static property `RangeErrors.template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}`, `{id}`, and optional `{max}`, `{min}` tags. By + * default, the value is equal to the static property `RangeErrors.template`. * @returns The return value is an instance of `RangeErrors`. * @angularpackage */ From 1241b7aa94577ce0b74f3b946b03a7783bf84742 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Sun, 13 Feb 2022 19:26:04 +0100 Subject: [PATCH 156/179] docs(Errors): update `set()` method instance. --- src/lib/errors.class.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts index 4fb7511..64a682f 100644 --- a/src/lib/errors.class.ts +++ b/src/lib/errors.class.ts @@ -19,13 +19,13 @@ export class Errors extends CommonErrors { //#region instance public methods. /** - * Sets the `Error` object with the message built from the given required `problem`, `id`, `fix` on the given or stored `template` under + * Sets the `Error` 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 `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is equal to the static property `Errors.template`. + * @param id The unique identification to the given `problem` of generic type variable `ErrorId`. + * @param template A template of the error message with a replaceable `{problem}`, `{fix}`, `{id}` tags. By default, the value is equal to + * the static property `Errors.template`. * @returns The return value is an instance of `Errors`. * @angularpackage */ From ca45f34ab99b0f670fa6bdf2ac51f96d59f6e969 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 11:02:21 +0100 Subject: [PATCH 157/179] docs(CommonError): update `#id` and `#link` property description. --- src/lib/common-error.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index da05c32..e5c1d96 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -74,12 +74,12 @@ export abstract class CommonError extends Error { #fix: string; /** - * A privately stored unique identification to the described problem of generic type variable `Id`. + * Optional privately stored unique identification of the described problem of generic type variable `Id`. */ #id?: Id; /** - * A privately stored link redirects to read more about the thrown error. + * The optional privately stored link redirects to read more about the thrown error of `string` type. */ #link?: string; From f290e532efbae762d83c56b811448d8f24e91e06 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 16:22:00 +0100 Subject: [PATCH 158/179] docs(CommonError): update `#`link` description. --- src/lib/common-error.class.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/common-error.class.ts b/src/lib/common-error.class.ts index e5c1d96..407b2ad 100644 --- a/src/lib/common-error.class.ts +++ b/src/lib/common-error.class.ts @@ -79,7 +79,7 @@ export abstract class CommonError extends Error { #id?: Id; /** - * The optional privately stored link redirects to read more about the thrown error of `string` type. + * The optional privately stored link of `string` type redirects to read more about the thrown error. */ #link?: string; From 3b5a04eb9d7ae5d174c8038ca7e9a813f07fae75 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:00:02 +0100 Subject: [PATCH 159/179] docs(Error): update static `define()` method parameter `template` description. --- src/lib/error.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index 5d36509..af85623 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -31,8 +31,8 @@ export class Error extends CommonError { * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is picked from the static property `template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}` tags. By default, the value + * is picked from the static property `template`. * @returns The return value is a new instance of the `Error` with the message built from the given required `problem`, `fix` and optional * `id` on the `template`. * @angularpackage From 9a0ff8eede7549361dc125c1e29df35e99faa619 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:02:35 +0100 Subject: [PATCH 160/179] refactor(Errors): update the `get()` and change the method name to `getErrors()`. Change the return type `Id` of the error to `Key` in the method `getErrors()`. --- src/lib/errors.class.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts index 64a682f..cc1eb1c 100644 --- a/src/lib/errors.class.ts +++ b/src/lib/errors.class.ts @@ -18,9 +18,29 @@ export class Errors extends CommonErrors { //#endregion constructor. //#region instance public methods. + /** + * Returns the `Error` instance of the given unique identification `id` if set, otherwise `undefined`. + * @param id The unique identification number of generic type variable `ErrorId` to pick an error from the object. + * @returns The return value is the `Error` instance of the given `id` if set, otherwise undefined. + * @angularpackage + */ + public get(id: ErrorId): Error | undefined { + return this.errors.get(id); + } + + /** + * The method returns the object of set errors, where the key is a unique identification. + * @returns The return value is an `object` of set errors. + * @angularpackage + */ + public getErrors(): { [Key in Id]: Error } { + return Object.fromEntries(this.errors.entries()) as any; + } + /** * Sets the `Error` object with the message built from the given required `problem`, `fix`, `id` on the given or stored `template` under * the given `id`. + * ! The error is not set, if the given `id` was not provided in the constructor. * @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`. @@ -39,14 +59,5 @@ export class Errors extends CommonErrors { this.errors.set(id, new Error(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]: Error } { - return Object.fromEntries(this.errors.entries()) as any; - } //#endregion instance public methods. } From c3771bafe52353dc06ad0baf6e1ac532557652fc Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:03:05 +0100 Subject: [PATCH 161/179] refactor(RangeErrors): update the `get()` and change the method name to `getErrors()`. Change the return type `Id` of the error to `Key` in the method `getErrors()`. --- src/lib/range-errors.class.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/lib/range-errors.class.ts b/src/lib/range-errors.class.ts index 4917d9a..9630e0f 100644 --- a/src/lib/range-errors.class.ts +++ b/src/lib/range-errors.class.ts @@ -18,6 +18,25 @@ export class RangeErrors extends CommonErrors { //#endregion constructor. //#region instance public methods. + /** + * Returns the `RangeError` instance of the given unique identification `id` if set, otherwise `undefined`. + * @param id The unique identification number of generic type variable `ErrorId` to pick an error from the object. + * @returns The return value is the `RangeError` instance of the given `id` if set, otherwise undefined. + * @angularpackage + */ + public get(id: ErrorId): RangeError | undefined { + return this.errors.get(id); + } + + /** + * The method returns the object of set type errors, where the key is a unique identification. + * @returns The return value is an `object` of set type errors. + * @angularpackage + */ + public getErrors(): { [Key in Id]: RangeError } { + return Object.fromEntries(this.errors.entries()) as any; + } + /** * Sets the `RangeError` object with the message built from the given required `problem`, `fix`, `id` and optional `min`, `max` on the * given or stored `template` under the given `id`. @@ -43,14 +62,5 @@ export class RangeErrors extends CommonErrors { this.errors.set(id, new RangeError(problem, fix, id, min, max, 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]: RangeError } { - return Object.fromEntries(this.errors.entries()) as any; - } //#endregion instance public methods. } From 8f7f0db03a6e378a1813e068c140e43c34bc8d88 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:03:54 +0100 Subject: [PATCH 162/179] refactor(TypeErrors): update the `get()` and change the method name to `getErrors()`. Change the return type `Id` of the error to `Key` in the method `getErrors()`. --- src/lib/type-errors.class.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/lib/type-errors.class.ts b/src/lib/type-errors.class.ts index 672c1ec..6b88e35 100644 --- a/src/lib/type-errors.class.ts +++ b/src/lib/type-errors.class.ts @@ -18,6 +18,25 @@ export class TypeErrors extends CommonErrors { //#endregion constructor. //#region instance public methods. + /** + * Returns the `TypeError` instance of the given unique identification `id` if set, otherwise `undefined`. + * @param id The unique identification number of generic type variable `ErrorId` to pick an error from the object. + * @returns The return value is the `TypeError` instance of the given `id` if set, otherwise undefined. + * @angularpackage + */ + public get(id: ErrorId): TypeError | undefined { + return this.errors.get(id); + } + + /** + * The method returns the object of set type errors, where the key is a unique identification. + * @returns The return value is an `object` of set errors. + * @angularpackage + */ + public getErrors(): { [Key in Id]: TypeError } { + return Object.fromEntries(this.errors.entries()) as any; + } + /** * Sets the `TypeError` object with the message built from the given required `problem`, `fix`, `id` and optional `type` on the given or * stored `template` under the given `id`. @@ -41,15 +60,5 @@ export class TypeErrors extends CommonErrors { this.errors.set(id, new TypeError(problem, fix, id, type, 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]: TypeError } { - return Object.fromEntries(this.errors.entries()) as any; - } - //#endregion instance public methods. } From ba36b5e8337a211ebe83f7179efbc36eae03652c Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:05:04 +0100 Subject: [PATCH 163/179] refactor(ValidationErrors): add the `get()` method and change the method name `toObject()` to `getErrors()`. Change the return type `Id` of the error to `Key` in the method `getErrors()`. --- src/lib/validation-errors.class.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/lib/validation-errors.class.ts b/src/lib/validation-errors.class.ts index b2c03f1..cfd5c67 100644 --- a/src/lib/validation-errors.class.ts +++ b/src/lib/validation-errors.class.ts @@ -18,6 +18,25 @@ export class ValidationErrors extends CommonErrors { //#endregion constructor. //#region instance public methods. + /** + * Returns the `ValidationError` instance of the given unique identification `id` if set, otherwise `undefined`. + * @param id The unique identification number of generic type variable `ErrorId` to pick an error from the object. + * @returns The return value is the `ValidationError` instance of the given `id` if set, otherwise undefined. + * @angularpackage + */ + public get(id: ErrorId): ValidationError | undefined { + return this.errors.get(id); + } + + /** + * 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 getErrors(): { [Key in Id]: ValidationError } { + return Object.fromEntries(this.errors.entries()) as any; + } + /** * 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`. @@ -39,15 +58,5 @@ export class ValidationErrors extends CommonErrors { 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 } { - return Object.fromEntries(this.errors.entries()) as any; - } - //#endregion instance public methods. } From 67e01fb15b9e45e012a3f000490780e45c18351f Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:05:50 +0100 Subject: [PATCH 164/179] docs(TypeError): update static `define()` parameter `template` description. --- src/lib/type-error.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/type-error.class.ts b/src/lib/type-error.class.ts index 5b45f6e..d805f6c 100644 --- a/src/lib/type-error.class.ts +++ b/src/lib/type-error.class.ts @@ -57,8 +57,8 @@ export class TypeError< * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param type The optional type of generic type variable `Type` that causes an error to be thrown(or not thrown). - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{min}`, `{max}` and - * `{type}` tags. By default, the value is picked from the static property `TypeError.template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{type}` tags. By default, + * the value is picked from the static property `TypeError.template`. * @returns The return value is a new instance of the `TypeError` with the message built from the given required `problem`, `fix` and * optional `id`, `type` on the given or stored `template`. * @angularpackage From b9411d4354e981ee56d2c9ae5f3c6538e25cc627 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 17:06:58 +0100 Subject: [PATCH 165/179] docs: update `set()` description. --- src/lib/range-errors.class.ts | 1 + src/lib/type-errors.class.ts | 1 + src/lib/validation-errors.class.ts | 1 + 3 files changed, 3 insertions(+) diff --git a/src/lib/range-errors.class.ts b/src/lib/range-errors.class.ts index 9630e0f..6d3346c 100644 --- a/src/lib/range-errors.class.ts +++ b/src/lib/range-errors.class.ts @@ -40,6 +40,7 @@ export class RangeErrors extends CommonErrors { /** * Sets the `RangeError` object with the message built from the given required `problem`, `fix`, `id` and optional `min`, `max` on the * given or stored `template` under the given `id`. + * ! The error is not set, if the given `id` was not provided in the constructor. * @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`. diff --git a/src/lib/type-errors.class.ts b/src/lib/type-errors.class.ts index 6b88e35..c9c6c5b 100644 --- a/src/lib/type-errors.class.ts +++ b/src/lib/type-errors.class.ts @@ -40,6 +40,7 @@ export class TypeErrors extends CommonErrors { /** * Sets the `TypeError` object with the message built from the given required `problem`, `fix`, `id` and optional `type` on the given or * stored `template` under the given `id`. + * ! The error is not set, if the given `id` was not provided in the constructor. * @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`. diff --git a/src/lib/validation-errors.class.ts b/src/lib/validation-errors.class.ts index cfd5c67..006ba9d 100644 --- a/src/lib/validation-errors.class.ts +++ b/src/lib/validation-errors.class.ts @@ -40,6 +40,7 @@ export class ValidationErrors extends CommonErrors { /** * 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`. + * ! The error is not set, if the given `id` was not provided in the constructor. * @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`. From 070a44290efa12c2d8ade64f499a58ab840be2af Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 18:30:39 +0100 Subject: [PATCH 166/179] refactor(Errors): add undefined to the return type of the `getErrors()` method and update jsdoc. --- src/lib/errors.class.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/errors.class.ts b/src/lib/errors.class.ts index cc1eb1c..4c92821 100644 --- a/src/lib/errors.class.ts +++ b/src/lib/errors.class.ts @@ -21,7 +21,7 @@ export class Errors extends CommonErrors { /** * Returns the `Error` instance of the given unique identification `id` if set, otherwise `undefined`. * @param id The unique identification number of generic type variable `ErrorId` to pick an error from the object. - * @returns The return value is the `Error` instance of the given `id` if set, otherwise undefined. + * @returns The return value is the `Error` instance of the given `id` if set, otherwise `undefined`. * @angularpackage */ public get(id: ErrorId): Error | undefined { @@ -29,11 +29,11 @@ export class Errors extends CommonErrors { } /** - * The method returns the object of set errors, where the key is a unique identification. + * Returns an `object` of set errors, where the key is a unique identification. * @returns The return value is an `object` of set errors. * @angularpackage */ - public getErrors(): { [Key in Id]: Error } { + public getErrors(): { [Key in Id]: Error | undefined } { return Object.fromEntries(this.errors.entries()) as any; } From 8cdd2c3e41993563850dadbf10eb10efd5849485 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 22:52:05 +0100 Subject: [PATCH 167/179] refactor(RangeError): change the template to have {min} and {max} in the fix section. --- src/lib/range-error.class.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib/range-error.class.ts b/src/lib/range-error.class.ts index cc0807b..afd53ff 100644 --- a/src/lib/range-error.class.ts +++ b/src/lib/range-error.class.ts @@ -11,9 +11,9 @@ export class RangeError< > extends CommonError { /** * A template of the error message of `string` type with the replaceable required `{problem}`, `{fix}` and optional `{id}`, `{max}`, - * `{min}`, `{type}` tags. By default, it's set to `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`. + * `{min}` tags. By default, it's set to `Problem{id}: {problem} => Fix: {fix} between {min} and {max}`. */ - public static template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`; + public static template = `Problem{id}: {problem} => Fix: {fix} between {min} and {max}`; //#region public instance accessors. /** @@ -90,9 +90,8 @@ export class RangeError< * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param min The optional minimum range of generic type variable `Min` that causes an error to be thrown(or not thrown). * @param max The optional maximum range of generic type variable `Max` that causes an error to be thrown(or not thrown). - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is picked from the static - * property `RangeError.template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and tags. + * By default, the value is picked from the static property `RangeError.template`. * @returns The return value is a new instance of the `RangeError` with the message built from the given required `problem`, `fix` and * optional `id`, `min`, `max` on the given or stored `template`. * @angularpackage From dd5147f7f10aafc96027003336841bf3c492e952 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 22:53:19 +0100 Subject: [PATCH 168/179] refactor(RangeErrors): add `undefined` to the return type of `getErrors()`. Update description of the `getErrors()`. --- src/lib/range-errors.class.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/range-errors.class.ts b/src/lib/range-errors.class.ts index 6d3346c..b6e9c13 100644 --- a/src/lib/range-errors.class.ts +++ b/src/lib/range-errors.class.ts @@ -29,11 +29,11 @@ export class RangeErrors extends CommonErrors { } /** - * The method returns the object of set type errors, where the key is a unique identification. - * @returns The return value is an `object` of set type errors. + * Returns the object of set range errors, where the key is a unique identification. + * @returns The return value is an `object` of set range errors. * @angularpackage */ - public getErrors(): { [Key in Id]: RangeError } { + public getErrors(): { [Key in Id]: RangeError | undefined } { return Object.fromEntries(this.errors.entries()) as any; } From 007764b8b2e587bc9e818d0eeffc5ae23f598507 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 22:53:59 +0100 Subject: [PATCH 169/179] refactor(TypeErrors): add `undefined` to the return type of `getErrors()`. Update description @returns of the `getErrors()`. --- src/lib/type-errors.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/type-errors.class.ts b/src/lib/type-errors.class.ts index c9c6c5b..ad33812 100644 --- a/src/lib/type-errors.class.ts +++ b/src/lib/type-errors.class.ts @@ -30,10 +30,10 @@ export class TypeErrors extends CommonErrors { /** * The method returns the object of set type errors, where the key is a unique identification. - * @returns The return value is an `object` of set errors. + * @returns The return value is an `object` of set type errors. * @angularpackage */ - public getErrors(): { [Key in Id]: TypeError } { + public getErrors(): { [Key in Id]: TypeError | undefined } { return Object.fromEntries(this.errors.entries()) as any; } From 74e514c1ad8a838fc12a5137899519d416a1397f Mon Sep 17 00:00:00 2001 From: angularpackage Date: Tue, 15 Feb 2022 22:55:14 +0100 Subject: [PATCH 170/179] refactor(ValidationErrors): add `undefined` to the return type of `getErrors()`. Use prettiezer. Update description of the `getErrors()`. --- src/lib/validation-errors.class.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/validation-errors.class.ts b/src/lib/validation-errors.class.ts index 006ba9d..78c0b14 100644 --- a/src/lib/validation-errors.class.ts +++ b/src/lib/validation-errors.class.ts @@ -24,16 +24,18 @@ export class ValidationErrors extends CommonErrors { * @returns The return value is the `ValidationError` instance of the given `id` if set, otherwise undefined. * @angularpackage */ - public get(id: ErrorId): ValidationError | undefined { + public get( + id: ErrorId + ): ValidationError | undefined { return this.errors.get(id); } /** - * 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. + * Returns the object of set validation errors, where the key is a unique identification. + * @returns The return value is an `object` of set validation errors. * @angularpackage */ - public getErrors(): { [Key in Id]: ValidationError } { + public getErrors(): { [Key in Id]: ValidationError | undefined } { return Object.fromEntries(this.errors.entries()) as any; } From ecaf1941e7fc04405a800afe4e6761b54e5035cb Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:15:32 +0100 Subject: [PATCH 171/179] docs(CHANGELOG.md): update. --- CHANGELOG.md | 146 +++++++++++++++++---------------------------------- 1 file changed, 47 insertions(+), 99 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c779b1..b5ce78c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,97 +6,45 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [3.0.0] - 2021-10-10 - -- Added `id` the identifier of the described `problem`, guarded by number type and it's a replacement for an optional tag [id] of the `template`. -- Added the `value` of any type affected by the validation error, which must be converted to a `string` to build a message and it's a replacement for an optional tag [value] of the `template`. -- Added `setTemplate()` static method to set the `template` of static `ValidationError` and as the default value for the `template` of an instance. -- Added `setValueParser()` static method to set the `function` to convert `any` value to `string` during message creation performed by the static `ValidationError.defineMessage()` method and as the default parser for the instance. -- Added `setId()` to set the `id` as an identifier of the described `problem`. -- Added `setValue()` to set the `value` affected by the validation error (must be converted to string). -- Added `setValueParser()` to sets the `function` to automatically convert the value of `any` type to the `string` during message creation. -- Added `template` property with optional `[id]`, `[value]` tags. -- Updated `throw()` method to throw a **new** instance instead of this. -- Removed possibility to throw just a `string` message. - -### 3.0.0 Added - -- [53c2043] [c00758a] - - **property `template`** - Added static public method `setTemplate()` to set the `template`. - Added tag `[id]` to the static private property `#defaultTemplate`. - - **property `value`** - Added private property `#value` by default equal to `''`. - Added `value` instance property that refers to private `#value`. - Added tag `[value]`. - Added `setValue()` method to set the `value` affected by the validation error. - - **property `#valueParser`** - Added static private `#defaultValueParser` property of the default value equal to `(value) => String(value)`. - Added private property `#valueParser` function of `ValueParser` type to convert the value of `any` type to `string` with the default value equal to static `ValidationError.#defaultValueParser`. - Added `setValueParser()` method of an instance to convert property `value` of an instance to `string` during message creation. - Added `setValueParser()` of static `ValidationError` to set value parser. - - **property `id`** - Added private property `#id` of an instance as an identifier of the described problem. - Added a public property `id` as an identifier of the described `problem`, guarded by `number` type, and as a replacement for an optional tag `[id]`. - Added tag `[id]` to static private property `#tags`. - Added public `setId()` method of an instance to set the `id`. - -- [285ef51] - Added property `template` of an instance that refers to private property `#tpl`. (changed to `#template` [c00758a]) - Added private property `#tags` of `string[]` to replace the template `[problem]` and `[fix]` tags. - -### 3.0.0 Changed - -- [c00758a] - Changed `updateMessage()` by adding an optional `callback` parameter and use of value parser function. - Changed `setMessage()` method of an instance by adding an optional parameter `parser` of `ValueParser` type and the way it assigns the message properties to only when they are all proper. - Changed `throw()` method to throw new instance of `ValidationError` based on actual settings. - Changed `constructor()` by adding an optional `parser` parameter of `ValueParser` type that initialize the default parser for the instance. - - **property `template`** - Changed private property name `#tpl` of an instance to `#template`. - Changed static private property name `#template` to `#defaultTemplate`. - Changed public property `template` of an instance to refers to private property `#template` of an instance. (changes [285ef51]) - -- [53c2043] - Changed `updateMessage()` method to include `#value` property. - -- [8a0f58d] - Changed library to `es2020`. - -- [285ef51] [25ea72b] - Changed conditionals to short if expressions. - Changed use of static `ValidationError` to `this`. - Changed to `ResultCallback` with generic type variable `Value`. - Changed usage of `is` and `guard` objects to use functions of `@angular-package/type`. - ~~Changed the `setMessage()` method to set the `problem` and `fix` properties to empty string if the provided message is a `string` type~~. - Changed `setMessage()` method to use as first, template from the given `message` parameter, and if it's empty the private property ~~`#tpl`~~ `#template`. - Changed `defineMessage()` static method to use private property `#tags` to replace them in the template. - -- [25ea72b] - Changed `set` prefix to `replace` of methods. - -### 3.0.0 Removed - -- [c00758a] - Removed possibility to add `string` type message in the `constructor`, `throw()`, `setMessage()` methods of an instance. - -- [7ed2b5c] [96c5563] [4af703a] - Removed the default usage of callback. - Removed `AllowedCallback` type because the removed default usage of callback. - -[53c2043]: https://github.com/angular-package/error/commit/53c20435df2225cb38e8d45d8f07e9c60db4d95a -[25ea72b]: https://github.com/angular-package/error/commit/25ea72b733939264b6546d01ef2945cc1e716fa5 -[8a0f58d]: https://github.com/angular-package/error/commit/8a0f58dae2b8a474848a2eccbe3218a0da0c017e -[285ef51]: https://github.com/angular-package/error/commit/285ef51805a3f1528b62d389214c106c3f213dfa -[c00758a]: https://github.com/angular-package/error/commit/c00758a02cd32e61b813719df7ee8b0f7d18cb12 -[7ed2b5c]: https://github.com/angular-package/error/commit/7ed2b5c90ed9922b8ced3622db4e0aa9b51afc38 -[96c5563]: https://github.com/angular-package/error/commit/96c5563285f86eb2ce5d1ecff39aa2110507f220 -[4af703a]: https://github.com/angular-package/error/commit/4af703afa7c2c432a902faad96fa535640bc5671 +## [3.0.0-rc] - 2022-02-16 + +### [3.0.0-rc] Added + +- Add the generic type variable `Id` to the `ValidationError`. +- Add `[Symbol.toStringTag]()` get accessor to return the object different class name. +- Add `ValidationErrors` object that is is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the `ValidationError` type are prepared to throw. [43a0162] +- Add `TypeErrors` object that is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the `TypeError` type are prepared to throw. [ff2feeb] +- Add `TypeError` object that is an extension of the `CommonError` class and is thrown when an operation could not be performed, typically(but not exclusively) when a value is not of the expected type, with the message built from the described problem and its solution, optional an explicit identification and type, on the given or stored template. [8112c16] +- Add `RangeErrors` object that is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the `RangeError` type are prepared to throw. [72b8582] +- Add `RangeError` object that is an extension of the `CommonError` class and is thrown when a value is not in the set or range of allowed values with the message built from the described problem and its solution, optional explicit identification and minimum/maximum range on the given or stored template. [77e4e0a] +- Add `Errors` object that is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the `Error` type are prepared to throw. [a5f391c] +- Add `Error` object that is an extension of the `CommonError` class and is thrown when a runtime error occurs with a message built from a solution to the described problem but with additional identification, on the template. [789083e] +- Add `CommonErrors` object that represents the storage of errors with unique identification numbers. [a17461b] +- Add `CommonError` abstract object to throw an identified error with a solution to the described problem, additional type, and range built on the template. [9089375] + +### [3.0.0-rc] Changed + +- Change the `constructor()` of the `ValidationError` to use direct parameters instead of object. +- Change `#tpl` property to `#template` and the default value to `Problem{id}: {problem} => Fix: {fix}`. +- Change the property `name` to `name` accessor. + +### [3.0.0-rc] Removed + +- Remove `#callback` private property from the `ValidationError` to simplify the object. +- Remove `VEAllowedCallback` type and `ErrorMessage` interface to simplify the object. +- Remove `set problem()`, `set message()`, `set fix()`, `set template()` accessors of an instance. +- Remove `#guardMessage()`, `#guardTemplate()`, `defineMessage()`, static method from the `ValidationError()`. +- Remove `updateMessage()`, `throw()`, `setTemplate()`, `setProblem()`, `setMessage()`, `setFix()` instance method from the `ValidationError()`. + +[43a0162]: https://github.com/angular-package/error/commit/43a01628af2a73aa428d7d6bcb48e9c3a1c755f3 +[ff2feeb]: https://github.com/angular-package/error/commit/ff2feebe48fdb1b3f8bfe3c58cedc09c8b6402df +[8112c16]: https://github.com/angular-package/error/commit/8112c166a2a7848b166bd4a45996f6e24b42862e +[72b8582]: https://github.com/angular-package/error/commit/72b8582f848075c27bd97ae8a05bed29c287ffd9 +[77e4e0a]: https://github.com/angular-package/error/commit/77e4e0a3760150a515f3a59b5efd5c779221427e +[a5f391c]: https://github.com/angular-package/error/commit/a5f391cbdb3a9a756b0f730bdc3c63232889ce0b +[789083e]: https://github.com/angular-package/error/commit/789083e5c79d6ee0f1f098bcc5a352a8dccf939b +[a17461b]: https://github.com/angular-package/error/commit/15a40397a17461bbd735079c3544c4c44f7b3f45 +[9089375]: https://github.com/angular-package/error/commit/908937597024576ad5d47fd1f1af652c1a2cc265 ## [2.0.2] - 2021-08-12 @@ -125,15 +73,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### 2.0.0 Added - [`069d111`][069d111] - Added static private property `#template` of a `string` type. - Added private instance `#callback` property of [`Callback`][package-callback] instance. - Added private instance `#fix`, `#problem`, `#tpl` property. - Added pubic methods [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage], [`setProblem()`][error-method-setproblem], [`setTemplate()`][error-method-settemplate], [`throw()`][error-method-throw], [`updateMessage()`][error-method-updatemssage] of an instance. - Added static private methods `#guardMessage()`, `#guardTemplate()`. + Add static private property `#template` of a `string` type. + Add private instance `#callback` property of [`Callback`][package-callback] instance. + Add private instance `#fix`, `#problem`, `#tpl` property. + Add pubic methods [`setFix()`][error-method-setfix], [`setMessage()`][error-method-setmessage], [`setProblem()`][error-method-setproblem], [`setTemplate()`][error-method-settemplate], [`throw()`][error-method-throw], [`updateMessage()`][error-method-updatemssage] of an instance. + Add static private methods `#guardMessage()`, `#guardTemplate()`. - [`4040750`][4040750] - Added an optional property `template` to the [`ErrorMessage`][error-interface-errormessage] interface. + Add an optional property `template` to the [`ErrorMessage`][error-interface-errormessage] interface. - [`0d5cc92`][0d5cc92] - Added [`VEAllowedCallback`][error-type-veallowedcallback] type of allowed names for internal instance of [`Callback`][package-callback]. + Add [`VEAllowedCallback`][error-type-veallowedcallback] type of allowed names for internal instance of [`Callback`][package-callback]. [069d111]: https://github.com/angular-package/error/commit/069d111220b63c2d2cdbffa499f3588121f14e16 [4040750]: https://github.com/angular-package/error/commit/40407503893484874e588b8b5b42c6e40a5fc3ab @@ -179,7 +127,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### 1.0.3 Fixed - [`5427c65`][5427c65] - Added message builder to api. + Add message builder to api. [5427c65]: https://github.com/angular-package/error/commit/5427c6585ddebe01bc6e3733425e07b924ec0ca6 From f47086ec3c809326c59fe6f7d0193b9b91cd3a87 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:43:17 +0100 Subject: [PATCH 172/179] docs(CHANGELOG.md): update. --- CHANGELOG.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5ce78c..d222b57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### [3.0.0-rc] Added -- Add the generic type variable `Id` to the `ValidationError`. -- Add `[Symbol.toStringTag]()` get accessor to return the object different class name. +- Add `define()`, `isValidationError()` static method to the `ValidationError` object. [b261662] [eb2d8e2] +- Add the generic type variable `Id` to the `ValidationError`. [eb2d8e2] +- Add `[Symbol.toStringTag]()` get accessor to return the object different class name. [eb2d8e2] - Add `ValidationErrors` object that is is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the `ValidationError` type are prepared to throw. [43a0162] - Add `TypeErrors` object that is an extension of the `CommonErrors` object that represents multiple identification numbers under which the errors of the `TypeError` type are prepared to throw. [ff2feeb] - Add `TypeError` object that is an extension of the `CommonError` class and is thrown when an operation could not be performed, typically(but not exclusively) when a value is not of the expected type, with the message built from the described problem and its solution, optional an explicit identification and type, on the given or stored template. [8112c16] @@ -31,11 +32,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### [3.0.0-rc] Removed - Remove `#callback` private property from the `ValidationError` to simplify the object. -- Remove `VEAllowedCallback` type and `ErrorMessage` interface to simplify the object. -- Remove `set problem()`, `set message()`, `set fix()`, `set template()` accessors of an instance. -- Remove `#guardMessage()`, `#guardTemplate()`, `defineMessage()`, static method from the `ValidationError()`. -- Remove `updateMessage()`, `throw()`, `setTemplate()`, `setProblem()`, `setMessage()`, `setFix()` instance method from the `ValidationError()`. - +- Remove `VEAllowedCallback` type and `ErrorMessage` interface to simplify the object. [00229cd] [30716b2] +- Remove `set problem()`, `set message()`, `set fix()`, `set template()` accessors of an `ValidationError` instance. [b261662] +- Remove `#guardMessage()`, `#guardTemplate()`, `defineMessage()`, static method from the `ValidationError()`. [b261662] +- Remove `updateMessage()`, `throw()`, `setTemplate()`, `setProblem()`, `setMessage()`, `setFix()` instance method from the `ValidationError()`. [b261662] + +[eb2d8e2]: https://github.com/angular-package/error/commit/eb2d8e243ff6ee5f44fd00e4d462d2b2c175702a +[b261662]: https://github.com/angular-package/error/commit/b2616625bb80790f97da9138f75305ceb3c55af2 +[30716b2]: https://github.com/angular-package/error/commit/30716b22970218bb4745d0482908e55138467833 +[00229cd]: https://github.com/angular-package/error/commit/00229cda3f116766df5d1872519184332ee0402d [43a0162]: https://github.com/angular-package/error/commit/43a01628af2a73aa428d7d6bcb48e9c3a1c755f3 [ff2feeb]: https://github.com/angular-package/error/commit/ff2feebe48fdb1b3f8bfe3c58cedc09c8b6402df [8112c16]: https://github.com/angular-package/error/commit/8112c166a2a7848b166bd4a45996f6e24b42862e From e78a407e134b762f957b81c3cd05dd2d2fedbc19 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:49:15 +0100 Subject: [PATCH 173/179] docs(ValidationError): update `define()` static method `template` parameter description. --- src/lib/validation-error.class.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/validation-error.class.ts b/src/lib/validation-error.class.ts index 4c020f1..8583101 100644 --- a/src/lib/validation-error.class.ts +++ b/src/lib/validation-error.class.ts @@ -33,8 +33,8 @@ export class ValidationError extends CommonError { * @param problem Description of the problem of a `string` type. * @param fix A solution to the given `problem` of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{min}`, `{max}` and - * `{type}` tags. By default, the value is picked from the static property `ValidationError.template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{min}`, `{max}` and tags. + * By default, the value is picked from the static property `ValidationError.template`. * @returns The return value is a new instance of the `ValidationError` with the message built from the given required `problem`, `fix` * and optional `id` on the given or stored `template`. * @angularpackage @@ -72,7 +72,7 @@ export class ValidationError extends CommonError { * @param fix A solution to the given validation issue of a `string` type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. * @param template Optional template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is picked from the static property `template`. + * tags. By default, the value is picked from the static property `template`. * @angularpackage */ constructor( From 66113ab269d9300e0be44affe397690d5a8a6a8a Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:49:49 +0100 Subject: [PATCH 174/179] docs(Error): update `constructor()` parameter `template` description. --- src/lib/error.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/error.class.ts b/src/lib/error.class.ts index af85623..7f4b1ef 100644 --- a/src/lib/error.class.ts +++ b/src/lib/error.class.ts @@ -68,8 +68,8 @@ export class Error extends CommonError { * @param problem Description of the problem of a string type. * @param fix A solution to the given `problem` of a string type. * @param id Optional unique identification to the given `problem` of generic type variable `Id`. - * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and - * `{type}` tags. By default, the value is equal to the static property `template`. + * @param template A template of error message with the replaceable `{problem}`, `{fix}` and optional `{id}`, `{max}`, `{min}` and tags. + * By default, the value is equal to the static property `template`. * @angularpackage */ constructor( From 26040039ddfc462c3f6e21d6244b84a523d93b18 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:50:11 +0100 Subject: [PATCH 175/179] test(testError()): add helper function. --- src/test/test-error.func.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/test-error.func.ts diff --git a/src/test/test-error.func.ts b/src/test/test-error.func.ts new file mode 100644 index 0000000..a5b252e --- /dev/null +++ b/src/test/test-error.func.ts @@ -0,0 +1,17 @@ + + +export const testError = (value: any, check: any) => { + + expect(value.fix).toEqual(check.fix); + expect(value.id).toEqual(check.id); + expect(value.link).toEqual(check.link); + expect(value.max).toEqual(check.max); + expect(value.min).toEqual(check.min); + expect(value.name).toEqual(check.name); + expect(value.problem).toEqual(check.problem); + expect(value.range).toEqual(check.range); + expect(value.message).toEqual(check.message); + expect(value.template).toEqual(check.template); + expect(value.type).toEqual(check.type); + +}; From 9bd289766dd386e1603569fbbc48a91d33259739 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:52:04 +0100 Subject: [PATCH 176/179] test: add tests. --- src/test/common-errors.spec.ts | 19 +++++ src/test/errors.spec.ts | 79 ++++++++++++++++++++ src/test/range-error.spec.ts | 12 +-- src/test/range-errors.spec.ts | 86 ++++++++++++++++++++++ src/test/type-errors.spec.ts | 114 +++++++++++++++++++++++++++++ src/test/validation-errors.spec.ts | 78 ++++++++++++++++++++ 6 files changed, 382 insertions(+), 6 deletions(-) create mode 100644 src/test/common-errors.spec.ts create mode 100644 src/test/errors.spec.ts create mode 100644 src/test/range-errors.spec.ts create mode 100644 src/test/type-errors.spec.ts create mode 100644 src/test/validation-errors.spec.ts diff --git a/src/test/common-errors.spec.ts b/src/test/common-errors.spec.ts new file mode 100644 index 0000000..bb5fb8e --- /dev/null +++ b/src/test/common-errors.spec.ts @@ -0,0 +1,19 @@ +import { CommonErrors } from '../lib/common-errors.class'; +import { Error } from '../lib/error.class'; + +export class TestClass extends CommonErrors { + public get errors(): Map { + return super.errors; + } + public set( + problem: string, + fix: string, + id: ErrorId + ): this { + if (super.isAllowedId(id)) { + this.errors.set(id, new Error(problem, fix, id)); + } + return this; + } +} + diff --git a/src/test/errors.spec.ts b/src/test/errors.spec.ts new file mode 100644 index 0000000..e27b6f1 --- /dev/null +++ b/src/test/errors.spec.ts @@ -0,0 +1,79 @@ +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +// Class. +import { Error } from '../lib/error.class'; +import { Errors } from '../lib/errors.class'; +import { testError } from './test-error.func'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] Errors', () => { + // Prepare the values. + const id1 = '(E:127)'; + const id2 = '(RE:227)'; + const id3 = '(TE:327)'; + const id4 = '(VE:427)'; + + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const problem = 'The value must be a string type.'; + const template = `Problem(VE{id}): {problem}\nFix: {fix} {type}`; + + let errors = new Errors(id1, id2, id3, id4); + + beforeEach(() => (errors = new Errors(id1, id2, id3, id4))); + + testing.describe(`[counter] Methods`, () => { + testing + + /* + Errors.prototype.delete() + */ + .it(`Errors.prototype.delete()`, () => { + toBe.instance(errors, Errors); + errors.set(problem, fix, '(E:127)'); + expect(errors.has('(E:127)')).toBeTrue(); + errors.delete('(E:127)'); + expect(errors.has('(E:127)')).toBeFalse(); + }) + + /* + Errors.prototype.has() + */ + .it(`Errors.prototype.has()`, () => { + errors.set(problem, fix, id4); + expect(errors.has(id4)).toBeTrue(); + }) + + /* + Errors.prototype.set() + */ + .it(`Errors.prototype.set()`, () => { + errors.set(problem, fix, id3, template); + expect(errors.has(id3)).toBeTrue(); + }) + + /* + Errors.prototype.throw() + */ + .it(`Errors.prototype.throw()`, () => { + try { + errors.set(problem, fix, id4, template).throw('(VE:427)'); + } catch (e) { + if (e instanceof Error) { + testError(e, { + fix, + id: id4, + name: 'Error', + message: `Problem(VE${id4}): ${problem}\nFix: ${fix} ${''}`, + problem, + template, + }); + } + } + }); + }); +}); diff --git a/src/test/range-error.spec.ts b/src/test/range-error.spec.ts index 5e904f4..e157303 100644 --- a/src/test/range-error.spec.ts +++ b/src/test/range-error.spec.ts @@ -31,10 +31,10 @@ testing.describe('[counter] RangeError', () => { */ .describe(`Static properties`, () => { testing.it(`RangeError.template`, () => { - expect(RangeError.template).toEqual(`Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`); + expect(RangeError.template).toEqual(`Problem{id}: {problem} => Fix: {fix} between {min} and {max}`); RangeError.template = `{problem} => Fix: {fix} of {id}`; expect(RangeError.template).toEqual(`{problem} => Fix: {fix} of {id}`); - RangeError.template = `Problem{id}: {problem} must be between {min} and {max} => Fix: {fix}`; + RangeError.template = `Problem{id}: {problem} => Fix: {fix} between {min} and {max}`; }); }) @@ -139,7 +139,7 @@ testing.describe('[counter] RangeError', () => { testing .it(`(problem, fix)`, () => { const e = new RangeError(problem, fix); - expect(e.message).toEqual(`Problem${''}: ${problem} must be between ${''} and ${''} => Fix: ${fix}`); + expect(e.message).toEqual(`Problem${''}: ${problem} => Fix: ${fix} between ${''} and ${''}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); @@ -152,7 +152,7 @@ testing.describe('[counter] RangeError', () => { .it(`(problem, fix, id, min, undefined)`, () => { const e = new RangeError(problem, fix, id, min, undefined); - expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${''} => Fix: ${fix}`); + expect(e.message).toEqual(`Problem${id}: ${problem} => Fix: ${fix} between ${min} and ${''}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); @@ -164,7 +164,7 @@ testing.describe('[counter] RangeError', () => { }) .it(`(problem, fix, id, undefined, max)`, () => { const e = new RangeError(problem, fix, id, undefined, max); - expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${''} and ${max} => Fix: ${fix}`); + expect(e.message).toEqual(`Problem${id}: ${problem} => Fix: ${fix} between ${''} and ${max}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); @@ -176,7 +176,7 @@ testing.describe('[counter] RangeError', () => { }) .it(`(problem, fix, id, min, max)`, () => { const e = new RangeError(problem, fix, id, min, max); - expect(e.message).toEqual(`Problem${id}: ${problem} must be between ${min} and ${max} => Fix: ${fix}`); + expect(e.message).toEqual(`Problem${id}: ${problem} => Fix: ${fix} between ${min} and ${max}`); // Required. expect(e.fix).toEqual(fix); expect(e.problem).toEqual(problem); diff --git a/src/test/range-errors.spec.ts b/src/test/range-errors.spec.ts new file mode 100644 index 0000000..8f50972 --- /dev/null +++ b/src/test/range-errors.spec.ts @@ -0,0 +1,86 @@ +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +// Class. +import { RangeErrors } from '../lib/range-errors.class'; +import { RangeError } from '../lib/range-error.class'; +// Function. +import { testError } from './test-error.func'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] RangeErrors', () => { + // Prepare the values. + const id1 = '(E:127)'; + const id2 = '(RE:227)'; + const id3 = '(TE:327)'; + const id4 = '(VE:427)'; + + const min = 9; + const max = 27; + + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const problem = 'The value must be a string type.'; + const template = `Problem(VE{id}): {problem}\nFix: {fix} {min} {max}`; + + let errors = new RangeErrors(id1, id2, id3, id4); + + beforeEach(() => (errors = new RangeErrors(id1, id2, id3, id4))); + + testing.describe(`[counter] Methods`, () => { + testing + + /* + RangeErrors.prototype.delete() + */ + .it(`RangeErrors.prototype.delete()`, () => { + toBe.instance(errors, RangeErrors); + errors.set(problem, fix, '(E:127)'); + expect(errors.has('(E:127)')).toBeTrue(); + errors.delete('(E:127)'); + expect(errors.has('(E:127)')).toBeFalse(); + }) + + /* + RangeErrors.prototype.has() + */ + .it(`RangeErrors.prototype.has()`, () => { + errors.set(problem, fix, id4, min, max, template); + expect(errors.has(id4)).toBeTrue(); + }) + + /* + RangeErrors.prototype.set() + */ + .it(`RangeErrors.prototype.set()`, () => { + errors.set(problem, fix, id3, min, max, template); + expect(errors.has(id3)).toBeTrue(); + }) + + /* + RangeErrors.prototype.throw() + */ + .it(`RangeErrors.prototype.throw()`, () => { + try { + errors.set(problem, fix, id4, min, max, template).throw(id4); + } catch (e) { + if (e instanceof RangeError) { + testError(e, { + fix, + id: id4, + name: 'RangeError', + message: `Problem(VE${id4}): ${problem}\nFix: ${fix} ${min} ${max}`, + min, + max, + range: {min, max}, + problem, + template, + }); + } + } + }); + }); +}); diff --git a/src/test/type-errors.spec.ts b/src/test/type-errors.spec.ts new file mode 100644 index 0000000..6a6615d --- /dev/null +++ b/src/test/type-errors.spec.ts @@ -0,0 +1,114 @@ + +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +// Class. +import { TypeError } from '../lib/type-error.class'; +import { TypeErrors } from '../lib/type-errors.class'; +import { testError } from './test-error.func'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] TypeErrors', () => { + // Prepare the values. + const id1 = '(E:127)'; + const id2 = '(RE:227)'; + const id3 = '(TE:327)'; + const id4 = '(VE:427)'; + + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const problem = 'The value must be a string type.'; + const type = 'number'; + const template = `Problem(VE{id}): {problem}\nFix: {fix} {type}`; + + let errors = new TypeErrors(id1, id2, id3, id4); + + beforeEach(() => errors = new TypeErrors(id1, id2, id3, id4)); + + testing + .describe(`[counter] Methods`, () => { + testing + + /* + TypeErrors.prototype.delete() + */ + .it(`TypeErrors.prototype.delete()`, () => { + toBe.instance(errors, TypeErrors); + errors.set(problem, fix, '(E:127)'); + expect(errors.has('(E:127)')).toBeTrue(); + errors.delete('(E:127)'); + expect(errors.has('(E:127)')).toBeFalse(); + }) + + /* + TypeErrors.prototype.has() + */ + .it(`TypeErrors.prototype.has()`, () => { + errors.set(problem, fix, id4, type, template); + expect(errors.has(id4)).toBeTrue(); + }) + + /* + TypeErrors.prototype.set() + */ + .it(`TypeErrors.prototype.set()`, () => { + errors.set(problem, fix, id3, type, template); + expect(errors.has(id3)).toBeTrue(); + }) + + /* + TypeErrors.prototype.throw() + */ + .it(`TypeErrors.prototype.throw()`, () => { + try { + errors.set(problem, fix, id4).throw(id4); + } catch (e) { + if (e instanceof TypeError) { + testError(e, { + fix, + id: id4, + name: 'TypeError', + message: `Problem${id4}: ${problem} => Fix: ${fix} must be of the ${''}`, + problem, + template: `Problem{id}: {problem} => Fix: {fix} must be of the {type}` + }); + } + } + + try { + errors.set(problem, fix, id4, type).throw(id4); + } catch (e) { + if (e instanceof TypeError) { + testError(e, { + fix, + id: id4, + name: 'TypeError', + message: `Problem${id4}: ${problem} => Fix: ${fix} must be of the ${type}`, + problem, + template: `Problem{id}: {problem} => Fix: {fix} must be of the {type}`, + type + }); + } + } + + try { + errors.set(problem, fix, id4, type, template).throw(id4); + } catch (e) { + if (e instanceof TypeError) { + testError(e, { + fix, + id: id4, + name: 'TypeError', + message: `Problem(VE${id4}): ${problem}\nFix: ${fix} ${type}`, + problem, + template, + type + }); + } + } + }); + }); +}); diff --git a/src/test/validation-errors.spec.ts b/src/test/validation-errors.spec.ts new file mode 100644 index 0000000..c6d9e46 --- /dev/null +++ b/src/test/validation-errors.spec.ts @@ -0,0 +1,78 @@ +import { Testing, TestingToBeMatchers } from '@angular-package/testing'; +// Class. +import { ValidationErrors } from '../lib/validation-errors.class'; +import { testError } from './test-error.func'; +/** + * Initialize `Testing`. + */ +const testing = new Testing(true, true); +const toBe = new TestingToBeMatchers(); +/** + * Tests. + */ +testing.describe('[counter] ValidationErrors', () => { + // Prepare the values. + const id1 = '(E:127)'; + const id2 = '(RE:227)'; + const id3 = '(TE:327)'; + const id4 = '(VE:427)'; + + const fix = 'Provide string type value. Read more: https://duckduckgo.com/'; + const problem = 'The value must be a string type.'; + const template = `Problem(VE{id}): {problem}\nFix: {fix} {type}`; + + let errors = new ValidationErrors(id1, id2, id3, id4); + + beforeEach(() => (errors = new ValidationErrors(id1, id2, id3, id4))); + + testing.describe(`[counter] Methods`, () => { + testing + + /* + ValidationErrors.prototype.delete() + */ + .it(`ValidationErrors.prototype.delete()`, () => { + toBe.instance(errors, ValidationErrors); + errors.set(problem, fix, '(E:127)'); + expect(errors.has('(E:127)')).toBeTrue(); + errors.delete('(E:127)'); + expect(errors.has('(E:127)')).toBeFalse(); + }) + + /* + ValidationErrors.prototype.has() + */ + .it(`ValidationErrors.prototype.has()`, () => { + errors.set(problem, fix, id4); + expect(errors.has(id4)).toBeTrue(); + }) + + /* + ValidationErrors.prototype.set() + */ + .it(`ValidationErrors.prototype.set()`, () => { + errors.set(problem, fix, id3, template); + expect(errors.has(id3)).toBeTrue(); + }) + + /* + ValidationErrors.prototype.throw() + */ + .it(`ValidationErrors.prototype.throw()`, () => { + try { + errors.set(problem, fix, id4, template).throw('(VE:427)'); + } catch (e) { + if (e instanceof Error) { + testError(e, { + fix, + id: id4, + name: 'ValidationError', + message: `Problem(VE${id4}): ${problem}\nFix: ${fix} ${''}`, + problem, + template, + }); + } + } + }); + }); +}); From 1795fe1afcee1edb6af2bba9eea40e6f9486b2c0 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:53:49 +0100 Subject: [PATCH 177/179] docs(README.md): update. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 283909d..920350d 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Manages an [`Error`][js-error]. ## Documentation -For the detailed documentation go to [**https://error.angular-package.dev/**](https://error.angular-package.dev/) +For the detailed documentation go to [**https://error.angular-package.dev**](https://error.angular-package.dev)
From a4f0cf94b9abe3b01642c878afa4cfc5101fb73c Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:54:24 +0100 Subject: [PATCH 178/179] chore(package): update. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ed0b60b..17f3594 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "tslib": "^2.3.0" }, "devDependencies": { - "@angular-package/testing": "^2.0.0", - "@angular-package/type": "^5.0.0" + "@angular-package/testing": "^2.0.0-rc", + "@angular-package/type": "^5.0.0-rc.0" }, "publishConfig": { "access": "public", From 87b695893391f5e21f97a6614dee5d353ddcf2c8 Mon Sep 17 00:00:00 2001 From: angularpackage Date: Wed, 16 Feb 2022 19:58:35 +0100 Subject: [PATCH 179/179] v3.0.0-rc --- package-lock.json | 357 ++-------------------------------------------- package.json | 2 +- 2 files changed, 11 insertions(+), 348 deletions(-) diff --git a/package-lock.json b/package-lock.json index 84fb7ea..7bce365 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,368 +1,31 @@ { "name": "@angular-package/error", - "version": "2.0.2", - "lockfileVersion": 2, + "version": "3.0.0-rc", + "lockfileVersion": 1, "requires": true, - "packages": { - "": { - "name": "@angular-package/error", - "version": "2.0.2", - "license": "MIT", - "dependencies": { - "tslib": "^2.2.0" - }, - "devDependencies": { - "@angular-package/testing": "^1.1.0" - }, - "peerDependencies": { - "@angular-package/callback": "^2.0.0", - "@angular-package/type": "^4.2.0" - } - }, - "node_modules/@angular-package/callback": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@angular-package/callback/-/callback-2.0.0.tgz", - "integrity": "sha512-4c2aG3s04cOOzIPpgI1LckaImS5wJXf5tnr/cFsyVbG9MMB+Agt3hP+OTGHdERTgLD38ZUkbURI2fcFmLRMOQw==", - "peer": true, - "dependencies": { - "tslib": "^2.2.0" - }, - "peerDependencies": { - "@angular-package/error": "^2.0.2", - "@angular-package/type": "^4.2.0" - } - }, - "node_modules/@angular-package/error": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/@angular-package/error/-/error-2.0.2.tgz", - "integrity": "sha512-kXigjTEKTZVDurGFXQSAQpN5EmTe70AiPW+oCcjKaqmaN1XJlkFFdgS+kTsVVGefd3Mh3h6Y5DF95y/RtPkF4w==", - "peer": true, - "dependencies": { - "tslib": "^2.2.0" - }, - "peerDependencies": { - "@angular-package/callback": "^2.0.0", - "@angular-package/type": "^4.2.0" - } - }, - "node_modules/@angular-package/testing": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@angular-package/testing/-/testing-1.1.0.tgz", - "integrity": "sha512-N/c75WBG4GPcgj7NYWAKSE9IwcPSqVMOYjkZ3RU4rUZW5equkjZvQ4zWKc8S4lbxr1wPD6KxPy3T52xTsBW3Dw==", - "dev": true, - "dependencies": { - "tslib": "^2.2.0" - }, - "peerDependencies": { - "@angular-package/type": ">= 4.2.0", - "jasmine": "^3.8.0" - } - }, - "node_modules/@angular-package/type": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/@angular-package/type/-/type-4.2.0.tgz", - "integrity": "sha512-Y7HT94Gia4soR2ynwK8qva0WTOplau++gInEQCwDnhXmYy/r/1J/XhW0+aVj5XIa3SLWWa70nHvI0ivBM7O1nw==", - "peer": true, - "dependencies": { - "tslib": "^2.1.0" - } - }, - "node_modules/balanced-match": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true, - "peer": true - }, - "node_modules/brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, - "peer": true, - "dependencies": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" - } - }, - "node_modules/concat-map": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true, - "peer": true - }, - "node_modules/fs.realpath": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true, - "peer": true - }, - "node_modules/glob": { - "version": "7.1.7", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", - "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", - "dev": true, - "peer": true, - "dependencies": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - }, - "engines": { - "node": "*" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, - "node_modules/inflight": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, - "peer": true, - "dependencies": { - "once": "^1.3.0", - "wrappy": "1" - } - }, - "node_modules/inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true, - "peer": true - }, - "node_modules/jasmine": { - "version": "3.8.0", - "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.8.0.tgz", - "integrity": "sha512-kdQ3SfcNpMbbMdgJPLyFe9IksixdnrgYaCJapP9sS0aLgdWdIZADNXEr+11Zafxm1VDfRSC5ZL4fzXT0bexzXw==", - "dev": true, - "peer": true, - "dependencies": { - "glob": "^7.1.6", - "jasmine-core": "~3.8.0" - }, - "bin": { - "jasmine": "bin/jasmine.js" - } - }, - "node_modules/jasmine-core": { - "version": "3.8.0", - "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.8.0.tgz", - "integrity": "sha512-zl0nZWDrmbCiKns0NcjkFGYkVTGCPUgoHypTaj+G2AzaWus7QGoXARSlYsSle2VRpSdfJmM+hzmFKzQNhF2kHg==", - "dev": true, - "peer": true - }, - "node_modules/minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, - "peer": true, - "dependencies": { - "brace-expansion": "^1.1.7" - }, - "engines": { - "node": "*" - } - }, - "node_modules/once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, - "peer": true, - "dependencies": { - "wrappy": "1" - } - }, - "node_modules/path-is-absolute": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true, - "peer": true, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/tslib": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", - "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==" - }, - "node_modules/wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true, - "peer": true - } - }, "dependencies": { - "@angular-package/callback": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@angular-package/callback/-/callback-2.0.0.tgz", - "integrity": "sha512-4c2aG3s04cOOzIPpgI1LckaImS5wJXf5tnr/cFsyVbG9MMB+Agt3hP+OTGHdERTgLD38ZUkbURI2fcFmLRMOQw==", - "peer": true, - "requires": { - "tslib": "^2.2.0" - } - }, - "@angular-package/error": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/@angular-package/error/-/error-2.0.2.tgz", - "integrity": "sha512-kXigjTEKTZVDurGFXQSAQpN5EmTe70AiPW+oCcjKaqmaN1XJlkFFdgS+kTsVVGefd3Mh3h6Y5DF95y/RtPkF4w==", - "peer": true, - "requires": { - "tslib": "^2.2.0" - } - }, "@angular-package/testing": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@angular-package/testing/-/testing-1.1.0.tgz", - "integrity": "sha512-N/c75WBG4GPcgj7NYWAKSE9IwcPSqVMOYjkZ3RU4rUZW5equkjZvQ4zWKc8S4lbxr1wPD6KxPy3T52xTsBW3Dw==", + "version": "2.0.0-rc", + "resolved": "https://registry.npmjs.org/@angular-package/testing/-/testing-2.0.0-rc.tgz", + "integrity": "sha512-DjlOD0gnLlqT8b4Qqr5FlPJWnl1NsO0823fH9B+e+rDxoJZa6Ys2cGg8716ZRBrSVWdBeuVxrjDhMJpYX5GVDw==", "dev": true, "requires": { - "tslib": "^2.2.0" + "tslib": "^2.3.0" } }, "@angular-package/type": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/@angular-package/type/-/type-4.2.0.tgz", - "integrity": "sha512-Y7HT94Gia4soR2ynwK8qva0WTOplau++gInEQCwDnhXmYy/r/1J/XhW0+aVj5XIa3SLWWa70nHvI0ivBM7O1nw==", - "peer": true, - "requires": { - "tslib": "^2.1.0" - } - }, - "balanced-match": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "version": "5.0.0-rc.0", + "resolved": "https://registry.npmjs.org/@angular-package/type/-/type-5.0.0-rc.0.tgz", + "integrity": "sha512-NR3ODKJTmmdEqCz7fn6YDO68p9DK/SNWGvCV8pUqQUSFHlbrc0RDaqqF0liIi1iJcRZhbF2UnhWIIOp/iamtsg==", "dev": true, - "peer": true - }, - "brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, - "peer": true, "requires": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" + "tslib": "^2.3.0" } }, - "concat-map": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true, - "peer": true - }, - "fs.realpath": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true, - "peer": true - }, - "glob": { - "version": "7.1.7", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", - "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", - "dev": true, - "peer": true, - "requires": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - } - }, - "inflight": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, - "peer": true, - "requires": { - "once": "^1.3.0", - "wrappy": "1" - } - }, - "inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true, - "peer": true - }, - "jasmine": { - "version": "3.8.0", - "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.8.0.tgz", - "integrity": "sha512-kdQ3SfcNpMbbMdgJPLyFe9IksixdnrgYaCJapP9sS0aLgdWdIZADNXEr+11Zafxm1VDfRSC5ZL4fzXT0bexzXw==", - "dev": true, - "peer": true, - "requires": { - "glob": "^7.1.6", - "jasmine-core": "~3.8.0" - } - }, - "jasmine-core": { - "version": "3.8.0", - "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.8.0.tgz", - "integrity": "sha512-zl0nZWDrmbCiKns0NcjkFGYkVTGCPUgoHypTaj+G2AzaWus7QGoXARSlYsSle2VRpSdfJmM+hzmFKzQNhF2kHg==", - "dev": true, - "peer": true - }, - "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, - "peer": true, - "requires": { - "brace-expansion": "^1.1.7" - } - }, - "once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, - "peer": true, - "requires": { - "wrappy": "1" - } - }, - "path-is-absolute": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true, - "peer": true - }, "tslib": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==" - }, - "wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true, - "peer": true } } } diff --git a/package.json b/package.json index 17f3594..d9eb854 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@angular-package/error", - "version": "3.0.0", + "version": "3.0.0-rc", "description": "Manages an error.", "author": "Angular Package (https://angular-package.dev)", "homepage": "https://error.angular-package.dev",