1
- const { PEOPLE_STATE_ACTIVE , PEOPLE_STATE_INACTIVE , PEOPLE_STATE_DELETED } = require ( "../constants" ) ;
2
-
3
1
const validator = ( data ) => {
4
2
let errors = { } ;
5
- return
6
- const MIN_FULLNAME_LENGTH = 2 ;
7
- const MAX_FULLNAME_LENGTH = 50 ;
8
- const MAX_ADDRESS_LENGTH = 100 ;
9
- const fullNameRegex = / ^ [ \p{ L} . ' - ] + $ / u;
10
- const geopositionRegex = / ^ - ? ( [ 1 - 8 ] ? [ 1 - 9 ] | [ 1 - 9 ] 0 ) \. { 1 } \d { 1 , 6 } , \s * - ? ( [ 1 ] ? [ 1 - 7 ] ? [ 1 - 9 ] | [ 1 - 9 ] 0 ) \. { 1 } \d { 1 , 6 } $ / ;
11
- const MAX_ABOUT_ME_LENGTH = 1000 ;
12
- const emailRegex = / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / ;
13
3
14
4
if (
5
+ ! data . fullName ||
15
6
typeof data . fullName !== 'string' ||
16
- data . fullName . trim ( ) . length < MIN_FULLNAME_LENGTH ||
17
- data . fullName . trim ( ) . length > MAX_FULLNAME_LENGTH ) {
18
- errors . fullName = `El nombre completo debe ser una cadena de texto entre ${ MIN_FULLNAME_LENGTH } y ${ MAX_FULLNAME_LENGTH } caracteres.` ;
19
- }
20
-
21
- if ( ! data . fullName . trim ( ) ) {
22
- errors . fullName = 'El nombre completo no puede estar vacío.' ;
23
- }
24
-
25
- if ( ! fullNameRegex . test ( data . fullName ) ) {
26
- errors . fullName = 'El nombre completo solo puede contener letras del alfabeto, espacios, guiones, comillas y puntos.' ;
27
- }
28
-
29
- if ( data . address && ( typeof data . address !== 'string' || data . address . trim ( ) . length > MAX_ADDRESS_LENGTH ) ) {
30
- errors . address = `La dirección debe ser una cadena de texto con un máximo de ${ MAX_ADDRESS_LENGTH } caracteres.`
31
- }
32
-
33
- if ( data . geoposition && ! geopositionRegex . test ( data . geoposition . trim ( ) ) ) {
34
- errors . geoposition = "El formato de la geoposición no es válido." ;
35
- }
36
-
37
- if ( ! / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( data . birthDate ) ) {
38
- errors . birthDate = "El formato de la fecha de nacimiento no es válido. Debe ser YYYY-MM-DD."
39
- }
40
-
41
- if ( data . idGenre !== undefined ) {
42
- if ( typeof data . idGenre !== 'number' || ! Number . isInteger ( data . idGenre ) ) {
43
- errors . idGenre = 'El valor de idGenre debe ser un número entero.' ;
44
- }
45
- }
46
-
47
- if ( data . state !== undefined ) {
48
- if ( ! [ PEOPLE_STATE_ACTIVE , PEOPLE_STATE_INACTIVE , PEOPLE_STATE_DELETED ] . includes ( data . state ) ) {
49
- errors . state = `El valor de state debe ser ${ PEOPLE_STATE_ACTIVE } ,${ PEOPLE_STATE_INACTIVE } ,${ PEOPLE_STATE_DELETED } ` ;
50
- }
7
+ data . fullName . trim ( ) . length === 0 ||
8
+ data . fullName . trim ( ) . length > 30 ||
9
+ ! / ^ [ a - z A - Z \s ] * $ / . test ( data . fullName ) ) {
10
+ errors . fullName = "El nombre completo es obligatorio y debe contener solo letras y espacios, con un máximo de 30 caracteres." ;
51
11
}
52
12
53
- if ( data . aboutMe !== undefined ) {
54
- if ( data . aboutMe . length > MAX_ABOUT_ME_LENGTH ) {
55
- errors . aboutMe = `El campo aboutMe no puede tener más de ${ MAX_ABOUT_ME_LENGTH } caracteres.` ;
56
- }
57
- }
58
-
59
- if ( data . typeOfPerson !== undefined ) {
60
- const allowedTypes = [ 'administrator' , 'customer' , 'provider' ] ;
61
- if ( ! allowedTypes . includes ( data . typeOfPerson ) ) {
62
- errors . typeOfPerson = 'El valor de typeOfPerson no es válido.' ;
63
- }
64
- }
65
-
66
- if ( data . email !== undefined ) {
67
- if ( ! emailRegex . test ( data . email ) ) {
68
- errors . email = 'El formato del email no es válido.' ;
69
- }
70
- }
71
-
72
- if ( data . password !== undefined ) {
73
- if ( data . password . length < 8 || data . password . length > 16 ) {
74
- errors . password = 'La contraseña debe tener entre 8 y 16 caracteres.' ;
75
- }
76
-
77
- if ( ! / [ A - Z ] / . test ( data . password ) ) {
78
- errors . password = 'La contraseña debe contener al menos una letra mayúscula.' ;
79
- }
80
-
81
- if ( ! / \d / . test ( data . password ) ) {
82
- errors . password = 'La contraseña debe contener al menos un número.' ;
83
- }
13
+ if (
14
+ data . phone &&
15
+ ( ! data . phone ||
16
+ typeof data . phone !== 'string' ||
17
+ data . phone . trim ( ) . length === 0 ||
18
+ ! / ^ \d { 10 } $ / . test ( data . phone ) ) ) {
19
+ errors . phone = "El número de teléfono debe tener un formato válido para Argentina (10 dígitos numéricos)." ;
84
20
}
85
21
86
- if ( data . weekCalendar !== undefined ) {
87
- if ( ! Array . isArray ( data . weekCalendar ) ) {
88
- errors . weekCalendar = 'weekCalendar debe ser un array.' ;
89
- } else {
90
- if ( data . weekCalendar . length !== 7 ) {
91
- errors . weekCalendar = 'weekCalendar debe contener 7 elementos correspondientes a los días de la semana.' ;
92
- } else {
93
- for ( let i = 0 ; i < data . weekCalendar . length ; i ++ ) {
94
- if ( typeof data . weekCalendar [ i ] !== 'boolean' ) {
95
- errors . weekCalendar = 'Los elementos de weekCalendar deben ser booleanos.' ;
96
- break ;
97
- }
98
- }
99
- }
100
- }
22
+ if (
23
+ data . address &&
24
+ ( ! data . address ||
25
+ typeof data . address !== 'string' ||
26
+ data . address . trim ( ) . length === 0 ||
27
+ data . address . trim ( ) . length > 20 || ! / ^ [ A - Z a - z 0 - 9 Á É Í Ó Ú á é í ó ú Ü ü Ñ ñ \s ] + $ / . test ( data . address ) ) ) {
28
+ errors . address = "La dirección debe contener solo letras, números y espacios, con un máximo de 20 caracteres." ;
101
29
}
102
30
103
- if ( data . image !== undefined ) {
104
- if ( typeof data . image !== 'string' ) {
105
- errors . image = 'image debe ser una cadena de caracteres.' ;
106
- }
31
+ if (
32
+ data . profession &&
33
+ ( ! data . profession ||
34
+ typeof data . profession !== 'string' ||
35
+ data . profession . trim ( ) . length === 0 ||
36
+ data . profession . trim ( ) . length > 25 || ! / ^ [ A - Z a - z \s ] + $ / . test ( data . profession ) ) ) {
37
+ errors . profession = "La ocupación debe contener solo letras y espacios, con un máximo de 25 caracteres." ;
107
38
}
108
39
109
- if ( data . age !== undefined ) {
110
- if ( typeof data . age !== 'number' || data . age < 0 || data . age > 150 ) {
111
- errors . image = `La edad calculada no es válida:', ${ data . age } ` ;
112
- }
40
+ if (
41
+ data . aboutMe &&
42
+ ( typeof data . aboutMe !== 'string' ||
43
+ data . aboutMe . trim ( ) . length === 0 ||
44
+ data . aboutMe . trim ( ) . length > 200 ) ) {
45
+ errors . aboutMe = "El campo 'Sobre mí' debe tener como máximo 200 caracteres." ;
113
46
}
114
47
115
48
return errors ;
116
-
117
49
}
118
50
119
51
module . exports = { validator } ;
0 commit comments