@itrocks/email-address 0.0.5 → 0.0.7
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/README.md +155 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -7,3 +7,158 @@
|
|
|
7
7
|
# email-address
|
|
8
8
|
|
|
9
9
|
An @EmailAddress() decorator to validate that a property contains a valid email address.
|
|
10
|
+
|
|
11
|
+
*This documentation was written by an artificial intelligence and may contain errors or approximations.
|
|
12
|
+
It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
|
|
13
|
+
please feel free to contact the author of this package.*
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @itrocks/email-address
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
`@itrocks/email-address` provides a property decorator `@EmailAddress()`
|
|
24
|
+
that lets you mark a field as expected to contain a valid email address.
|
|
25
|
+
The decorator does not perform the validation itself: it adds metadata
|
|
26
|
+
that other parts of the framework (or your own code) can use to check
|
|
27
|
+
and enforce the value.
|
|
28
|
+
|
|
29
|
+
You can also read this metadata using the helper function
|
|
30
|
+
`emailAddressOf`, for example to build your own validation pipeline or
|
|
31
|
+
form‑rendering logic.
|
|
32
|
+
|
|
33
|
+
### Minimal example
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { EmailAddress } from '@itrocks/email-address'
|
|
37
|
+
|
|
38
|
+
class User {
|
|
39
|
+
@EmailAddress()
|
|
40
|
+
email = ''
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Here, the `email` property is marked as containing an email address.
|
|
45
|
+
A generic validator can then iterate over the properties decorated with
|
|
46
|
+
`@EmailAddress()` to verify that they match the expected format.
|
|
47
|
+
|
|
48
|
+
### Complete example with decorator lookup
|
|
49
|
+
|
|
50
|
+
In a typical use case, you combine this package with other components
|
|
51
|
+
that rely on decorators to generate forms or validate data.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import type { ObjectOrType } from '@itrocks/class-type'
|
|
55
|
+
import { EmailAddress, emailAddressOf } from '@itrocks/email-address'
|
|
56
|
+
|
|
57
|
+
class User {
|
|
58
|
+
@EmailAddress()
|
|
59
|
+
email = ''
|
|
60
|
+
|
|
61
|
+
// This property will not be considered an email address
|
|
62
|
+
name = ''
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function listEmailFields<T extends object>(type: ObjectOrType<T>): (keyof T)[] {
|
|
66
|
+
const result: (keyof T)[] = []
|
|
67
|
+
|
|
68
|
+
// Simplified example: iterate over known properties
|
|
69
|
+
for (const property of ['email', 'name'] as (keyof T)[]) {
|
|
70
|
+
if (emailAddressOf(type, property)) {
|
|
71
|
+
result.push(property)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return result
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Result: ['email']
|
|
79
|
+
const emailFields = listEmailFields(User)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
In practice, you will usually rely on helpers provided by other
|
|
83
|
+
`@itrocks/*` packages to navigate decorator metadata, instead of
|
|
84
|
+
manually listing properties as in this example.
|
|
85
|
+
|
|
86
|
+
## API
|
|
87
|
+
|
|
88
|
+
### `function EmailAddress<T extends object>(value?: boolean): DecorateCaller<T>`
|
|
89
|
+
|
|
90
|
+
Property decorator indicating that a field is expected to contain a
|
|
91
|
+
valid email address.
|
|
92
|
+
|
|
93
|
+
#### Parameters
|
|
94
|
+
|
|
95
|
+
- `value` *(optional, default: `true`)* – enables or disables the
|
|
96
|
+
marking of the property as an email address. In most cases you will
|
|
97
|
+
simply use `@EmailAddress()`. Passing `false` lets you remove or
|
|
98
|
+
ignore this marker in advanced scenarios.
|
|
99
|
+
|
|
100
|
+
#### Return value
|
|
101
|
+
|
|
102
|
+
- `DecorateCaller<T>` – function from `@itrocks/decorator/property`
|
|
103
|
+
used internally by TypeScript to apply the decorator on the target
|
|
104
|
+
property. You normally do not need to call it directly; you just apply
|
|
105
|
+
`@EmailAddress()` to the property.
|
|
106
|
+
|
|
107
|
+
#### Example
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
class ContactForm {
|
|
111
|
+
@EmailAddress()
|
|
112
|
+
replyTo = ''
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
### `function emailAddressOf<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): boolean`
|
|
119
|
+
|
|
120
|
+
Checks whether a given property is decorated with `@EmailAddress()`.
|
|
121
|
+
|
|
122
|
+
#### Parameters
|
|
123
|
+
|
|
124
|
+
- `target` – the class (`User`) or instance (`new User()`) that owns the
|
|
125
|
+
property to check.
|
|
126
|
+
- `property` – the name of the property to inspect.
|
|
127
|
+
|
|
128
|
+
#### Return value
|
|
129
|
+
|
|
130
|
+
- `boolean` – `true` if the property is currently marked as an email
|
|
131
|
+
address, `false` otherwise.
|
|
132
|
+
|
|
133
|
+
#### Example
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import type { ObjectOrType } from '@itrocks/class-type'
|
|
137
|
+
import { EmailAddress, emailAddressOf } from '@itrocks/email-address'
|
|
138
|
+
|
|
139
|
+
class User {
|
|
140
|
+
@EmailAddress()
|
|
141
|
+
email = ''
|
|
142
|
+
|
|
143
|
+
name = ''
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function isEmailProperty<T extends object>(type: ObjectOrType<T>, property: keyof T): boolean {
|
|
147
|
+
return emailAddressOf(type, property)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
isEmailProperty(User, 'email') // true
|
|
151
|
+
isEmailProperty(User, 'name') // false
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Typical use cases
|
|
155
|
+
|
|
156
|
+
- Mark properties in your domain models that must contain an email
|
|
157
|
+
address, so that a generic validator can verify them.
|
|
158
|
+
- Automatically generate email‑type form fields based on decorator
|
|
159
|
+
metadata.
|
|
160
|
+
- Build a validation or rendering system that relies on decorators to
|
|
161
|
+
infer the constraints associated with each property.
|
|
162
|
+
- Centralize the definition of what a “valid email address” is inside a
|
|
163
|
+
single component of your application or framework, while keeping model
|
|
164
|
+
annotations simple (`@EmailAddress()`).
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"description": "An @EmailAddress() decorator to validate that a property contains a valid email address",
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"typescript": "~5.
|
|
12
|
+
"typescript": "~5.9"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=18"
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"backend",
|
|
28
28
|
"decorator",
|
|
29
29
|
"email",
|
|
30
|
+
"email address",
|
|
30
31
|
"it.rocks",
|
|
31
32
|
"property",
|
|
32
33
|
"validation"
|
|
@@ -41,5 +42,5 @@
|
|
|
41
42
|
"build": "tsc"
|
|
42
43
|
},
|
|
43
44
|
"types": "./cjs/email-address.d.ts",
|
|
44
|
-
"version": "0.0.
|
|
45
|
+
"version": "0.0.7"
|
|
45
46
|
}
|