validators_actor.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package validators
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/netutils"
  21. )
  22. type IValidateActor interface {
  23. Validate(val interface{}) (bool, error)
  24. Set(data *jsonutils.JSONDict, key string, val interface{})
  25. getValue() interface{}
  26. }
  27. type actorSetString struct{}
  28. func (actor *actorSetString) Set(data *jsonutils.JSONDict, key string, val interface{}) {
  29. s := val.(string)
  30. data.Set(key, jsonutils.NewString(s))
  31. }
  32. type ActorIPv4Prefix struct {
  33. actorSetString
  34. val *netutils.IPV4Prefix
  35. }
  36. func NewActorIPv4Prefix() *ActorIPv4Prefix {
  37. return &ActorIPv4Prefix{}
  38. }
  39. func (actor *ActorIPv4Prefix) Validate(val interface{}) (bool, error) {
  40. s, ok := val.(string)
  41. if !ok {
  42. return false, fmt.Errorf("invalid type, want string, got %t", val)
  43. }
  44. p, err := netutils.NewIPV4Prefix(s)
  45. if err != nil {
  46. return false, err
  47. }
  48. actor.val = &p
  49. return true, nil
  50. }
  51. func (actor *ActorIPv4Prefix) getValue() interface{} {
  52. return actor.val
  53. }
  54. type ActorJoinedBy struct {
  55. sep string
  56. trimSpace bool
  57. ignoreEmpty bool
  58. actor IValidateActor
  59. sanitized string
  60. vals []interface{}
  61. }
  62. func NewActorJoinedBy(sep string, subActor IValidateActor) *ActorJoinedBy {
  63. return &ActorJoinedBy{
  64. sep: sep,
  65. trimSpace: true,
  66. ignoreEmpty: true,
  67. actor: subActor,
  68. }
  69. }
  70. func (actor *ActorJoinedBy) TrimSpace(b bool) *ActorJoinedBy {
  71. actor.trimSpace = b
  72. return actor
  73. }
  74. func (actor *ActorJoinedBy) IgnoreEmpty(b bool) *ActorJoinedBy {
  75. actor.ignoreEmpty = b
  76. return actor
  77. }
  78. func (actor *ActorJoinedBy) Validate(val interface{}) (bool, error) {
  79. s, ok := val.(string)
  80. if !ok {
  81. return false, fmt.Errorf("invalid type, want string, got %t", val)
  82. }
  83. parts := strings.Split(s, actor.sep)
  84. sanitizedParts := make([]string, 0, len(parts))
  85. vals := make([]interface{}, 0, len(parts))
  86. for i, part := range parts {
  87. if actor.trimSpace {
  88. part = strings.TrimSpace(part)
  89. }
  90. if actor.ignoreEmpty && part == "" {
  91. continue
  92. }
  93. if _, err := actor.actor.Validate(part); err != nil {
  94. return false, fmt.Errorf("bad value at index %d: %v", i, err)
  95. }
  96. sanitizedParts = append(sanitizedParts, part)
  97. vals = append(vals, actor.actor.getValue())
  98. }
  99. actor.sanitized = strings.Join(sanitizedParts, actor.sep)
  100. actor.vals = vals
  101. return true, nil
  102. }
  103. func (actor *ActorJoinedBy) Set(data *jsonutils.JSONDict, key string, val interface{}) {
  104. data.Set(key, jsonutils.NewString(actor.sanitized))
  105. }
  106. func (actor *ActorJoinedBy) getValue() interface{} {
  107. return actor.vals
  108. }
  109. type ValidatorByActor struct {
  110. Validator
  111. Value interface{}
  112. actor IValidateActor
  113. }
  114. func NewValidatorByActor(key string, actor IValidateActor) *ValidatorByActor {
  115. v := &ValidatorByActor{
  116. Validator: Validator{Key: key},
  117. actor: actor,
  118. }
  119. v.SetParent(v)
  120. return v
  121. }
  122. func (v *ValidatorByActor) Default(val interface{}) IValidator {
  123. if ok, err := v.actor.Validate(val); !ok {
  124. panic(fmt.Sprintf("invalid default value: %v", err))
  125. }
  126. return v.Validator.Default(val)
  127. }
  128. func (v *ValidatorByActor) getValue() interface{} {
  129. return v.Value
  130. }
  131. func (v *ValidatorByActor) Validate(ctx context.Context, data *jsonutils.JSONDict) error {
  132. if err, isSet := v.Validator.validateEx(data); err != nil || !isSet {
  133. return err
  134. }
  135. val := v.value.Interface()
  136. if ok, err := v.actor.Validate(val); !ok {
  137. return newInvalidValueErrorEx(v.Key, err)
  138. }
  139. v.actor.Set(data, v.Key, val)
  140. v.Value = v.actor.getValue()
  141. return nil
  142. }