Skip to content

Commit 886e215

Browse files
author
Aashutosh
committed
changes in apply()
1 parent 2ea6fe0 commit 886e215

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

validating.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (r *Rule) Apply(v *Validation) (stop bool) {
100100
return
101101
}
102102

103-
// has beforeFunc and it returns FALSE, skip validate
103+
// has beforeFunc and it return FALSE, skip validate
104104
if r.beforeFunc != nil && !r.beforeFunc(v) {
105105
return
106106
}
@@ -133,11 +133,12 @@ func (r *Rule) Apply(v *Validation) (stop bool) {
133133
// get field value. val, exist := v.Get(field)
134134
val, exist, isDefault := v.GetWithDefault(field)
135135

136-
// value not exists but has default value
136+
// not exists but has default value
137137
if isDefault {
138138
// update source data field value and re-set value
139139
val, err := v.updateValue(field, val)
140140
if err != nil {
141+
// panicf(err.Error())
141142
v.AddErrorf(field, err.Error())
142143
if v.StopOnError {
143144
return true
@@ -147,7 +148,8 @@ func (r *Rule) Apply(v *Validation) (stop bool) {
147148

148149
// dont need check default value
149150
if !v.CheckDefault {
150-
v.safeData[field] = val // save validated value.
151+
// save validated value.
152+
v.safeData[field] = val
151153
continue
152154
}
153155

@@ -159,14 +161,15 @@ func (r *Rule) Apply(v *Validation) (stop bool) {
159161

160162
// apply filter func.
161163
if exist && r.filterFunc != nil {
162-
if val, err = r.filterFunc(val); err != nil {
163-
v.AddError(filterError, filterError, field+": "+err.Error())
164+
if val, err = r.filterFunc(val); err != nil { // has error
165+
v.AddError(filterError, filterError, err.Error())
164166
return true
165167
}
166168

167169
// update source field value
168170
newVal, err := v.updateValue(field, val)
169171
if err != nil {
172+
// panicf(err.Error())
170173
v.AddErrorf(field, err.Error())
171174
if v.StopOnError {
172175
return true
@@ -179,20 +182,26 @@ func (r *Rule) Apply(v *Validation) (stop bool) {
179182
// save filtered value.
180183
v.filteredData[field] = val
181184
}
182-
183-
// empty value AND is not required* AND skip on empty.
185+
// Todo: Update validation and filtering flow
186+
if val != nil{
187+
// Customization: We need to bind all data
188+
v.safeData[field] = val
189+
}
190+
// empty value AND skip on empty.
184191
if r.skipEmpty && isNotRequired && IsEmpty(val) {
185192
continue
186193
}
187194

188195
// validate field value
189196
if r.valueValidate(field, name, val, v) {
190-
v.safeData[field] = val
197+
if val != nil {
198+
v.safeData[field] = val // save validated value.
199+
}
191200
} else { // build and collect error message
192201
v.AddError(field, r.validator, r.errorMessage(field, r.validator, v))
193202
}
194203

195-
// Customization: To validate all the fields we need to continue iterating rather stopping on single error.
204+
// Customization: To validate all the fields we need to continue iterating rather stopping on single error.
196205

197206
// stop on error
198207
/*if v.shouldStop() {

0 commit comments

Comments
 (0)