ngx-smart-errors 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +167 -0
- package/cli/index.js +64 -0
- package/package.json +43 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.0] - 2024-04-03
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **NgxErrorComponent** — standalone component for rendering validation errors with signal-based reactivity.
|
|
13
|
+
- **NgxErrorDirective** — attribute directive that auto-appends error UI next to form controls.
|
|
14
|
+
- **ErrorMessageService** — centralized service for resolving, interpolating, and localizing error messages.
|
|
15
|
+
- **provideSmartErrors()** — environment provider function for global configuration.
|
|
16
|
+
- **useError()** — signal-based hook for programmatic error access in component classes.
|
|
17
|
+
- **i18n support** — multi-language error message maps with runtime language switching.
|
|
18
|
+
- **Custom templates** — `ng-template` support for fully custom error rendering.
|
|
19
|
+
- **Smart triggers** — `touched`, `dirty`, and `submit` trigger strategies.
|
|
20
|
+
- **Interpolation engine** — automatic replacement of `{requiredLength}`, `{min}`, `{max}`, etc.
|
|
21
|
+
- **CLI setup wizard** — `npx ngx-smart-errors` interactive configuration generator.
|
|
22
|
+
- **Debug mode** — optional console logging for development troubleshooting.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Burcin Eren
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# ngx-smart-errors ✨
|
|
2
|
+
|
|
3
|
+
**Production-ready Angular form error handling system.**
|
|
4
|
+
Stop writing repetitive boilerplates for error validation and start using smart, reactive messages.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 🎯 The Problem
|
|
9
|
+
|
|
10
|
+
Normally, to show errors in Angular, you write this for *every* input:
|
|
11
|
+
|
|
12
|
+
```html
|
|
13
|
+
<!-- BEFORE: ❌ Repetitive, messy, and hard to maintain -->
|
|
14
|
+
@if (email.invalid && (email.touched || email.dirty)) {
|
|
15
|
+
<div class="error">
|
|
16
|
+
@if (email.errors?.['required']) {
|
|
17
|
+
<span>Email is required</span>
|
|
18
|
+
}
|
|
19
|
+
@if (email.errors?.['email']) {
|
|
20
|
+
<span>Invalid email format</span>
|
|
21
|
+
}
|
|
22
|
+
</div>
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 🚀 The Solution
|
|
27
|
+
|
|
28
|
+
With `ngx-smart-errors`, it's zero-boilerplate:
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<!-- AFTER: ✅ Clean, dynamic, and configurable -->
|
|
32
|
+
<input [formControl]="email" ngxError />
|
|
33
|
+
|
|
34
|
+
<!-- OR using the component -->
|
|
35
|
+
<ngx-error [control]="email" />
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## ⚙️ Features
|
|
41
|
+
|
|
42
|
+
- **Signals-based Reactivity**: Fully reactive with Angular 17+ Signals.
|
|
43
|
+
- **Zero Configuration**: Sensible defaults for all built-in validators.
|
|
44
|
+
- **Smart Triggers**: Show errors on `touched`, `dirty`, or `submit`.
|
|
45
|
+
- **Global & Local Config**: Override messages globally or locally.
|
|
46
|
+
- **Structural Directive**: Append error UI automatically with a simple `ngxError` attribute.
|
|
47
|
+
- **i18n Ready**: Native support for multi-language message maps.
|
|
48
|
+
- **Interpolation**: Automatically Replace `{requiredLength}`, `{min}`, etc. from validator results.
|
|
49
|
+
- **Custom Templates**: Complete control over Error UI via `ng-template`.
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 📦 Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm install ngx-smart-errors
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## 🛠️ Global Configuration
|
|
62
|
+
|
|
63
|
+
Provide global error messages in your `app.config.ts`:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { provideSmartErrors } from 'ngx-smart-errors';
|
|
67
|
+
|
|
68
|
+
export const appConfig: ApplicationConfig = {
|
|
69
|
+
providers: [
|
|
70
|
+
provideSmartErrors({
|
|
71
|
+
errorMessages: {
|
|
72
|
+
required: 'This field is required',
|
|
73
|
+
email: 'Please enter a valid email address',
|
|
74
|
+
minlength: 'Minimum length is {requiredLength} characters',
|
|
75
|
+
},
|
|
76
|
+
defaultTrigger: 'touched', // or 'dirty', 'submit'
|
|
77
|
+
defaultLanguage: 'en',
|
|
78
|
+
})
|
|
79
|
+
]
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## 📖 Usage Examples
|
|
86
|
+
|
|
87
|
+
### 1. Simple Directive Usage
|
|
88
|
+
|
|
89
|
+
Automatically appends the error component under the input.
|
|
90
|
+
|
|
91
|
+
```html
|
|
92
|
+
<input formControlName="username" ngxError />
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 2. Manual Component Usage
|
|
96
|
+
|
|
97
|
+
```html
|
|
98
|
+
<ngx-error [control]="form.controls.password" trigger="dirty" />
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 3. Custom Local Messages
|
|
102
|
+
|
|
103
|
+
```html
|
|
104
|
+
<ngx-error
|
|
105
|
+
[control]="email"
|
|
106
|
+
[messages]="{
|
|
107
|
+
required: 'We really need your email',
|
|
108
|
+
email: 'That does not look like an email'
|
|
109
|
+
}"
|
|
110
|
+
/>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 4. UI Customization with Templates
|
|
114
|
+
|
|
115
|
+
```html
|
|
116
|
+
<ngx-error [control]="email">
|
|
117
|
+
<ng-template let-error>
|
|
118
|
+
<div class="alert alert-danger">
|
|
119
|
+
<i class="icon-warning"></i> {{ error }}
|
|
120
|
+
</div>
|
|
121
|
+
</ng-template>
|
|
122
|
+
</ngx-error>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 5. Using Signals (Modern Reactive API)
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { useError } from 'ngx-smart-errors';
|
|
129
|
+
|
|
130
|
+
@Component({ ... })
|
|
131
|
+
export class MyComponent {
|
|
132
|
+
email = new FormControl('', [Validators.required]);
|
|
133
|
+
// reactive signal that updates automatically
|
|
134
|
+
emailError = useError(this.email);
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## 🌍 i18n support
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
provideSmartErrors({
|
|
144
|
+
errorMessages: {
|
|
145
|
+
required: {
|
|
146
|
+
en: 'Required',
|
|
147
|
+
tr: 'Zorunlu alan'
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
defaultLanguage: 'en'
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## 🧪 Roadmap
|
|
157
|
+
|
|
158
|
+
- [ ] Support for Material Design (Auto-integration)
|
|
159
|
+
- [ ] Auto-discovery of `FormGroup` submission state
|
|
160
|
+
- [ ] Custom validation hint animations
|
|
161
|
+
- [ ] Support for Async Validators
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## 🎁 License
|
|
166
|
+
|
|
167
|
+
MIT
|
package/cli/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const readline = require('readline');
|
|
6
|
+
|
|
7
|
+
const rl = readline.createInterface({
|
|
8
|
+
input: process.stdin,
|
|
9
|
+
output: process.stdout
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const blue = (text) => `\x1b[34m${text}\x1b[0m`;
|
|
13
|
+
const green = (text) => `\x1b[32m${text}\x1b[0m`;
|
|
14
|
+
const yellow = (text) => `\x1b[33m${text}\x1b[0m`;
|
|
15
|
+
|
|
16
|
+
console.log(`\n✨ ${blue('ngx-smart-errors')} ${green('CLI Setup Wizard')}\n`);
|
|
17
|
+
|
|
18
|
+
async function run() {
|
|
19
|
+
const language = await ask('🌍 Preferred language (en/tr)? ', 'en');
|
|
20
|
+
const trigger = await ask('🎯 Default trigger (touched/dirty/submit)? ', 'touched');
|
|
21
|
+
const theme = await ask('🎨 UI theme (default/tailwind)? ', 'default');
|
|
22
|
+
|
|
23
|
+
console.log(`\n🚀 Generating configuration...`);
|
|
24
|
+
|
|
25
|
+
const config = `
|
|
26
|
+
import { provideSmartErrors } from 'ngx-smart-errors';
|
|
27
|
+
|
|
28
|
+
export const smartErrorsConfig = provideSmartErrors({
|
|
29
|
+
defaultLanguage: '${language}',
|
|
30
|
+
defaultTrigger: '${trigger}',
|
|
31
|
+
errorMessages: {
|
|
32
|
+
required: '${language === 'tr' ? 'Bu alan zorunludur' : 'This field is required'}',
|
|
33
|
+
email: '${language === 'tr' ? 'Geçersiz e-posta' : 'Invalid email address'}'
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
const targetPath = path.join(process.cwd(), 'src', 'app', 'smart-errors.config.ts');
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
if (!fs.existsSync(path.dirname(targetPath))) {
|
|
42
|
+
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
43
|
+
}
|
|
44
|
+
fs.writeFileSync(targetPath, config);
|
|
45
|
+
console.log(`\n✅ ${green('Success!')} Configuration generated at ${yellow('src/app/smart-errors.config.ts')}`);
|
|
46
|
+
console.log(`\nNext steps:`);
|
|
47
|
+
console.log(`1. Add 'smartErrorsConfig' to your app.config.ts providers.`);
|
|
48
|
+
console.log(`2. Import NgxErrorDirective/Component in your components.`);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
console.error(`\n❌ Error writing file: ${err.message}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
rl.close();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function ask(question, defaultValue) {
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
rl.question(`${question} ${blue(`(${defaultValue})`)}: `, (answer) => {
|
|
59
|
+
resolve(answer || defaultValue);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
run();
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ngx-smart-errors",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Modern, signal-based Angular form validation error handling. Zero boilerplate, global/local config, i18n, custom templates.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"angular",
|
|
7
|
+
"forms",
|
|
8
|
+
"validation",
|
|
9
|
+
"errors",
|
|
10
|
+
"reactive-forms",
|
|
11
|
+
"signals",
|
|
12
|
+
"i18n",
|
|
13
|
+
"form-validation",
|
|
14
|
+
"ngx",
|
|
15
|
+
"angular-library"
|
|
16
|
+
],
|
|
17
|
+
"author": "Burcin Eren",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/burcineren/ngx-smart-errors.git"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/burcineren/ngx-smart-errors#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/burcineren/ngx-smart-errors/issues"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@angular/common": ">=17.0.0",
|
|
29
|
+
"@angular/core": ">=17.0.0",
|
|
30
|
+
"@angular/forms": ">=17.0.0",
|
|
31
|
+
"rxjs": ">=7.0.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"tslib": "^2.3.0"
|
|
35
|
+
},
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"bin": {
|
|
38
|
+
"ngx-smart-errors": "cli/index.js"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|