validator 包

validator 包提供了一系列的方法帮助用户快速生成用于校验参数的 Operator:

  1. // OperatorKind means opeartor kind. All operators generated in this package
  2. // are has kind `validator`.
  3. const OperatorKind = "validator"
  4. // Category distinguishs validation type based on different Validator implementation.
  5. type Category string
  6. const (
  7. // CategoryVar indicates that the validator can validate basic built-in types.
  8. // Types: string, int*, uint*, bool.
  9. CategoryVar Category = "Var"
  10. // CategoryStruct indicates that the validator can validate struct.
  11. CategoryStruct Category = "Struct"
  12. // CategoryCustom indicates the validator is a custom validator.
  13. CategoryCustom Category = "Custom"
  14. )
  15. // Validator describes an interface for all validator.
  16. type Validator interface {
  17. definition.Operator
  18. // Category indicates validator type.
  19. Category() Category
  20. // Tag returns tag.
  21. Tag() string
  22. // Description returns description of current validator.
  23. Description() string
  24. }
  25. // NewCustom calls f for validation, using description for doc gen.
  26. // User should only do custom validation in f.
  27. // Validations which can be done by other way should be done in another Operator.
  28. // Exp:
  29. // []definition.Operator{NewCustom(f,"custom validation description")}
  30. // f should be func(ctx context.Context, object AnyType) error
  31. func NewCustom(f interface{}, description string) Validator
  32. // Struct returns an operator to validate a structs exposed fields, and automatically validates nested structs, unless otherwise specified
  33. // and also allows passing of context.Context for contextual validation information.
  34. func Struct(instance interface{}) Validator
  35. // String creates validator for string type.
  36. func String(tag string) Validator
  37. // Int creates validator for int type.
  38. func Int(tag string) Validator
  39. // Int64 creates validator for int64 type.
  40. func Int64(tag string) Validator
  41. // Int32 creates validator for int32 type.
  42. func Int32(tag string) Validator
  43. // Int16 creates validator for int16 type.
  44. func Int16(tag string) Validator
  45. // Int8 creates validator for int8 type.
  46. func Int8(tag string) Validator
  47. // Byte creates validator for byte type.
  48. func Byte(tag string) Validator
  49. // Uint creates validator for uint type.
  50. func Uint(tag string) Validator
  51. // Uint64 creates validator for uint64 type.
  52. func Uint64(tag string) Validator
  53. // Uint32 creates validator for uint32 type.
  54. func Uint32(tag string) Validator
  55. // Uint16 creates validator for uint16 type.
  56. func Uint16(tag string) Validator
  57. // Uint8 creates validator for uint8 type.
  58. func Uint8(tag string) Validator
  59. // Bool creates validator for bool type.
  60. func Bool(tag string) Validator

目前支持三种类型的验证,分别对应 golang 基础类型,结构体类型和自定义类型。目前验证方法基于 gopkg.in/go-playground/validator.v9,如果希望使用其他工具,可以自定义一套类似的验证函数。