Merge pull request #2971 from yousong/bugfix/yousong-validators-missing-field

validators: 应对"listener"参数被重复Validate的情况
This commit is contained in:
yunion-ci-robot
2019-09-18 23:43:27 +08:00
committed by GitHub
+19 -4
View File
@@ -383,10 +383,11 @@ func NewNonNegativeValidator(key string) *ValidatorRange {
type ValidatorModelIdOrName struct {
Validator
ModelKeyword string
OwnerId mcclient.IIdentityProvider
ModelManager db.IModelManager
Model db.IModel
ModelKeyword string
OwnerId mcclient.IIdentityProvider
ModelManager db.IModelManager
Model db.IModel
modelIdKey string
noPendingDeleted bool
}
@@ -459,6 +460,20 @@ func (v *ValidatorModelIdOrName) AllowPendingDeleted(b bool) *ValidatorModelIdOr
}
func (v *ValidatorModelIdOrName) validate(data *jsonutils.JSONDict) error {
if !data.Contains(v.Key) && data.Contains(v.modelIdKey) {
// a hack when validator is used solely for fetching model
// object. This can happen when input json data was validated
// more than once in different places
j, err := data.Get(v.modelIdKey)
if err != nil {
return err
}
data.Set(v.Key, j)
defer func() {
// restore
data.Remove(v.Key)
}()
}
if err, isSet := v.Validator.validateEx(data); err != nil || !isSet {
return err
}