structure-verifier 0.0.14 → 0.0.16
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 +299 -325
- package/dist/index.d.ts +50 -18
- package/dist/index.js +20 -15
- package/dist/index.js.map +1 -1
- package/dist/src/error/v_error.d.ts +9 -4
- package/dist/src/error/v_error.js +14 -7
- package/dist/src/error/v_error.js.map +1 -1
- package/dist/src/interfaces/types.d.ts +3 -3
- package/dist/src/languages/message.d.ts +1 -1
- package/dist/src/languages/message.js +7 -4
- package/dist/src/languages/message.js.map +1 -1
- package/dist/src/verifiers/any/v_any.d.ts +1 -2
- package/dist/src/verifiers/any/v_any.js +8 -3
- package/dist/src/verifiers/any/v_any.js.map +1 -1
- package/dist/src/verifiers/array/v_array.d.ts +7 -6
- package/dist/src/verifiers/array/v_array.js +44 -33
- package/dist/src/verifiers/array/v_array.js.map +1 -1
- package/dist/src/verifiers/boolean/v_boolean.d.ts +1 -2
- package/dist/src/verifiers/boolean/v_boolean.js +30 -21
- package/dist/src/verifiers/boolean/v_boolean.js.map +1 -1
- package/dist/src/verifiers/date/v_date.d.ts +1 -2
- package/dist/src/verifiers/date/v_date.js +68 -47
- package/dist/src/verifiers/date/v_date.js.map +1 -1
- package/dist/src/verifiers/number/v_number.d.ts +1 -2
- package/dist/src/verifiers/number/v_number.js +30 -52
- package/dist/src/verifiers/number/v_number.js.map +1 -1
- package/dist/src/verifiers/object/v_object.d.ts +14 -15
- package/dist/src/verifiers/object/v_object.js +98 -68
- package/dist/src/verifiers/object/v_object.js.map +1 -1
- package/dist/src/verifiers/string/v_string.d.ts +1 -2
- package/dist/src/verifiers/string/v_string.js +73 -85
- package/dist/src/verifiers/string/v_string.js.map +1 -1
- package/dist/src/verifiers/type.d.ts +1 -0
- package/dist/src/verifiers/uuid/v_uuid.d.ts +1 -2
- package/dist/src/verifiers/uuid/v_uuid.js +83 -46
- package/dist/src/verifiers/uuid/v_uuid.js.map +1 -1
- package/dist/src/verifiers/verifier.js +1 -1
- package/dist/src/verifiers/verifier.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,422 +1,396 @@
|
|
|
1
|
-
# structure-verifier 0.0.
|
|
1
|
+
# structure-verifier 0.0.16
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Libreria TypeScript para validar estructuras de datos con tipado inferido.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Permite validar valores simples y estructuras anidadas (objetos y arrays), con errores detallados por ruta (por ejemplo: name, items[0], address.city).
|
|
6
|
+
|
|
7
|
+
## Instalacion
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
|
|
10
|
+
npm install structure-verifier
|
|
9
11
|
```
|
|
10
12
|
|
|
11
|
-
##
|
|
13
|
+
## Inicio rapido
|
|
12
14
|
|
|
13
|
-
```
|
|
15
|
+
```ts
|
|
14
16
|
import { Verifiers as V, VerificationError } from "structure-verifier";
|
|
15
|
-
//////////Verificator object creation
|
|
16
|
-
const v = new V.Number();
|
|
17
|
-
/////////Running validations
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
18
|
+
const userValidator = V.ObjectNotNull({
|
|
19
|
+
id: V.UUIDNotNull({ version: 4 }),
|
|
20
|
+
name: V.StringNotNull({ minLength: 2 }),
|
|
21
|
+
age: V.Number({ min: 0 }),
|
|
22
|
+
active: V.BooleanNotNull(),
|
|
23
|
+
});
|
|
25
24
|
|
|
26
25
|
try {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
const user = userValidator.check({
|
|
27
|
+
id: "550e8400-e29b-41d4-a716-446655440000",
|
|
28
|
+
name: "Ana",
|
|
29
|
+
age: 31,
|
|
30
|
+
active: "true",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// user queda tipado con los tipos inferidos de cada propiedad
|
|
34
|
+
console.log(user);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error instanceof VerificationError) {
|
|
37
|
+
console.log(error.errors); // mensajes planos
|
|
38
|
+
console.log(error.errorsObj); // mensajes con key y metadatos
|
|
39
|
+
}
|
|
31
40
|
}
|
|
32
41
|
```
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
```
|
|
39
|
-
import {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
43
|
+
## API publica
|
|
44
|
+
|
|
45
|
+
Import principal:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import {
|
|
49
|
+
Verifiers,
|
|
50
|
+
VerificationError,
|
|
51
|
+
Verifier,
|
|
52
|
+
InferType,
|
|
53
|
+
InferFactoryType,
|
|
54
|
+
VAny,
|
|
55
|
+
VAnyConditions,
|
|
56
|
+
VArray,
|
|
57
|
+
VArrayNotNull,
|
|
58
|
+
VArrayConditions,
|
|
59
|
+
VBoolean,
|
|
60
|
+
VBooleanNotNull,
|
|
61
|
+
VBooleanConditions,
|
|
62
|
+
VDate,
|
|
63
|
+
VDateNotNull,
|
|
64
|
+
VDateConditions,
|
|
65
|
+
VNumber,
|
|
66
|
+
VNumberNotNull,
|
|
67
|
+
VNumberConditions,
|
|
68
|
+
VObject,
|
|
69
|
+
VObjectNotNull,
|
|
70
|
+
VObjectConditions,
|
|
71
|
+
VObjectConditionsNotNull,
|
|
72
|
+
VString,
|
|
73
|
+
VStringNotNull,
|
|
74
|
+
VStringConditions,
|
|
75
|
+
VUUID,
|
|
76
|
+
VUUIDNotNull,
|
|
77
|
+
VUUIDConditions,
|
|
78
|
+
datetime,
|
|
79
|
+
} from "structure-verifier";
|
|
57
80
|
```
|
|
58
|
-
## Types
|
|
59
|
-
In case it is necessary to infer the type of the response, it is achieved by using InferType
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
import { InferType, Verifiers as V } from "structure-verifier";
|
|
63
|
-
|
|
64
|
-
const val = new V.Number();
|
|
65
|
-
type valType = InferType<typeof val>;
|
|
66
|
-
|
|
67
|
-
function action(data:valType){
|
|
68
|
-
...
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
## Validations
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
Validations to numerical data
|
|
75
|
-
```typescript
|
|
76
|
-
const numberVal = new V.Number();////return number|null
|
|
77
|
-
const notNullNumberVal = new V.NumberNotNull() ////return number
|
|
78
|
-
```
|
|
79
|
-
Number exclusive conditions
|
|
80
|
-
|
|
81
|
-
- **min:** - Used to denote the smallest valid number.
|
|
82
|
-
- **max:** - Used to denote the biggest valid number.
|
|
83
|
-
- **in:** - Used to denote an array of valid numbers that the value must be one of.
|
|
84
|
-
- **notIn:** - Used to denote an array of numbers that the value must not be one of.
|
|
85
|
-
- **maxDecimalPlaces:** - Used to denote the maximum number of decimal places allowed.
|
|
86
|
-
- **minDecimalPlaces:** - Used to denote the minimum number of decimal places required.
|
|
87
|
-
|
|
88
|
-
#### Example
|
|
89
|
-
```typescript
|
|
90
|
-
const numberVal = new V.Number({
|
|
91
|
-
min:10,
|
|
92
|
-
max:20,
|
|
93
|
-
in: [15,16,17],
|
|
94
|
-
notIn: [18,19,20],
|
|
95
|
-
maxDecimalPlaces: 0,
|
|
96
|
-
minDecimalPlaces: 0
|
|
97
|
-
});
|
|
98
|
-
/////Validate a number or null that meets all conditions otherwise error (VerificationError)
|
|
99
|
-
```
|
|
100
|
-
***
|
|
101
|
-
### Strings
|
|
102
|
-
Validations for string data.
|
|
82
|
+
## Formas de uso
|
|
103
83
|
|
|
104
|
-
|
|
105
|
-
const stringVal = new V.String(); // Returns string | null
|
|
106
|
-
const notNullStringVal = new V.StringNotNull(); // Returns string
|
|
107
|
-
```
|
|
108
|
-
String Exclusive Conditions
|
|
109
|
-
- **minLength:** - Specifies the minimum length the string must have.
|
|
110
|
-
- **maxLength:** - Specifies the maximum length the string can have.
|
|
111
|
-
- **regex:** - Specifies a regular expression pattern the string must match.
|
|
112
|
-
- **notRegex:** - Specifies a regular expression pattern the string must not match.
|
|
113
|
-
- **in:** - Specifies an array of valid string values; the string must be one of these values.
|
|
114
|
-
- **notIn:** - Specifies an array of string values that the string must not be one of.
|
|
115
|
-
- **strictMode:** - When true, ensures the value is strictly a string (not coerced from another type).
|
|
116
|
-
- **ignoreCase:** - When true, makes the in condition case-insensitive.
|
|
117
|
-
|
|
118
|
-
#### Example
|
|
119
|
-
```typescript
|
|
120
|
-
const stringVal = new V.String({
|
|
121
|
-
minLength: 5,
|
|
122
|
-
maxLength: 10,
|
|
123
|
-
regex: /^[a-zA-Z]+$/,
|
|
124
|
-
notRegex: /[^a-zA-Z]/,
|
|
125
|
-
in: ['apple', 'banana', 'cherry'],
|
|
126
|
-
notIn: ['date', 'fig', 'grape'],
|
|
127
|
-
strictMode: true,
|
|
128
|
-
ignoreCase: true
|
|
129
|
-
});
|
|
130
|
-
///// Validate a string or null that meets all conditions otherwise error (VerificationError)
|
|
131
|
-
```
|
|
132
|
-
***
|
|
133
|
-
### Booleans
|
|
134
|
-
Validations for boolean data.
|
|
84
|
+
La libreria soporta dos estilos:
|
|
135
85
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const notNullBooleanVal = new V.BooleanNotNull(); // Returns boolean
|
|
139
|
-
```
|
|
86
|
+
1. API recomendada con Verifiers (callable, con o sin new)
|
|
87
|
+
2. Clases directas (new VString(...), new VObject(...), etc.)
|
|
140
88
|
|
|
141
|
-
|
|
89
|
+
Ejemplo con Verifiers:
|
|
142
90
|
|
|
143
|
-
|
|
91
|
+
```ts
|
|
92
|
+
import { Verifiers as V } from "structure-verifier";
|
|
144
93
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const booleanVal = new V.Boolean();
|
|
148
|
-
const notNullBooleanVal = new V.BooleanNotNull();
|
|
94
|
+
const a = V.StringNotNull({ minLength: 3 });
|
|
95
|
+
const b = new V.StringNotNull({ minLength: 3 });
|
|
149
96
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
console.log(booleanVal.check('FALSE')); // Output: false
|
|
153
|
-
console.log(booleanVal.check(null)); // Output: null
|
|
154
|
-
console.log(notNullBooleanVal.check('1')); // Output: true
|
|
155
|
-
console.log(notNullBooleanVal.check(0)); // Output: false
|
|
156
|
-
} catch (error) {
|
|
157
|
-
console.error(error);
|
|
158
|
-
}
|
|
97
|
+
console.log(a.check("hola"));
|
|
98
|
+
console.log(b.check("hola"));
|
|
159
99
|
```
|
|
160
|
-
***
|
|
161
|
-
### Objects
|
|
162
|
-
Validations for object data.
|
|
163
100
|
|
|
164
|
-
|
|
165
|
-
const objectVal = new V.Object({ properties: { name: new V.String({ minLength: 3 })/* properties with validations */ } }); // Returns object {name:""} | null
|
|
166
|
-
const notNullObjectVal = new V.ObjectNotNull({ properties: { name: new V.String({ minLength: 3 })/* properties with validations */ } }); // Returns object {name:""}
|
|
167
|
-
```
|
|
101
|
+
## Inferencia de tipos
|
|
168
102
|
|
|
169
|
-
|
|
103
|
+
### InferType
|
|
170
104
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
- **ignoreCase:** - When `true`, makes the property names case-insensitive.
|
|
174
|
-
- **takeAllValues:** - When `true`, allows taking all values from the input object, not just those defined in the properties.
|
|
105
|
+
```ts
|
|
106
|
+
import { InferType, Verifiers as V } from "structure-verifier";
|
|
175
107
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
properties: {
|
|
180
|
-
name: new V.String({ minLength: 3 }),
|
|
181
|
-
age: new V.Number({ min: 18, max: 99 }),
|
|
182
|
-
},
|
|
183
|
-
strictMode: true,
|
|
184
|
-
ignoreCase: true,
|
|
185
|
-
invalidPropertyMessage: {
|
|
186
|
-
message: () => "no es una propiedad valida",
|
|
187
|
-
val: undefined
|
|
188
|
-
}
|
|
108
|
+
const v = V.ObjectNotNull({
|
|
109
|
+
id: V.UUIDNotNull(),
|
|
110
|
+
tags: V.ArrayNotNull(V.StringNotNull()),
|
|
189
111
|
});
|
|
190
112
|
|
|
191
|
-
|
|
192
|
-
properties: {
|
|
193
|
-
name: new V.StringNotNull({ minLength: 3 }),
|
|
194
|
-
age: new V.NumberNotNull({ min: 18, max: 99 }),
|
|
195
|
-
},
|
|
196
|
-
strictMode: true,
|
|
197
|
-
ignoreCase: true,
|
|
198
|
-
invalidPropertyMessage: {
|
|
199
|
-
message: () => "no es una propiedad valida",
|
|
200
|
-
val: undefined
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
console.log(objectVal.check({ name: 'John', age: 25 })); // Output: { name: 'John', age: 25 }
|
|
206
|
-
console.log(objectVal.check(null)); // Output: null
|
|
207
|
-
console.log(notNullObjectVal.check({ name: 'Jane', age: 30 })); // Output: { name: 'Jane', age: 30 }
|
|
208
|
-
} catch (error) {
|
|
209
|
-
console.error(error);
|
|
210
|
-
}
|
|
113
|
+
type User = InferType<typeof v>;
|
|
211
114
|
```
|
|
212
|
-
***
|
|
213
|
-
### Arrays
|
|
214
|
-
Validations for array data.
|
|
215
115
|
|
|
216
|
-
|
|
217
|
-
const arrayVal = new V.Array({verifier: new V.Number()}); // Returns Array | null
|
|
218
|
-
const notNullArrayVal = new V.ArrayNotNull({verifier: new V.Number()}); // Returns Array
|
|
219
|
-
```
|
|
116
|
+
### InferFactoryType
|
|
220
117
|
|
|
221
|
-
|
|
118
|
+
Funciona con funciones que retornan un Verifier (por ejemplo, miembros callable de Verifiers).
|
|
222
119
|
|
|
223
|
-
|
|
224
|
-
|
|
120
|
+
```ts
|
|
121
|
+
import { InferFactoryType, Verifiers as V } from "structure-verifier";
|
|
122
|
+
|
|
123
|
+
type NullableNumber = InferFactoryType<typeof V.Number>; // number | null
|
|
124
|
+
type RequiredNumber = InferFactoryType<typeof V.NumberNotNull>; // number
|
|
125
|
+
```
|
|
225
126
|
|
|
226
|
-
|
|
227
|
-
```typescript
|
|
228
|
-
const arrayVal = new V.Array({ verifier: new V.Number(), minLength: 1, maxLength: 5 });
|
|
229
|
-
const notNullArrayVal = new V.ArrayNotNull({ verifier: new V.Number(), minLength: 2 });
|
|
127
|
+
## Manejo de errores
|
|
230
128
|
|
|
129
|
+
Cuando una validacion falla se lanza VerificationError.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
231
132
|
try {
|
|
232
|
-
|
|
233
|
-
console.log(arrayVal.check([])); // Throws VerificationError (array too short)
|
|
234
|
-
console.log(arrayVal.check(null)); // Output: null
|
|
235
|
-
console.log(notNullArrayVal.check([1, 2])); // Output: [1, 2]
|
|
236
|
-
console.log(notNullArrayVal.check([1])); // Throws VerificationError (array too short)
|
|
133
|
+
V.NumberNotNull({ min: 10 }).check(5);
|
|
237
134
|
} catch (error) {
|
|
238
|
-
|
|
135
|
+
if (error instanceof VerificationError) {
|
|
136
|
+
console.log(error.errors); // ["debe ser mayor o igual a 10"]
|
|
137
|
+
console.log(error.errorsObj); // [{ key: "", message: "..." }]
|
|
138
|
+
}
|
|
239
139
|
}
|
|
240
140
|
```
|
|
241
|
-
***
|
|
242
|
-
### Any
|
|
243
|
-
Validations for any data.
|
|
244
141
|
|
|
245
|
-
|
|
246
|
-
const anyVal = new V.Any(); // Returns any type
|
|
247
|
-
```
|
|
142
|
+
## Opciones comunes
|
|
248
143
|
|
|
249
|
-
|
|
144
|
+
Muchos validadores comparten estas opciones:
|
|
250
145
|
|
|
251
|
-
|
|
146
|
+
- isRequired: fuerza que el valor exista.
|
|
147
|
+
- emptyAsNull: si llega string vacio, se convierte a null antes de validar.
|
|
148
|
+
- defaultValue: valor por defecto si llega undefined (y en algunos flujos tambien null).
|
|
149
|
+
- badTypeMessage: mensaje personalizado de tipo invalido.
|
|
252
150
|
|
|
253
|
-
|
|
254
|
-
```typescript
|
|
255
|
-
const anyVal = new V.Any();
|
|
151
|
+
Puedes pasar mensajes simples o mensajes dinamicos con estructura:
|
|
256
152
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
console.log(anyVal.check('FALSE')); // Output: 'FALSE'
|
|
260
|
-
console.log(anyVal.check(null)); // Output: null
|
|
261
|
-
console.log(anyVal.check('1')); // Output: '1'
|
|
262
|
-
console.log(anyVal.check(0)); // Output: 0
|
|
263
|
-
} catch (error) {
|
|
264
|
-
console.error(error);
|
|
265
|
-
}
|
|
153
|
+
```ts
|
|
154
|
+
{ val: valorReal, message: (values) => "mensaje" }
|
|
266
155
|
```
|
|
267
|
-
***
|
|
268
|
-
### Date
|
|
269
|
-
Validations for date data (depends Moment).
|
|
270
156
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
157
|
+
## Validadores
|
|
158
|
+
|
|
159
|
+
### Number / NumberNotNull
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
const n1 = V.Number();
|
|
163
|
+
const n2 = V.NumberNotNull();
|
|
274
164
|
```
|
|
275
165
|
|
|
276
|
-
|
|
166
|
+
Condiciones:
|
|
277
167
|
|
|
278
|
-
-
|
|
279
|
-
-
|
|
280
|
-
-
|
|
281
|
-
-
|
|
168
|
+
- min
|
|
169
|
+
- max
|
|
170
|
+
- in
|
|
171
|
+
- notIn
|
|
172
|
+
- maxDecimalPlaces
|
|
173
|
+
- minDecimalPlaces
|
|
174
|
+
- isRequired
|
|
175
|
+
- emptyAsNull
|
|
176
|
+
- defaultValue
|
|
177
|
+
- badTypeMessage
|
|
282
178
|
|
|
283
|
-
|
|
284
|
-
#### Basic Date Validation
|
|
179
|
+
Notas:
|
|
285
180
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
console.log(vdate.check("2023-08-09")?.format("YYYY-MM-DD")); // Output: "2023-08-09"
|
|
289
|
-
```
|
|
181
|
+
- Convierte con Number(data), por lo que acepta strings numericos como "42".
|
|
182
|
+
- "" y NaN fallan.
|
|
290
183
|
|
|
291
|
-
|
|
184
|
+
### String / StringNotNull
|
|
292
185
|
|
|
293
|
-
```
|
|
294
|
-
const
|
|
295
|
-
|
|
186
|
+
```ts
|
|
187
|
+
const s1 = V.String();
|
|
188
|
+
const s2 = V.StringNotNull();
|
|
296
189
|
```
|
|
297
190
|
|
|
298
|
-
|
|
191
|
+
Condiciones:
|
|
299
192
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
193
|
+
- minLength
|
|
194
|
+
- maxLength
|
|
195
|
+
- regex
|
|
196
|
+
- notRegex
|
|
197
|
+
- in
|
|
198
|
+
- notIn
|
|
199
|
+
- strictMode
|
|
200
|
+
- ignoreCase
|
|
201
|
+
- isRequired
|
|
202
|
+
- emptyAsNull
|
|
203
|
+
- defaultValue
|
|
204
|
+
- badTypeMessage
|
|
305
205
|
|
|
306
|
-
|
|
206
|
+
Notas:
|
|
307
207
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
208
|
+
- En modo no estricto convierte con String(data).
|
|
209
|
+
- En strictMode exige typeof data === "string".
|
|
210
|
+
- ignoreCase afecta in y notIn.
|
|
211
|
+
|
|
212
|
+
### Boolean / BooleanNotNull
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const b1 = V.Boolean();
|
|
216
|
+
const b2 = V.BooleanNotNull();
|
|
314
217
|
```
|
|
315
218
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
219
|
+
Condiciones:
|
|
220
|
+
|
|
221
|
+
- strictMode
|
|
222
|
+
- isRequired
|
|
223
|
+
- emptyAsNull
|
|
224
|
+
- defaultValue
|
|
225
|
+
- badTypeMessage
|
|
319
226
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
227
|
+
Notas:
|
|
228
|
+
|
|
229
|
+
- En modo no estricto acepta: true, false, 1, 0, "1", "0", "true", "false" (case-insensitive).
|
|
230
|
+
- En strictMode solo acepta boolean real.
|
|
231
|
+
|
|
232
|
+
### UUID / UUIDNotNull
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
const u1 = V.UUID();
|
|
236
|
+
const u2 = V.UUIDNotNull();
|
|
323
237
|
```
|
|
324
238
|
|
|
325
|
-
|
|
239
|
+
Condiciones:
|
|
240
|
+
|
|
241
|
+
- version (1 | 2 | 3 | 4 | 5)
|
|
242
|
+
- allowNoHyphens
|
|
243
|
+
- strictMode
|
|
244
|
+
- isRequired
|
|
245
|
+
- emptyAsNull
|
|
246
|
+
- defaultValue
|
|
247
|
+
- badTypeMessage
|
|
326
248
|
|
|
327
|
-
|
|
328
|
-
- **allowNoHyphens**: When `true`, allows UUIDs without hyphens. Default is `false`.
|
|
329
|
-
- **strictMode**: When `true`, ensures the input value is strictly a string (not coerced from another type).
|
|
249
|
+
Notas:
|
|
330
250
|
|
|
331
|
-
|
|
251
|
+
- Devuelve UUID normalizado en minusculas y con guiones.
|
|
252
|
+
- Si allowNoHyphens es false, exige formato 8-4-4-4-12.
|
|
332
253
|
|
|
333
|
-
|
|
254
|
+
### Date / DateNotNull
|
|
334
255
|
|
|
335
|
-
```
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
256
|
+
```ts
|
|
257
|
+
import { Verifiers as V, datetime } from "structure-verifier";
|
|
258
|
+
|
|
259
|
+
const d1 = V.Date();
|
|
260
|
+
const d2 = V.DateNotNull({
|
|
261
|
+
format: "YYYY-MM-DD",
|
|
262
|
+
timeZone: "UTC",
|
|
263
|
+
minDate: datetime("2024-01-01"),
|
|
264
|
+
maxDate: datetime("2026-12-31"),
|
|
265
|
+
});
|
|
339
266
|
```
|
|
340
267
|
|
|
341
|
-
|
|
268
|
+
Condiciones:
|
|
269
|
+
|
|
270
|
+
- format
|
|
271
|
+
- timeZone
|
|
272
|
+
- minDate
|
|
273
|
+
- maxDate
|
|
274
|
+
- default
|
|
275
|
+
- isRequired
|
|
276
|
+
- emptyAsNull
|
|
277
|
+
- defaultValue
|
|
278
|
+
- badTypeMessage
|
|
279
|
+
|
|
280
|
+
Notas:
|
|
342
281
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
282
|
+
- Retorna instancias dayjs (re-exportado como datetime).
|
|
283
|
+
- Inputs soportados: number, string, Date y datetime.Dayjs.
|
|
284
|
+
- timeZone por defecto: UTC.
|
|
285
|
+
|
|
286
|
+
### Any
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
const anyVal = V.Any();
|
|
346
290
|
```
|
|
347
291
|
|
|
348
|
-
|
|
292
|
+
Condiciones:
|
|
293
|
+
|
|
294
|
+
- isRequired
|
|
295
|
+
- emptyAsNull
|
|
296
|
+
- defaultValue
|
|
297
|
+
- badTypeMessage
|
|
298
|
+
|
|
299
|
+
### Array / ArrayNotNull
|
|
349
300
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
301
|
+
Firma:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
const arr1 = V.Array(verifier, conditions?);
|
|
305
|
+
const arr2 = V.ArrayNotNull(verifier, conditions?);
|
|
353
306
|
```
|
|
354
307
|
|
|
355
|
-
|
|
308
|
+
Ejemplo:
|
|
356
309
|
|
|
357
|
-
```
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
} catch (error) {
|
|
363
|
-
console.error(error);
|
|
364
|
-
}
|
|
310
|
+
```ts
|
|
311
|
+
const numbers = V.ArrayNotNull(V.NumberNotNull(), {
|
|
312
|
+
minLength: 1,
|
|
313
|
+
maxLength: 5,
|
|
314
|
+
});
|
|
365
315
|
```
|
|
366
316
|
|
|
367
|
-
|
|
368
|
-
## VerificationError
|
|
317
|
+
Condiciones:
|
|
369
318
|
|
|
370
|
-
|
|
319
|
+
- minLength
|
|
320
|
+
- maxLength
|
|
321
|
+
- isRequired
|
|
322
|
+
- emptyAsNull
|
|
323
|
+
- defaultValue
|
|
324
|
+
- badTypeMessage
|
|
371
325
|
|
|
372
|
-
|
|
326
|
+
Notas:
|
|
373
327
|
|
|
374
|
-
|
|
328
|
+
- Valida item por item con el verificador interno.
|
|
329
|
+
- En errores anidados utiliza claves como [0], [1].name, etc.
|
|
375
330
|
|
|
376
|
-
|
|
377
|
-
import { VerificationError } from "./path/to/your/VerificationError";
|
|
378
|
-
```
|
|
331
|
+
### Object / ObjectNotNull
|
|
379
332
|
|
|
380
|
-
|
|
333
|
+
Firma:
|
|
381
334
|
|
|
382
|
-
|
|
335
|
+
```ts
|
|
336
|
+
const o1 = V.Object(properties, conditions?);
|
|
337
|
+
const o2 = V.ObjectNotNull(properties, conditions?);
|
|
338
|
+
```
|
|
383
339
|
|
|
384
|
-
|
|
340
|
+
Ejemplo:
|
|
385
341
|
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}
|
|
342
|
+
```ts
|
|
343
|
+
const user = V.ObjectNotNull(
|
|
344
|
+
{
|
|
345
|
+
name: V.StringNotNull({ minLength: 3 }),
|
|
346
|
+
age: V.NumberNotNull({ min: 18 }),
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
strictMode: true,
|
|
350
|
+
ignoreCase: true,
|
|
351
|
+
takeAllValues: false,
|
|
352
|
+
},
|
|
353
|
+
);
|
|
392
354
|
```
|
|
393
355
|
|
|
394
|
-
|
|
356
|
+
Condiciones:
|
|
395
357
|
|
|
396
|
-
-
|
|
397
|
-
-
|
|
358
|
+
- invalidPropertyMessage
|
|
359
|
+
- strictMode
|
|
360
|
+
- ignoreCase
|
|
361
|
+
- takeAllValues
|
|
362
|
+
- conds
|
|
363
|
+
- isRequired
|
|
364
|
+
- emptyAsNull
|
|
365
|
+
- defaultValue
|
|
366
|
+
- badTypeMessage
|
|
398
367
|
|
|
399
|
-
|
|
368
|
+
Notas:
|
|
400
369
|
|
|
401
|
-
|
|
370
|
+
- strictMode rechaza propiedades extra.
|
|
371
|
+
- ignoreCase permite mapear propiedades sin importar mayusculas/minusculas.
|
|
372
|
+
- takeAllValues conserva propiedades no definidas en el esquema.
|
|
373
|
+
- conds ejecuta una validacion adicional al final con el objeto ya tipado.
|
|
402
374
|
|
|
403
|
-
|
|
375
|
+
## datetime (dayjs)
|
|
404
376
|
|
|
405
|
-
|
|
377
|
+
La libreria re-exporta dayjs como datetime con plugins:
|
|
406
378
|
|
|
407
|
-
|
|
379
|
+
- utc
|
|
380
|
+
- timezone
|
|
381
|
+
- customParseFormat
|
|
408
382
|
|
|
409
|
-
|
|
383
|
+
Ejemplo:
|
|
410
384
|
|
|
411
|
-
```
|
|
412
|
-
import {
|
|
385
|
+
```ts
|
|
386
|
+
import { datetime } from "structure-verifier";
|
|
413
387
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
```
|
|
388
|
+
const now = datetime();
|
|
389
|
+
console.log(now.tz("UTC").format());
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
## Ejecutar tests del proyecto
|
|
393
|
+
|
|
394
|
+
```bash
|
|
395
|
+
npm test
|
|
396
|
+
```
|