structure-verifier 0.0.16 → 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/README.md +318 -241
- package/dist/index.d.ts +8 -16
- package/dist/index.js.map +1 -1
- package/dist/src/error/v_error.js +4 -3
- package/dist/src/error/v_error.js.map +1 -1
- package/dist/src/verifiers/any/v_any.js +0 -1
- package/dist/src/verifiers/any/v_any.js.map +1 -1
- package/dist/src/verifiers/array/v_array.d.ts +21 -3
- package/dist/src/verifiers/array/v_array.js +24 -5
- package/dist/src/verifiers/array/v_array.js.map +1 -1
- package/dist/src/verifiers/boolean/v_boolean.d.ts +4 -0
- package/dist/src/verifiers/boolean/v_boolean.js +15 -5
- package/dist/src/verifiers/boolean/v_boolean.js.map +1 -1
- package/dist/src/verifiers/date/v_date.d.ts +26 -0
- package/dist/src/verifiers/date/v_date.js +28 -1
- package/dist/src/verifiers/date/v_date.js.map +1 -1
- package/dist/src/verifiers/helpers/conditionMessage.d.ts +3 -0
- package/dist/src/verifiers/helpers/conditionMessage.js +19 -0
- package/dist/src/verifiers/helpers/conditionMessage.js.map +1 -0
- package/dist/src/verifiers/number/v_number.d.ts +38 -0
- package/dist/src/verifiers/number/v_number.js +40 -1
- package/dist/src/verifiers/number/v_number.js.map +1 -1
- package/dist/src/verifiers/object/v_object.d.ts +7 -0
- package/dist/src/verifiers/object/v_object.js +21 -0
- package/dist/src/verifiers/object/v_object.js.map +1 -1
- package/dist/src/verifiers/string/v_string.d.ts +58 -0
- package/dist/src/verifiers/string/v_string.js +109 -11
- package/dist/src/verifiers/string/v_string.js.map +1 -1
- package/dist/src/verifiers/uuid/v_uuid.d.ts +8 -0
- package/dist/src/verifiers/uuid/v_uuid.js +35 -4
- package/dist/src/verifiers/uuid/v_uuid.js.map +1 -1
- package/dist/src/verifiers/verifier.d.ts +1 -0
- package/dist/src/verifiers/verifier.js +8 -0
- package/dist/src/verifiers/verifier.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,34 @@
|
|
|
1
|
-
# structure-verifier
|
|
1
|
+
# structure-verifier
|
|
2
2
|
|
|
3
|
-
Libreria
|
|
3
|
+
Libreria de validacion para TypeScript orientada a esquemas declarativos, tipado inferido y errores detallados por ruta.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Ideal para validar payloads de API, formularios, configuraciones y estructuras anidadas.
|
|
6
|
+
|
|
7
|
+
## Objetivo de esta documentacion
|
|
8
|
+
|
|
9
|
+
Este README está diseñado para ayudarte a:
|
|
10
|
+
|
|
11
|
+
- Comprender rápidamente cómo funciona la librería.
|
|
12
|
+
- Integrarla de forma sencilla en un proyecto real.
|
|
13
|
+
- Conocer el contrato público de la API antes de utilizarla en producción.
|
|
14
|
+
- Explorar ejemplos prácticos de validación y tipado.
|
|
15
|
+
- Entender las decisiones de diseño y el enfoque de la librería.
|
|
16
|
+
|
|
17
|
+
## Tabla de contenido
|
|
18
|
+
|
|
19
|
+
- [Instalacion](#instalacion)
|
|
20
|
+
- [Inicio rapido](#inicio-rapido)
|
|
21
|
+
- [Conceptos clave](#conceptos-clave)
|
|
22
|
+
- [API publica](#api-publica)
|
|
23
|
+
- [Estilos de uso](#estilos-de-uso)
|
|
24
|
+
- [Validadores disponibles](#validadores-disponibles)
|
|
25
|
+
- [Transformaciones](#transformaciones)
|
|
26
|
+
- [Tipado inferido](#tipado-inferido)
|
|
27
|
+
- [Manejo de errores](#manejo-de-errores)
|
|
28
|
+
- [datetime (dayjs)](#datetime-dayjs)
|
|
29
|
+
- [Buenas practicas para v1.0.0](#buenas-practicas-para-v100)
|
|
30
|
+
- [Scripts de desarrollo](#scripts-de-desarrollo)
|
|
31
|
+
- [Licencia](#licencia)
|
|
6
32
|
|
|
7
33
|
## Instalacion
|
|
8
34
|
|
|
@@ -15,34 +41,94 @@ npm install structure-verifier
|
|
|
15
41
|
```ts
|
|
16
42
|
import { Verifiers as V, VerificationError } from "structure-verifier";
|
|
17
43
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
const userVerifier = V.ObjectNotNull(
|
|
45
|
+
{
|
|
46
|
+
id: V.UUID({ version: 4 }).required(),
|
|
47
|
+
name: V.StringNotNull({ minLength: 2 }).trim(),
|
|
48
|
+
age: V.Number({ min: 0 }),
|
|
49
|
+
active: V.BooleanNotNull(),
|
|
50
|
+
tags: V.ArrayNotNull(V.StringNotNull(), { minLength: 1 }),
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
strictMode: true,
|
|
54
|
+
},
|
|
55
|
+
);
|
|
24
56
|
|
|
25
57
|
try {
|
|
26
|
-
const user =
|
|
58
|
+
const user = userVerifier.check({
|
|
27
59
|
id: "550e8400-e29b-41d4-a716-446655440000",
|
|
28
|
-
name: "Ana",
|
|
29
|
-
age: 31,
|
|
60
|
+
name: " Ana ",
|
|
61
|
+
age: "31",
|
|
30
62
|
active: "true",
|
|
63
|
+
tags: ["admin"],
|
|
31
64
|
});
|
|
32
65
|
|
|
33
|
-
// user queda tipado con los tipos inferidos de cada propiedad
|
|
34
66
|
console.log(user);
|
|
67
|
+
// {
|
|
68
|
+
// id: "550e8400-e29b-41d4-a716-446655440000",
|
|
69
|
+
// name: "Ana",
|
|
70
|
+
// age: 31,
|
|
71
|
+
// active: true,
|
|
72
|
+
// tags: ["admin"]
|
|
73
|
+
// }
|
|
35
74
|
} catch (error) {
|
|
36
75
|
if (error instanceof VerificationError) {
|
|
37
|
-
console.
|
|
38
|
-
console.
|
|
76
|
+
console.error(error.message);
|
|
77
|
+
console.error(error.errors);
|
|
78
|
+
console.error(error.errorsObj);
|
|
39
79
|
}
|
|
40
80
|
}
|
|
41
81
|
```
|
|
42
82
|
|
|
83
|
+
## Conceptos clave
|
|
84
|
+
|
|
85
|
+
### 1) Nullable vs NotNull
|
|
86
|
+
|
|
87
|
+
- `VNumber`, `VString`, `VBoolean`, `VDate`, `VUUID`, `VArray`, `VObject` y `VAny` pueden devolver `null`.
|
|
88
|
+
- Las variantes `NotNull` devuelven siempre tipo requerido.
|
|
89
|
+
- En validadores nullable puedes promover a requerido con `.required()`.
|
|
90
|
+
|
|
91
|
+
### 2) Inmutabilidad de reglas
|
|
92
|
+
|
|
93
|
+
Cada metodo de regla devuelve una nueva instancia del validador.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const base = V.StringNotNull();
|
|
97
|
+
const withMin = base.minLength(3);
|
|
98
|
+
|
|
99
|
+
// base no cambia
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 3) Reglas transversales
|
|
103
|
+
|
|
104
|
+
Muchos validadores comparten estas opciones:
|
|
105
|
+
|
|
106
|
+
- `isRequired`: fuerza existencia del valor.
|
|
107
|
+
- `emptyAsNull`: transforma `""` a `null` antes de validar.
|
|
108
|
+
- `defaultValue`: valor por defecto para `undefined` y ciertos casos de `null`.
|
|
109
|
+
- `badTypeMessage`: reemplaza mensaje de tipo invalido.
|
|
110
|
+
|
|
111
|
+
### 4) Mensajes personalizados
|
|
112
|
+
|
|
113
|
+
Puedes pasar mensajes como:
|
|
114
|
+
|
|
115
|
+
- `string` fijo.
|
|
116
|
+
- callback usando el valor de la regla.
|
|
117
|
+
- objeto `{ val, message }`.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const age = V.NumberNotNull().min(18, (v) => `Edad minima: ${v.min}`);
|
|
121
|
+
```
|
|
122
|
+
|
|
43
123
|
## API publica
|
|
44
124
|
|
|
45
|
-
Import principal:
|
|
125
|
+
Import principal recomendado:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { Verifiers as V } from "structure-verifier";
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Import completo disponible:
|
|
46
132
|
|
|
47
133
|
```ts
|
|
48
134
|
import {
|
|
@@ -52,345 +138,336 @@ import {
|
|
|
52
138
|
InferType,
|
|
53
139
|
InferFactoryType,
|
|
54
140
|
VAny,
|
|
55
|
-
VAnyConditions,
|
|
56
141
|
VArray,
|
|
57
142
|
VArrayNotNull,
|
|
58
|
-
VArrayConditions,
|
|
59
143
|
VBoolean,
|
|
60
144
|
VBooleanNotNull,
|
|
61
|
-
VBooleanConditions,
|
|
62
145
|
VDate,
|
|
63
146
|
VDateNotNull,
|
|
64
|
-
VDateConditions,
|
|
65
147
|
VNumber,
|
|
66
148
|
VNumberNotNull,
|
|
67
|
-
VNumberConditions,
|
|
68
149
|
VObject,
|
|
69
150
|
VObjectNotNull,
|
|
70
|
-
VObjectConditions,
|
|
71
|
-
VObjectConditionsNotNull,
|
|
72
151
|
VString,
|
|
73
152
|
VStringNotNull,
|
|
74
|
-
VStringConditions,
|
|
75
153
|
VUUID,
|
|
76
154
|
VUUIDNotNull,
|
|
77
|
-
VUUIDConditions,
|
|
78
155
|
datetime,
|
|
79
156
|
} from "structure-verifier";
|
|
80
157
|
```
|
|
81
158
|
|
|
82
|
-
|
|
159
|
+
Tambien se exportan estos tipos de condiciones:
|
|
83
160
|
|
|
84
|
-
|
|
161
|
+
- `VAnyConditions`
|
|
162
|
+
- `VArrayConditions`
|
|
163
|
+
- `VBooleanConditions`
|
|
164
|
+
- `VDateConditions`
|
|
165
|
+
- `VNumberConditions`
|
|
166
|
+
- `VObjectConditions`
|
|
167
|
+
- `VObjectConditionsNotNull`
|
|
168
|
+
- `VStringConditions`
|
|
169
|
+
- `VUUIDConditions`
|
|
85
170
|
|
|
86
|
-
|
|
87
|
-
2. Clases directas (new VString(...), new VObject(...), etc.)
|
|
171
|
+
## Estilos de uso
|
|
88
172
|
|
|
89
|
-
|
|
173
|
+
### API de fabrica (recomendada)
|
|
90
174
|
|
|
91
175
|
```ts
|
|
92
176
|
import { Verifiers as V } from "structure-verifier";
|
|
93
177
|
|
|
94
|
-
const
|
|
95
|
-
|
|
178
|
+
const nameV = V.StringNotNull({ minLength: 3 });
|
|
179
|
+
```
|
|
96
180
|
|
|
97
|
-
|
|
98
|
-
|
|
181
|
+
### Clases directas
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { VStringNotNull } from "structure-verifier";
|
|
185
|
+
|
|
186
|
+
const nameV = new VStringNotNull({ minLength: 3 });
|
|
99
187
|
```
|
|
100
188
|
|
|
101
|
-
|
|
189
|
+
### Callable constructors en `Verifiers`
|
|
102
190
|
|
|
103
|
-
|
|
191
|
+
Los miembros de `Verifiers` aceptan llamada normal o `new`.
|
|
104
192
|
|
|
105
193
|
```ts
|
|
106
|
-
|
|
194
|
+
const a = V.NumberNotNull({ min: 1 });
|
|
195
|
+
const b = new V.NumberNotNull({ min: 1 });
|
|
196
|
+
```
|
|
107
197
|
|
|
108
|
-
|
|
109
|
-
id: V.UUIDNotNull(),
|
|
110
|
-
tags: V.ArrayNotNull(V.StringNotNull()),
|
|
111
|
-
});
|
|
198
|
+
## Validadores disponibles
|
|
112
199
|
|
|
113
|
-
|
|
114
|
-
```
|
|
200
|
+
### Number / NumberNotNull
|
|
115
201
|
|
|
116
|
-
|
|
202
|
+
Salida:
|
|
117
203
|
|
|
118
|
-
|
|
204
|
+
- `VNumber`: `number | null`
|
|
205
|
+
- `VNumberNotNull`: `number`
|
|
119
206
|
|
|
120
|
-
|
|
121
|
-
import { InferFactoryType, Verifiers as V } from "structure-verifier";
|
|
207
|
+
Reglas:
|
|
122
208
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
209
|
+
- `min`
|
|
210
|
+
- `max`
|
|
211
|
+
- `in`
|
|
212
|
+
- `notIn`
|
|
213
|
+
- `maxDecimalPlaces`
|
|
214
|
+
- `minDecimalPlaces`
|
|
215
|
+
- reglas transversales (`isRequired`, `emptyAsNull`, `defaultValue`, `badTypeMessage`)
|
|
126
216
|
|
|
127
|
-
|
|
217
|
+
Comportamiento importante:
|
|
128
218
|
|
|
129
|
-
|
|
219
|
+
- Convierte con `Number(data)`.
|
|
220
|
+
- Rechaza `""` y valores `NaN`.
|
|
130
221
|
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
V.NumberNotNull({ min: 10 }).check(5);
|
|
134
|
-
} catch (error) {
|
|
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
|
-
}
|
|
139
|
-
}
|
|
140
|
-
```
|
|
222
|
+
### String / StringNotNull
|
|
141
223
|
|
|
142
|
-
|
|
224
|
+
Salida:
|
|
143
225
|
|
|
144
|
-
|
|
226
|
+
- `VString`: `string | null`
|
|
227
|
+
- `VStringNotNull`: `string`
|
|
145
228
|
|
|
146
|
-
|
|
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.
|
|
229
|
+
Reglas:
|
|
150
230
|
|
|
151
|
-
|
|
231
|
+
- `minLength`
|
|
232
|
+
- `maxLength`
|
|
233
|
+
- `regex`
|
|
234
|
+
- `notRegex`
|
|
235
|
+
- `in`
|
|
236
|
+
- `notIn`
|
|
237
|
+
- `strictMode`
|
|
238
|
+
- `ignoreCase`
|
|
239
|
+
- reglas transversales
|
|
152
240
|
|
|
153
|
-
|
|
154
|
-
{ val: valorReal, message: (values) => "mensaje" }
|
|
155
|
-
```
|
|
241
|
+
Comportamiento importante:
|
|
156
242
|
|
|
157
|
-
|
|
243
|
+
- En modo normal convierte con `String(data)`.
|
|
244
|
+
- En `strictMode` exige tipo string real.
|
|
245
|
+
- `ignoreCase` aplica a reglas `in` y `notIn`.
|
|
158
246
|
|
|
159
|
-
###
|
|
247
|
+
### Boolean / BooleanNotNull
|
|
160
248
|
|
|
161
|
-
|
|
162
|
-
const n1 = V.Number();
|
|
163
|
-
const n2 = V.NumberNotNull();
|
|
164
|
-
```
|
|
249
|
+
Salida:
|
|
165
250
|
|
|
166
|
-
|
|
251
|
+
- `VBoolean`: `boolean | null`
|
|
252
|
+
- `VBooleanNotNull`: `boolean`
|
|
167
253
|
|
|
168
|
-
|
|
169
|
-
- max
|
|
170
|
-
- in
|
|
171
|
-
- notIn
|
|
172
|
-
- maxDecimalPlaces
|
|
173
|
-
- minDecimalPlaces
|
|
174
|
-
- isRequired
|
|
175
|
-
- emptyAsNull
|
|
176
|
-
- defaultValue
|
|
177
|
-
- badTypeMessage
|
|
254
|
+
Reglas:
|
|
178
255
|
|
|
179
|
-
|
|
256
|
+
- `strictMode`
|
|
257
|
+
- reglas transversales
|
|
180
258
|
|
|
181
|
-
|
|
182
|
-
- "" y NaN fallan.
|
|
259
|
+
Comportamiento importante:
|
|
183
260
|
|
|
184
|
-
|
|
261
|
+
- Modo normal acepta: `true`, `false`, `1`, `0`, `"1"`, `"0"`, `"true"`, `"false"`.
|
|
262
|
+
- `strictMode` acepta solo boolean real.
|
|
185
263
|
|
|
186
|
-
|
|
187
|
-
const s1 = V.String();
|
|
188
|
-
const s2 = V.StringNotNull();
|
|
189
|
-
```
|
|
264
|
+
### UUID / UUIDNotNull
|
|
190
265
|
|
|
191
|
-
|
|
266
|
+
Salida:
|
|
192
267
|
|
|
193
|
-
-
|
|
194
|
-
-
|
|
195
|
-
- regex
|
|
196
|
-
- notRegex
|
|
197
|
-
- in
|
|
198
|
-
- notIn
|
|
199
|
-
- strictMode
|
|
200
|
-
- ignoreCase
|
|
201
|
-
- isRequired
|
|
202
|
-
- emptyAsNull
|
|
203
|
-
- defaultValue
|
|
204
|
-
- badTypeMessage
|
|
268
|
+
- `VUUID`: `string | null`
|
|
269
|
+
- `VUUIDNotNull`: `string`
|
|
205
270
|
|
|
206
|
-
|
|
271
|
+
Reglas:
|
|
207
272
|
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
-
|
|
273
|
+
- `version` (`1 | 2 | 3 | 4 | 5`)
|
|
274
|
+
- `allowNoHyphens`
|
|
275
|
+
- `strictMode`
|
|
276
|
+
- reglas transversales
|
|
211
277
|
|
|
212
|
-
|
|
278
|
+
Comportamiento importante:
|
|
213
279
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const b2 = V.BooleanNotNull();
|
|
217
|
-
```
|
|
280
|
+
- Normaliza salida a minusculas con formato `8-4-4-4-12`.
|
|
281
|
+
- Si `allowNoHyphens` es `false`, exige UUID con guiones.
|
|
218
282
|
|
|
219
|
-
|
|
283
|
+
### Date / DateNotNull
|
|
220
284
|
|
|
221
|
-
|
|
222
|
-
- isRequired
|
|
223
|
-
- emptyAsNull
|
|
224
|
-
- defaultValue
|
|
225
|
-
- badTypeMessage
|
|
285
|
+
Salida:
|
|
226
286
|
|
|
227
|
-
|
|
287
|
+
- `VDate`: `datetime.Dayjs | null`
|
|
288
|
+
- `VDateNotNull`: `datetime.Dayjs`
|
|
228
289
|
|
|
229
|
-
|
|
230
|
-
- En strictMode solo acepta boolean real.
|
|
290
|
+
Reglas:
|
|
231
291
|
|
|
232
|
-
|
|
292
|
+
- `format`
|
|
293
|
+
- `timeZone`
|
|
294
|
+
- `maxDate`
|
|
295
|
+
- `minDate`
|
|
296
|
+
- reglas transversales
|
|
233
297
|
|
|
234
|
-
|
|
235
|
-
const u1 = V.UUID();
|
|
236
|
-
const u2 = V.UUIDNotNull();
|
|
237
|
-
```
|
|
298
|
+
Comportamiento importante:
|
|
238
299
|
|
|
239
|
-
|
|
300
|
+
- Soporta entrada `number`, `string`, `Date` y `datetime.Dayjs`.
|
|
301
|
+
- Si no defines `timeZone`, usa `UTC`.
|
|
302
|
+
- Si el `format` no incluye zona horaria, aplica la zona configurada.
|
|
240
303
|
|
|
241
|
-
|
|
242
|
-
- allowNoHyphens
|
|
243
|
-
- strictMode
|
|
244
|
-
- isRequired
|
|
245
|
-
- emptyAsNull
|
|
246
|
-
- defaultValue
|
|
247
|
-
- badTypeMessage
|
|
304
|
+
### Array / ArrayNotNull
|
|
248
305
|
|
|
249
|
-
|
|
306
|
+
Salida:
|
|
250
307
|
|
|
251
|
-
-
|
|
252
|
-
-
|
|
308
|
+
- `VArray`: `ReturnType<T["check"]>[] | null`
|
|
309
|
+
- `VArrayNotNull`: `ReturnType<T["check"]>[]`
|
|
253
310
|
|
|
254
|
-
|
|
311
|
+
Reglas:
|
|
255
312
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
313
|
+
- `minLength`
|
|
314
|
+
- `maxLength`
|
|
315
|
+
- reglas transversales
|
|
316
|
+
|
|
317
|
+
Comportamiento importante:
|
|
318
|
+
|
|
319
|
+
- Valida item por item con el verificador interno.
|
|
320
|
+
- En errores anidados agrega rutas como `[0]`, `[1].name`, etc.
|
|
321
|
+
|
|
322
|
+
### Object / ObjectNotNull
|
|
323
|
+
|
|
324
|
+
Salida:
|
|
325
|
+
|
|
326
|
+
- `VObject`: objeto tipado o `null`
|
|
327
|
+
- `VObjectNotNull`: objeto tipado
|
|
267
328
|
|
|
268
|
-
|
|
329
|
+
Reglas:
|
|
269
330
|
|
|
270
|
-
-
|
|
271
|
-
-
|
|
272
|
-
-
|
|
273
|
-
-
|
|
274
|
-
-
|
|
275
|
-
-
|
|
276
|
-
- emptyAsNull
|
|
277
|
-
- defaultValue
|
|
278
|
-
- badTypeMessage
|
|
331
|
+
- `invalidPropertyMessage`
|
|
332
|
+
- `strictMode`
|
|
333
|
+
- `ignoreCase`
|
|
334
|
+
- `takeAllValues`
|
|
335
|
+
- `conds`
|
|
336
|
+
- reglas transversales
|
|
279
337
|
|
|
280
|
-
|
|
338
|
+
Comportamiento importante:
|
|
281
339
|
|
|
282
|
-
-
|
|
283
|
-
-
|
|
284
|
-
-
|
|
340
|
+
- `strictMode`: rechaza propiedades no declaradas.
|
|
341
|
+
- `ignoreCase`: permite mapear llaves sin distinguir mayusculas/minusculas.
|
|
342
|
+
- `takeAllValues`: conserva propiedades extra en la salida.
|
|
343
|
+
- `conds`: ejecuta validacion de negocio final sobre el objeto ya validado.
|
|
285
344
|
|
|
286
345
|
### Any
|
|
287
346
|
|
|
288
|
-
|
|
289
|
-
const anyVal = V.Any();
|
|
290
|
-
```
|
|
347
|
+
Salida:
|
|
291
348
|
|
|
292
|
-
|
|
349
|
+
- `VAny`: `any | null`
|
|
293
350
|
|
|
294
|
-
|
|
295
|
-
- emptyAsNull
|
|
296
|
-
- defaultValue
|
|
297
|
-
- badTypeMessage
|
|
351
|
+
Reglas:
|
|
298
352
|
|
|
299
|
-
|
|
353
|
+
- reglas transversales
|
|
354
|
+
|
|
355
|
+
## Transformaciones
|
|
300
356
|
|
|
301
|
-
|
|
357
|
+
La clase base `Verifier` incluye `transform(mapper)` para postprocesar salida validada.
|
|
358
|
+
|
|
359
|
+
`VString` y `VStringNotNull` incluyen helpers:
|
|
360
|
+
|
|
361
|
+
- `trim`
|
|
362
|
+
- `trimStart`
|
|
363
|
+
- `trimEnd`
|
|
364
|
+
- `toLowerCase`
|
|
365
|
+
- `toUpperCase`
|
|
366
|
+
- `removeAccents`
|
|
367
|
+
- `padStart`
|
|
368
|
+
- `padEnd`
|
|
302
369
|
|
|
303
370
|
```ts
|
|
304
|
-
const
|
|
305
|
-
|
|
371
|
+
const usernameV = V.StringNotNull({ minLength: 3 })
|
|
372
|
+
.trim()
|
|
373
|
+
.toLowerCase()
|
|
374
|
+
.removeAccents();
|
|
306
375
|
```
|
|
307
376
|
|
|
308
|
-
|
|
377
|
+
## Tipado inferido
|
|
378
|
+
|
|
379
|
+
### InferType
|
|
309
380
|
|
|
310
381
|
```ts
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
382
|
+
import { InferType, Verifiers as V } from "structure-verifier";
|
|
383
|
+
|
|
384
|
+
const schema = V.ObjectNotNull({
|
|
385
|
+
id: V.UUIDNotNull(),
|
|
386
|
+
profile: V.ObjectNotNull({
|
|
387
|
+
name: V.StringNotNull(),
|
|
388
|
+
age: V.Number(),
|
|
389
|
+
}),
|
|
314
390
|
});
|
|
391
|
+
|
|
392
|
+
type User = InferType<typeof schema>;
|
|
315
393
|
```
|
|
316
394
|
|
|
317
|
-
|
|
395
|
+
### InferFactoryType
|
|
318
396
|
|
|
319
|
-
|
|
320
|
-
-
|
|
321
|
-
- isRequired
|
|
322
|
-
- emptyAsNull
|
|
323
|
-
- defaultValue
|
|
324
|
-
- badTypeMessage
|
|
397
|
+
```ts
|
|
398
|
+
import { InferFactoryType, Verifiers as V } from "structure-verifier";
|
|
325
399
|
|
|
326
|
-
|
|
400
|
+
type NumberMaybe = InferFactoryType<typeof V.Number>;
|
|
401
|
+
type NumberRequired = InferFactoryType<typeof V.NumberNotNull>;
|
|
402
|
+
```
|
|
327
403
|
|
|
328
|
-
|
|
329
|
-
- En errores anidados utiliza claves como [0], [1].name, etc.
|
|
404
|
+
## Manejo de errores
|
|
330
405
|
|
|
331
|
-
|
|
406
|
+
Cuando una validacion falla se lanza `VerificationError`.
|
|
332
407
|
|
|
333
|
-
|
|
408
|
+
Campos principales:
|
|
334
409
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
```
|
|
410
|
+
- `message`: string unico con errores concatenados por `;`.
|
|
411
|
+
- `errors`: arreglo de mensajes planos.
|
|
412
|
+
- `errorsObj`: arreglo con `key`, `message` y metadatos.
|
|
339
413
|
|
|
340
414
|
Ejemplo:
|
|
341
415
|
|
|
342
416
|
```ts
|
|
343
|
-
|
|
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
|
-
);
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
Condiciones:
|
|
417
|
+
import { Verifiers as V, VerificationError } from "structure-verifier";
|
|
357
418
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
419
|
+
try {
|
|
420
|
+
V.ObjectNotNull(
|
|
421
|
+
{
|
|
422
|
+
name: V.StringNotNull({ minLength: 3 }),
|
|
423
|
+
tags: V.ArrayNotNull(V.StringNotNull(), { minLength: 1 }),
|
|
424
|
+
},
|
|
425
|
+
{ strictMode: true },
|
|
426
|
+
).check({ name: "Al", tags: [], extra: true });
|
|
427
|
+
} catch (error) {
|
|
428
|
+
if (error instanceof VerificationError) {
|
|
429
|
+
console.log(error.message);
|
|
430
|
+
console.log(error.errors);
|
|
431
|
+
console.log(error.errorsObj);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
```
|
|
367
435
|
|
|
368
|
-
|
|
436
|
+
Posible salida en `errorsObj`:
|
|
369
437
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
438
|
+
```ts
|
|
439
|
+
[
|
|
440
|
+
{ key: "name", message: "debe tener una longitud minima de 3" },
|
|
441
|
+
{ key: "tags", message: "debe tener al menos 1 elementos" },
|
|
442
|
+
{ key: "extra", message: "no es una propiedad valida" },
|
|
443
|
+
];
|
|
444
|
+
```
|
|
374
445
|
|
|
375
446
|
## datetime (dayjs)
|
|
376
447
|
|
|
377
|
-
La libreria
|
|
448
|
+
La libreria exporta `dayjs` como `datetime` con plugins habilitados:
|
|
378
449
|
|
|
379
|
-
- utc
|
|
380
|
-
- timezone
|
|
381
|
-
- customParseFormat
|
|
382
|
-
|
|
383
|
-
Ejemplo:
|
|
450
|
+
- `utc`
|
|
451
|
+
- `timezone`
|
|
452
|
+
- `customParseFormat`
|
|
384
453
|
|
|
385
454
|
```ts
|
|
386
455
|
import { datetime } from "structure-verifier";
|
|
387
456
|
|
|
388
457
|
const now = datetime();
|
|
389
|
-
|
|
458
|
+
const utc = now.tz("UTC").format();
|
|
459
|
+
console.log(utc);
|
|
390
460
|
```
|
|
391
461
|
|
|
392
|
-
##
|
|
462
|
+
## Buenas practicas para v1.0.0
|
|
393
463
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
464
|
+
- Define siempre `strictMode: true` en payloads de entrada externa.
|
|
465
|
+
- Usa variantes `NotNull` en campos de negocio obligatorios.
|
|
466
|
+
- Agrega reglas semanticas con `conds` para validaciones cruzadas.
|
|
467
|
+
- Encadena normalizaciones (`trim`, `toLowerCase`, etc.) para salida consistente.
|
|
468
|
+
- Centraliza mensajes custom cuando quieras UX de errores uniforme.
|
|
469
|
+
- Cubre cada esquema critico con pruebas de casos validos e invalidos.
|
|
470
|
+
|
|
471
|
+
## Licencia
|
|
472
|
+
|
|
473
|
+
ISC
|