本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
13KB

  1. // Copyright 2015 go-swagger maintainers
  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 validate
  15. import (
  16. "fmt"
  17. "reflect"
  18. "unicode/utf8"
  19. "github.com/go-openapi/errors"
  20. "github.com/go-openapi/strfmt"
  21. "github.com/go-openapi/swag"
  22. )
  23. // Enum validates if the data is a member of the enum
  24. func Enum(path, in string, data interface{}, enum interface{}) *errors.Validation {
  25. val := reflect.ValueOf(enum)
  26. if val.Kind() != reflect.Slice {
  27. return nil
  28. }
  29. var values []interface{}
  30. for i := 0; i < val.Len(); i++ {
  31. ele := val.Index(i)
  32. enumValue := ele.Interface()
  33. if data != nil {
  34. if reflect.DeepEqual(data, enumValue) {
  35. return nil
  36. }
  37. actualType := reflect.TypeOf(enumValue)
  38. if actualType == nil { // Safeguard. Frankly, I don't know how we may get a nil
  39. continue
  40. }
  41. expectedValue := reflect.ValueOf(data)
  42. if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
  43. // Attempt comparison after type conversion
  44. if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), enumValue) {
  45. return nil
  46. }
  47. }
  48. }
  49. values = append(values, enumValue)
  50. }
  51. return errors.EnumFail(path, in, data, values)
  52. }
  53. // MinItems validates that there are at least n items in a slice
  54. func MinItems(path, in string, size, min int64) *errors.Validation {
  55. if size < min {
  56. return errors.TooFewItems(path, in, min)
  57. }
  58. return nil
  59. }
  60. // MaxItems validates that there are at most n items in a slice
  61. func MaxItems(path, in string, size, max int64) *errors.Validation {
  62. if size > max {
  63. return errors.TooManyItems(path, in, max)
  64. }
  65. return nil
  66. }
  67. // UniqueItems validates that the provided slice has unique elements
  68. func UniqueItems(path, in string, data interface{}) *errors.Validation {
  69. val := reflect.ValueOf(data)
  70. if val.Kind() != reflect.Slice {
  71. return nil
  72. }
  73. var unique []interface{}
  74. for i := 0; i < val.Len(); i++ {
  75. v := val.Index(i).Interface()
  76. for _, u := range unique {
  77. if reflect.DeepEqual(v, u) {
  78. return errors.DuplicateItems(path, in)
  79. }
  80. }
  81. unique = append(unique, v)
  82. }
  83. return nil
  84. }
  85. // MinLength validates a string for minimum length
  86. func MinLength(path, in, data string, minLength int64) *errors.Validation {
  87. strLen := int64(utf8.RuneCount([]byte(data)))
  88. if strLen < minLength {
  89. return errors.TooShort(path, in, minLength)
  90. }
  91. return nil
  92. }
  93. // MaxLength validates a string for maximum length
  94. func MaxLength(path, in, data string, maxLength int64) *errors.Validation {
  95. strLen := int64(utf8.RuneCount([]byte(data)))
  96. if strLen > maxLength {
  97. return errors.TooLong(path, in, maxLength)
  98. }
  99. return nil
  100. }
  101. // Required validates an interface for requiredness
  102. func Required(path, in string, data interface{}) *errors.Validation {
  103. val := reflect.ValueOf(data)
  104. if val.IsValid() {
  105. if reflect.DeepEqual(reflect.Zero(val.Type()).Interface(), val.Interface()) {
  106. return errors.Required(path, in)
  107. }
  108. return nil
  109. }
  110. return errors.Required(path, in)
  111. }
  112. // RequiredString validates a string for requiredness
  113. func RequiredString(path, in, data string) *errors.Validation {
  114. if data == "" {
  115. return errors.Required(path, in)
  116. }
  117. return nil
  118. }
  119. // RequiredNumber validates a number for requiredness
  120. func RequiredNumber(path, in string, data float64) *errors.Validation {
  121. if data == 0 {
  122. return errors.Required(path, in)
  123. }
  124. return nil
  125. }
  126. // Pattern validates a string against a regular expression
  127. func Pattern(path, in, data, pattern string) *errors.Validation {
  128. re, err := compileRegexp(pattern)
  129. if err != nil {
  130. return errors.FailedPattern(path, in, fmt.Sprintf("%s, but pattern is invalid: %s", pattern, err.Error()))
  131. }
  132. if !re.MatchString(data) {
  133. return errors.FailedPattern(path, in, pattern)
  134. }
  135. return nil
  136. }
  137. // MaximumInt validates if a number is smaller than a given maximum
  138. func MaximumInt(path, in string, data, max int64, exclusive bool) *errors.Validation {
  139. if (!exclusive && data > max) || (exclusive && data >= max) {
  140. return errors.ExceedsMaximumInt(path, in, max, exclusive)
  141. }
  142. return nil
  143. }
  144. // MaximumUint validates if a number is smaller than a given maximum
  145. func MaximumUint(path, in string, data, max uint64, exclusive bool) *errors.Validation {
  146. if (!exclusive && data > max) || (exclusive && data >= max) {
  147. return errors.ExceedsMaximumUint(path, in, max, exclusive)
  148. }
  149. return nil
  150. }
  151. // Maximum validates if a number is smaller than a given maximum
  152. func Maximum(path, in string, data, max float64, exclusive bool) *errors.Validation {
  153. if (!exclusive && data > max) || (exclusive && data >= max) {
  154. return errors.ExceedsMaximum(path, in, max, exclusive)
  155. }
  156. return nil
  157. }
  158. // Minimum validates if a number is smaller than a given minimum
  159. func Minimum(path, in string, data, min float64, exclusive bool) *errors.Validation {
  160. if (!exclusive && data < min) || (exclusive && data <= min) {
  161. return errors.ExceedsMinimum(path, in, min, exclusive)
  162. }
  163. return nil
  164. }
  165. // MinimumInt validates if a number is smaller than a given minimum
  166. func MinimumInt(path, in string, data, min int64, exclusive bool) *errors.Validation {
  167. if (!exclusive && data < min) || (exclusive && data <= min) {
  168. return errors.ExceedsMinimumInt(path, in, min, exclusive)
  169. }
  170. return nil
  171. }
  172. // MinimumUint validates if a number is smaller than a given minimum
  173. func MinimumUint(path, in string, data, min uint64, exclusive bool) *errors.Validation {
  174. if (!exclusive && data < min) || (exclusive && data <= min) {
  175. return errors.ExceedsMinimumUint(path, in, min, exclusive)
  176. }
  177. return nil
  178. }
  179. // MultipleOf validates if the provided number is a multiple of the factor
  180. func MultipleOf(path, in string, data, factor float64) *errors.Validation {
  181. // multipleOf factor must be positive
  182. if factor < 0 {
  183. return errors.MultipleOfMustBePositive(path, in, factor)
  184. }
  185. var mult float64
  186. if factor < 1 {
  187. mult = 1 / factor * data
  188. } else {
  189. mult = data / factor
  190. }
  191. if !swag.IsFloat64AJSONInteger(mult) {
  192. return errors.NotMultipleOf(path, in, factor)
  193. }
  194. return nil
  195. }
  196. // MultipleOfInt validates if the provided integer is a multiple of the factor
  197. func MultipleOfInt(path, in string, data int64, factor int64) *errors.Validation {
  198. // multipleOf factor must be positive
  199. if factor < 0 {
  200. return errors.MultipleOfMustBePositive(path, in, factor)
  201. }
  202. mult := data / factor
  203. if mult*factor != data {
  204. return errors.NotMultipleOf(path, in, factor)
  205. }
  206. return nil
  207. }
  208. // MultipleOfUint validates if the provided unsigned integer is a multiple of the factor
  209. func MultipleOfUint(path, in string, data, factor uint64) *errors.Validation {
  210. mult := data / factor
  211. if mult*factor != data {
  212. return errors.NotMultipleOf(path, in, factor)
  213. }
  214. return nil
  215. }
  216. // FormatOf validates if a string matches a format in the format registry
  217. func FormatOf(path, in, format, data string, registry strfmt.Registry) *errors.Validation {
  218. if registry == nil {
  219. registry = strfmt.Default
  220. }
  221. if ok := registry.ContainsName(format); !ok {
  222. return errors.InvalidTypeName(format)
  223. }
  224. if ok := registry.Validates(format, data); !ok {
  225. return errors.InvalidType(path, in, format, data)
  226. }
  227. return nil
  228. }
  229. // MaximumNativeType provides native type constraint validation as a facade
  230. // to various numeric types versions of Maximum constraint check.
  231. //
  232. // Assumes that any possible loss conversion during conversion has been
  233. // checked beforehand.
  234. //
  235. // NOTE: currently, the max value is marshalled as a float64, no matter what,
  236. // which means there may be a loss during conversions (e.g. for very large integers)
  237. //
  238. // TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
  239. func MaximumNativeType(path, in string, val interface{}, max float64, exclusive bool) *errors.Validation {
  240. kind := reflect.ValueOf(val).Type().Kind()
  241. switch kind {
  242. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  243. value := valueHelp.asInt64(val)
  244. return MaximumInt(path, in, value, int64(max), exclusive)
  245. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  246. value := valueHelp.asUint64(val)
  247. if max < 0 {
  248. return errors.ExceedsMaximum(path, in, max, exclusive)
  249. }
  250. return MaximumUint(path, in, value, uint64(max), exclusive)
  251. case reflect.Float32, reflect.Float64:
  252. fallthrough
  253. default:
  254. value := valueHelp.asFloat64(val)
  255. return Maximum(path, in, value, max, exclusive)
  256. }
  257. }
  258. // MinimumNativeType provides native type constraint validation as a facade
  259. // to various numeric types versions of Minimum constraint check.
  260. //
  261. // Assumes that any possible loss conversion during conversion has been
  262. // checked beforehand.
  263. //
  264. // NOTE: currently, the min value is marshalled as a float64, no matter what,
  265. // which means there may be a loss during conversions (e.g. for very large integers)
  266. //
  267. // TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
  268. func MinimumNativeType(path, in string, val interface{}, min float64, exclusive bool) *errors.Validation {
  269. kind := reflect.ValueOf(val).Type().Kind()
  270. switch kind {
  271. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  272. value := valueHelp.asInt64(val)
  273. return MinimumInt(path, in, value, int64(min), exclusive)
  274. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  275. value := valueHelp.asUint64(val)
  276. if min < 0 {
  277. return nil
  278. }
  279. return MinimumUint(path, in, value, uint64(min), exclusive)
  280. case reflect.Float32, reflect.Float64:
  281. fallthrough
  282. default:
  283. value := valueHelp.asFloat64(val)
  284. return Minimum(path, in, value, min, exclusive)
  285. }
  286. }
  287. // MultipleOfNativeType provides native type constraint validation as a facade
  288. // to various numeric types version of MultipleOf constraint check.
  289. //
  290. // Assumes that any possible loss conversion during conversion has been
  291. // checked beforehand.
  292. //
  293. // NOTE: currently, the multipleOf factor is marshalled as a float64, no matter what,
  294. // which means there may be a loss during conversions (e.g. for very large integers)
  295. //
  296. // TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
  297. func MultipleOfNativeType(path, in string, val interface{}, multipleOf float64) *errors.Validation {
  298. kind := reflect.ValueOf(val).Type().Kind()
  299. switch kind {
  300. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  301. value := valueHelp.asInt64(val)
  302. return MultipleOfInt(path, in, value, int64(multipleOf))
  303. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  304. value := valueHelp.asUint64(val)
  305. return MultipleOfUint(path, in, value, uint64(multipleOf))
  306. case reflect.Float32, reflect.Float64:
  307. fallthrough
  308. default:
  309. value := valueHelp.asFloat64(val)
  310. return MultipleOf(path, in, value, multipleOf)
  311. }
  312. }
  313. // IsValueValidAgainstRange checks that a numeric value is compatible with
  314. // the range defined by Type and Format, that is, may be converted without loss.
  315. //
  316. // NOTE: this check is about type capacity and not formal verification such as: 1.0 != 1L
  317. func IsValueValidAgainstRange(val interface{}, typeName, format, prefix, path string) error {
  318. kind := reflect.ValueOf(val).Type().Kind()
  319. // What is the string representation of val
  320. stringRep := ""
  321. switch kind {
  322. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  323. stringRep = swag.FormatUint64(valueHelp.asUint64(val))
  324. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  325. stringRep = swag.FormatInt64(valueHelp.asInt64(val))
  326. case reflect.Float32, reflect.Float64:
  327. stringRep = swag.FormatFloat64(valueHelp.asFloat64(val))
  328. default:
  329. return fmt.Errorf("%s value number range checking called with invalid (non numeric) val type in %s", prefix, path)
  330. }
  331. var errVal error
  332. switch typeName {
  333. case "integer":
  334. switch format {
  335. case "int32":
  336. _, errVal = swag.ConvertInt32(stringRep)
  337. case "uint32":
  338. _, errVal = swag.ConvertUint32(stringRep)
  339. case "uint64":
  340. _, errVal = swag.ConvertUint64(stringRep)
  341. case "int64":
  342. fallthrough
  343. default:
  344. _, errVal = swag.ConvertInt64(stringRep)
  345. }
  346. case "number":
  347. fallthrough
  348. default:
  349. switch format {
  350. case "float", "float32":
  351. _, errVal = swag.ConvertFloat32(stringRep)
  352. case "double", "float64":
  353. fallthrough
  354. default:
  355. // No check can be performed here since
  356. // no number beyond float64 is supported
  357. }
  358. }
  359. if errVal != nil { // We don't report the actual errVal from strconv
  360. if format != "" {
  361. errVal = fmt.Errorf("%s value must be of type %s with format %s in %s", prefix, typeName, format, path)
  362. } else {
  363. errVal = fmt.Errorf("%s value must be of type %s (default format) in %s", prefix, typeName, path)
  364. }
  365. }
  366. return errVal
  367. }
上海开阖软件有限公司 沪ICP备12045867号-1