1
- import { type App } from 'vue'
1
+ import { getCurrentInstance , type App , type VNode } from 'vue'
2
2
import { useErrors } from './store'
3
3
import { validEmail , validLength , validRegex , validRequired } from './utils'
4
4
import ValidateTypes from './validate-type'
@@ -19,7 +19,7 @@ declare global {
19
19
20
20
export default {
21
21
install : ( app : App ) => {
22
- const { addError, delError, cleanupErrors } = useErrors ( )
22
+ const { errors , currentErrorsScope , addError, delError, cleanupErrors } = useErrors ( )
23
23
24
24
app . config . globalProperties . $formMaxLength = {
25
25
email : 64 ,
@@ -35,87 +35,86 @@ export default {
35
35
password : 128
36
36
}
37
37
38
+ app . config . globalProperties . $validator = {
39
+ validateAll : ( ) => {
40
+ return new Promise ( ( resolve , reject ) => {
41
+ const result = ! ! currentErrorsScope . value
42
+ resolve ( {
43
+ isValid : ! result
44
+ } )
45
+ } )
46
+ } ,
47
+ }
48
+
38
49
app . directive ( 'validate' , {
39
50
deep : true ,
40
- mounted : ( el : HTMLInputElement , binding ) => {
41
- const isStringSchema = binding . value instanceof String
51
+ mounted : ( el : HTMLInputElement , binding , vnode : VNode ) => {
52
+ const isStringSchema = typeof binding . value === 'string'
53
+ const stringRules = isStringSchema ? binding . value . split ( SplitChar ) : [ ]
42
54
const schema = isStringSchema
43
- ? binding . value . split ( SplitChar ) . map ( ( rule : string ) => {
55
+ ? stringRules . map ( ( rule : string ) =>
44
56
rule . includes ( ':' ) ? rule . split ( ':' ) : [ rule , true ]
45
- } )
57
+ )
46
58
: Object . entries ( binding . value )
47
59
48
- binding . value . split ( SplitChar ) . map ( ( rule : string ) => {
49
- rule . includes ( ':' ) ? console . log ( rule . split ( ':' ) ) : console . log ( [ rule , true ] )
50
- } )
51
-
52
- console . log ( schema )
53
-
54
- const actions = el . dataset . vvValidateOn ?. split ( SplitChar )
55
- const bubble = binding . modifiers ?. bubble
56
-
57
60
// prevent immediate submission
58
61
schema . map ( ( [ rule , value ] : [ string , unknown ] ) => {
59
62
if ( rule !== ValidateTypes . required ) return
60
- value && addError ( el , ValidateTypes . firstMountedRequired )
63
+ value && addError ( vnode , ValidateTypes . onMountedRequired )
61
64
} )
62
65
63
- const handler = ( el : HTMLInputElement ) => {
64
- delError ( el , ValidateTypes . firstMountedRequired )
66
+ const handler = ( ) => {
67
+ delError ( vnode , ValidateTypes . onMountedRequired )
65
68
const rules = schema . map ( ( rule : [ string , unknown ] ) => rule [ 0 ] )
66
69
67
- bubble && binding . value ( el )
68
-
69
70
schema . map ( ( [ , value ] : [ string , unknown ] ) => {
70
71
const satisfyRequired = validRequired ( rules , el , ! ! value )
71
- satisfyRequired && delError ( el , ValidateTypes . required )
72
- ! satisfyRequired && addError ( el , ValidateTypes . required )
72
+ satisfyRequired && delError ( vnode , ValidateTypes . required )
73
+ ! satisfyRequired && addError ( vnode , ValidateTypes . required )
73
74
74
75
const satisfyEmail = validEmail ( rules , el , value as boolean )
75
- satisfyEmail && delError ( el , ValidateTypes . email )
76
- ! satisfyEmail && addError ( el , ValidateTypes . email )
76
+ satisfyEmail && delError ( vnode , ValidateTypes . email )
77
+ ! satisfyEmail && addError ( vnode , ValidateTypes . email )
77
78
78
79
const satisfyRegex = validRegex ( rules , el , value as RegExp )
79
- satisfyRegex && delError ( el , ValidateTypes . regex )
80
- ! satisfyRegex && addError ( el , ValidateTypes . regex )
80
+ satisfyRegex && delError ( vnode , ValidateTypes . regex )
81
+ ! satisfyRegex && addError ( vnode , ValidateTypes . regex )
81
82
82
83
const satisfyLength = validLength ( rules , el , value )
83
- console . log ( value )
84
- satisfyLength && delError ( el , ValidateTypes . length )
85
- ! satisfyLength && addError ( el , ValidateTypes . length )
84
+ satisfyLength && delError ( vnode , ValidateTypes . length )
85
+ ! satisfyLength && addError ( vnode , ValidateTypes . length )
86
86
87
87
if ( ! satisfyRequired || ! satisfyEmail || ! satisfyRegex || ! satisfyLength ) {
88
- addError ( el )
88
+ addError ( vnode )
89
89
} else {
90
- delError ( el )
90
+ delError ( vnode )
91
91
}
92
92
} )
93
93
}
94
94
95
95
el [ UniquePropertyID ] = handler
96
96
97
+ const actions = el . dataset . vvValidateOn ?. split ( SplitChar )
97
98
actions ?. forEach ( ( action ) => {
98
- el . addEventListener ( action , ( ) => handler ( el ) )
99
+ el . addEventListener ( action , ( ) => handler ( ) )
99
100
} )
100
101
101
- const form = el . closest ( 'form' ) as HTMLFormElement
102
- if ( ! [ ...form . elements ] . includes ( el ) ) return
103
-
102
+ const form = el . closest ( `form[${ vnode . scopeId } ]` ) as HTMLFormElement
104
103
form ?. addEventListener ( 'submit' , ( e ) => {
105
104
e . preventDefault ( )
106
- handler ( el )
105
+ handler ( )
107
106
} )
108
107
} ,
109
- unmounted : ( el : HTMLInputElement ) => {
108
+ unmounted : ( el : HTMLInputElement , _binding , vnode : VNode ) => {
110
109
const actions = el . dataset . vvValidateOn ?. split ( SplitChar )
111
110
actions ?. forEach ( ( action ) => {
112
111
el . removeEventListener ( action , ( ) => el [ UniquePropertyID ] )
113
112
} )
114
113
115
114
// clear cache handler
116
115
el [ UniquePropertyID ] = ( ) => { }
117
- cleanupErrors ( )
118
- }
116
+ cleanupErrors ( vnode )
117
+ } ,
119
118
} )
120
119
}
121
120
}
0 commit comments