defuss-runtime 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 - 2025 Aron Homberg
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,216 @@
1
+ <h1 align="center">
2
+
3
+ <img src="assets/defuss_mascott.png" width="100px" />
4
+
5
+ <p align="center">
6
+ <code>defuss-runtime</code>
7
+ </p>
8
+
9
+ <sup align="center">
10
+
11
+ Isomorphic JS Runtime API Enhancements
12
+
13
+ </sup>
14
+
15
+ </h1>
16
+
17
+
18
+ > `defuss-runtime` provides a set of isomorphic runtime API enhancements for JavaScript applications, including utilities for working with promises, arrays, objects, dates, functions and more. It is designed to be used in both Node.js and browser environments.
19
+
20
+
21
+ <h3 align="center">
22
+
23
+ Features
24
+
25
+ </h3>
26
+
27
+ <h3 align="center">
28
+
29
+ Basic Usage
30
+
31
+ </h3>
32
+
33
+ ```typescript
34
+ import { validate, validateAll } from 'defuss-runtime';
35
+
36
+ const formData = {
37
+ username: 'johndoe',
38
+ email: 'john@example.com',
39
+ age: 25
40
+ };
41
+
42
+ // Create validation chains for each field
43
+ const usernameChain = validate(formData, 'username')
44
+ .isRequired()
45
+ .isString()
46
+ .isLongerThan(3);
47
+
48
+ const emailChain = validate(formData, 'email')
49
+ .isRequired()
50
+ .isString()
51
+ .isEmail();
52
+
53
+ const ageChain = validate(formData, 'age')
54
+ .isRequired()
55
+ .isNumber()
56
+ .isGreaterThan(18);
57
+
58
+ // Validate all chains at once
59
+ const result = await validateAll([
60
+ usernameChain,
61
+ emailChain,
62
+ ageChain
63
+ ]);
64
+
65
+ // Check if all validations passed
66
+ if (await result.isValid()) {
67
+ console.log('All fields are valid!');
68
+ } else {
69
+ // Get all error messages grouped by field
70
+ const errors = await result.getErrors();
71
+ console.log('Validation errors:', errors);
72
+ }
73
+ ```
74
+
75
+ <h3 align="center">
76
+
77
+ Custom Validators
78
+
79
+ </h3>
80
+
81
+ You can register your own custom validators using the `ValidatorRegistry`:
82
+
83
+ ```typescript
84
+ import { ValidatorRegistry, validate, validateAll } from 'defuss-validate';
85
+
86
+ // Register a simple validator (takes only a value)
87
+ ValidatorRegistry.registerSimple(
88
+ 'isHexColor',
89
+ (value) => typeof value === 'string' && /^#([0-9A-F]{3}|[0-9A-F]{6})$/i.test(value),
90
+ 'Must be a valid hex color code'
91
+ );
92
+
93
+ // Register a parameterized validator (takes value plus parameters)
94
+ ValidatorRegistry.registerParameterized(
95
+ 'isDivisibleBy',
96
+ (value, divisor) => typeof value === 'number' && value % divisor === 0,
97
+ 'Must be divisible by {0}' // Use {0}, {1}, etc. for parameter placeholders
98
+ );
99
+
100
+ // Apply the registered validators to the ValidationChain
101
+ ValidatorRegistry.applyValidatorsToPrototype(ValidationChain.prototype);
102
+
103
+ // Use your custom validators
104
+ const colorChain = validate(formData, 'color').isHexColor();
105
+ const numberChain = validate(formData, 'value').isNumber().isDivisibleBy(2);
106
+
107
+ const result = await validateAll([colorChain, numberChain]);
108
+ ```
109
+
110
+ <h3 align="center">
111
+
112
+ Type-Safe Custom Validators with Generics
113
+
114
+ </h3>
115
+
116
+ To make your custom validators type-safe with proper TypeScript support:
117
+
118
+ ```typescript
119
+ import { ValidatorRegistry, ValidationChain, validate } from 'defuss-validate';
120
+ import type { SimpleValidators, ParameterizedValidators } from 'defuss-validate/extend-types';
121
+
122
+ // 1. Define interfaces for your custom validators
123
+ interface HexColorValidator<T> extends SimpleValidators<T> {
124
+ isHexColor(): T;
125
+ }
126
+
127
+ interface DivisibleByValidator<T> extends ParameterizedValidators<T> {
128
+ isDivisibleBy(divisor: number): T;
129
+ }
130
+
131
+ // 2. Create a custom ValidationChain type with your validators
132
+ type CustomValidationChain<T = any> = ValidationChain<
133
+ T,
134
+ HexColorValidator<any>,
135
+ DivisibleByValidator<any>
136
+ >;
137
+
138
+ // 3. Register your custom validators
139
+ ValidatorRegistry.registerSimple(
140
+ 'isHexColor',
141
+ (value) => typeof value === 'string' && /^#([0-9A-F]{3}|[0-9A-F]{6})$/i.test(value),
142
+ 'Must be a valid hex color code'
143
+ );
144
+
145
+ ValidatorRegistry.registerParameterized(
146
+ 'isDivisibleBy',
147
+ (value, divisor) => typeof value === 'number' && value % divisor === 0,
148
+ 'Must be divisible by {0}'
149
+ );
150
+
151
+ // 4. Apply validators to the ValidationChain prototype
152
+ ValidatorRegistry.applyValidatorsToPrototype(ValidationChain.prototype);
153
+
154
+ // 5. Create a helper function to use your custom ValidationChain
155
+ function customValidate<T = any>(data: T, fieldPath: string): CustomValidationChain<T> {
156
+ return validate<T, HexColorValidator<any>, DivisibleByValidator<any>>(data, fieldPath);
157
+ }
158
+
159
+ // 6. Now you can use your custom validators with full type safety!
160
+ const colorChain = customValidate(formData, 'color')
161
+ .isRequired()
162
+ .isString()
163
+ .isHexColor(); // TypeScript now recognizes this method
164
+
165
+ const numberChain = customValidate(formData, 'value')
166
+ .isRequired()
167
+ .isNumber()
168
+ .isDivisibleBy(2); // TypeScript now recognizes this method with parameters
169
+ ```
170
+
171
+ <h3 align="center">
172
+
173
+ Available Validators
174
+
175
+ </h3>
176
+
177
+ ### Simple Validators (no parameters)
178
+ - `isRequired()` - Checks if a value is not undefined, null, or empty string
179
+ - `isString()` - Checks if a value is a string
180
+ - `isNumber()` - Checks if a value is a number
181
+ - `isEmail()` - Checks if a value is a valid email address
182
+ - `isUrl()` - Checks if a value is a valid URL
183
+ - `isDate()` - Checks if a value is a valid date
184
+ - `isArray()` - Checks if a value is an array
185
+ - `isObject()` - Checks if a value is an object
186
+ - `isEmpty()` - Checks if a value is empty
187
+ - `isNumeric()` - Checks if a value is numeric
188
+ - `isPhoneNumber()` - Checks if a value is a valid phone number
189
+ - `isSlug()` - Checks if a value is a valid slug
190
+ - `isUrlPath()` - Checks if a value is a valid URL path
191
+
192
+ ### Parameterized Validators (with parameters)
193
+ - `isLongerThan(minLength)` - Checks if a string is longer than the specified length
194
+ - `isShorterThan(maxLength)` - Checks if a string is shorter than the specified length
195
+ - `isGreaterThan(minValue)` - Checks if a number is greater than the specified value
196
+ - `isLessThan(maxValue)` - Checks if a number is lower than the specified value
197
+ - `hasPattern(pattern)` - Checks if a string matches the specified regex pattern
198
+ - `isEqual(compareValue)` - Checks if a value is equal to another value
199
+ - `isOneOf(allowedValues)` - Checks if a value is one of the allowed values
200
+ - `isAfter(minDate)` - Checks if a date is after the minimum date
201
+ - `isBefore(maxDate)` - Checks if a date is before the maximum date
202
+
203
+ ## 🧞 Commands
204
+
205
+ All commands are run from the root of the project, from a terminal:
206
+
207
+ | Command | Action |
208
+ | :------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
209
+ | `npm build` | Build a new version of the library. |
210
+ | `npm test` | Run the tests for the `defuss-validate` package. |
211
+
212
+ ---
213
+
214
+ <img src="assets/defuss_comic.png" />
215
+
216
+ <caption><i><b>Come visit us on defuss island!</b></i></caption>