pols-validator 1.0.1 → 2.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/dist/index.d.ts +3 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -73
- package/dist/rules.d.ts +7 -4
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +130 -135
- package/dist/rulesEngine.d.ts +21 -10
- package/dist/rulesEngine.d.ts.map +1 -1
- package/dist/rulesEngine.js +69 -13
- package/dist/validate.d.ts +12 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +56 -0
- package/package.json +4 -3
- package/src/index.ts +3 -86
- package/src/rules.ts +143 -158
- package/src/rulesEngine.ts +87 -16
- package/test/validaciones.ts +7 -5
package/src/rulesEngine.ts
CHANGED
|
@@ -1,36 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
import { PUtils } from "pols-utils"
|
|
2
|
+
|
|
3
|
+
export type PRulesParams = {
|
|
2
4
|
label?: string
|
|
5
|
+
separator?: string
|
|
3
6
|
} & ({
|
|
4
7
|
required?: boolean
|
|
5
8
|
} | {
|
|
6
9
|
default?: unknown
|
|
7
10
|
})
|
|
8
11
|
|
|
9
|
-
export type
|
|
10
|
-
schema: Record<string, RulesEngine>
|
|
11
|
-
prefix?: string
|
|
12
|
-
}
|
|
12
|
+
export type PRulesFunction = (wrapper: PRulesWrapper, ...args: unknown[]) => string | string[] | void | null | undefined
|
|
13
13
|
|
|
14
|
-
export type
|
|
14
|
+
export type PRulesWrapper<T = unknown> = {
|
|
15
15
|
value: T
|
|
16
16
|
label: string
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
export type PRulesResponse<T> = {
|
|
20
|
+
error: false
|
|
21
|
+
success: true
|
|
22
|
+
result: T
|
|
23
|
+
} | {
|
|
24
|
+
error: true
|
|
25
|
+
success: false
|
|
26
|
+
messages: string[]
|
|
27
|
+
}
|
|
26
28
|
|
|
29
|
+
export class PRulesEngine {
|
|
30
|
+
prefix: string
|
|
27
31
|
label: string
|
|
32
|
+
separator: string
|
|
28
33
|
required: boolean = false
|
|
29
34
|
default: unknown = null
|
|
30
|
-
collection: Record<string, ValidationFunction> = {}
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
private collectionFunctions: PRulesFunction[] = []
|
|
37
|
+
private collectionNames: string[] = []
|
|
38
|
+
|
|
39
|
+
constructor(params?: PRulesParams) {
|
|
40
|
+
this.label = params?.label
|
|
41
|
+
this.separator = params?.separator ?? ' > '
|
|
42
|
+
this.required = params && 'required' in params ? params?.required : false
|
|
43
|
+
this.default = params && 'default' in params ? params?.default : null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected add(name: string, validationFunction: PRulesFunction) {
|
|
47
|
+
if (!this.collectionNames.includes(name)) {
|
|
48
|
+
this.collectionNames.push(name)
|
|
49
|
+
this.collectionFunctions.push(validationFunction)
|
|
50
|
+
}
|
|
34
51
|
return this
|
|
35
52
|
}
|
|
53
|
+
|
|
54
|
+
validate = <T>(target: unknown): PRulesResponse<T> => {
|
|
55
|
+
const errorMessages: string[] = []
|
|
56
|
+
|
|
57
|
+
if (typeof target == 'string') target = target.trim()
|
|
58
|
+
|
|
59
|
+
const isEmpty = target == null || (typeof target == 'string' && !target)
|
|
60
|
+
const label = this.label
|
|
61
|
+
|
|
62
|
+
if (this.required && isEmpty) {
|
|
63
|
+
return {
|
|
64
|
+
error: true,
|
|
65
|
+
success: false,
|
|
66
|
+
messages: [`'${label}' es requerido`]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isEmpty) {
|
|
71
|
+
return {
|
|
72
|
+
error: false,
|
|
73
|
+
success: true,
|
|
74
|
+
result: this.default as T
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const wrapper: PRulesWrapper<T> = {
|
|
79
|
+
value: PUtils.clone(target) as T,
|
|
80
|
+
label
|
|
81
|
+
}
|
|
82
|
+
for (const validationFunction of this.collectionFunctions) {
|
|
83
|
+
const result = validationFunction(wrapper)
|
|
84
|
+
if (!result) continue
|
|
85
|
+
if (typeof result == 'string') {
|
|
86
|
+
errorMessages.push(result)
|
|
87
|
+
break
|
|
88
|
+
} else {
|
|
89
|
+
errorMessages.push(...result)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (errorMessages.length) {
|
|
94
|
+
return {
|
|
95
|
+
error: true,
|
|
96
|
+
success: false,
|
|
97
|
+
messages: errorMessages
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
return {
|
|
101
|
+
error: false,
|
|
102
|
+
success: true,
|
|
103
|
+
result: wrapper.value
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
36
107
|
}
|
package/test/validaciones.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { rules
|
|
1
|
+
import { rules } from '../src/index'
|
|
2
2
|
|
|
3
3
|
const original = {
|
|
4
4
|
uno: 'hola',
|
|
5
|
-
dos:
|
|
5
|
+
dos: 'er',
|
|
6
6
|
tres: '67',
|
|
7
7
|
cuatro: {
|
|
8
8
|
aaa: '2024-10-28 23:56:00',
|
|
@@ -10,13 +10,15 @@ const original = {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
|
|
13
|
+
const otro = 56
|
|
14
|
+
|
|
15
|
+
const resultados = rules({ label: 'Body' }).isObject({
|
|
16
|
+
uno: rules({ label: 'Uno', required: true }).isNatural(),
|
|
15
17
|
dos: rules().isBoolean(),
|
|
16
18
|
tres: rules({ required: true }).isNaturalNoZero(),
|
|
17
19
|
cuatro: rules({ label: 'Cuatro', default: {} }).isObject({
|
|
18
20
|
aaa: rules().isDateTime()
|
|
19
21
|
})
|
|
20
|
-
}
|
|
22
|
+
}).validate(original)
|
|
21
23
|
|
|
22
24
|
console.log(original, resultados)
|