This init is used to create a validator and register validation-specific functionality for the HTTP API. A single validator instance is used, because it caches struct parsing.
()
| 31 | // |
| 32 | // A single validator instance is used, because it caches struct parsing. |
| 33 | func init() { |
| 34 | Validate = validator.New() |
| 35 | Validate.RegisterTagNameFunc(func(fld reflect.StructField) string { |
| 36 | name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0] |
| 37 | if name == "-" { |
| 38 | return "" |
| 39 | } |
| 40 | return name |
| 41 | }) |
| 42 | |
| 43 | nameValidator := func(fl validator.FieldLevel) bool { |
| 44 | f := fl.Field().Interface() |
| 45 | str, ok := f.(string) |
| 46 | if !ok { |
| 47 | return false |
| 48 | } |
| 49 | valid := codersdk.NameValid(str) |
| 50 | return valid == nil |
| 51 | } |
| 52 | for _, tag := range []string{"username", "organization_name", "template_name", "workspace_name", "oauth2_app_name"} { |
| 53 | err := Validate.RegisterValidation(tag, nameValidator) |
| 54 | if err != nil { |
| 55 | panic(err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | displayNameValidator := func(fl validator.FieldLevel) bool { |
| 60 | f := fl.Field().Interface() |
| 61 | str, ok := f.(string) |
| 62 | if !ok { |
| 63 | return false |
| 64 | } |
| 65 | valid := codersdk.DisplayNameValid(str) |
| 66 | return valid == nil |
| 67 | } |
| 68 | for _, displayNameTag := range []string{"organization_display_name", "template_display_name", "group_display_name"} { |
| 69 | err := Validate.RegisterValidation(displayNameTag, displayNameValidator) |
| 70 | if err != nil { |
| 71 | panic(err) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | templateVersionNameValidator := func(fl validator.FieldLevel) bool { |
| 76 | f := fl.Field().Interface() |
| 77 | str, ok := f.(string) |
| 78 | if !ok { |
| 79 | return false |
| 80 | } |
| 81 | valid := codersdk.TemplateVersionNameValid(str) |
| 82 | return valid == nil |
| 83 | } |
| 84 | err := Validate.RegisterValidation("template_version_name", templateVersionNameValidator) |
| 85 | if err != nil { |
| 86 | panic(err) |
| 87 | } |
| 88 | |
| 89 | userRealNameValidator := func(fl validator.FieldLevel) bool { |
| 90 | f := fl.Field().Interface() |
nothing calls this directly
no test coverage detected