Skip to content

Commit

Permalink
created new validation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gaokevin1 committed Dec 17, 2024
1 parent 80faf1b commit 4fb4b0c
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions internal/models/authentication/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ func (m *OAuthModel) Validate(h *helpers.Handler) {
}
}

func ensureRequiredCustomProviderField(h *helpers.Handler, field types.String, fieldKey, name string) {
if field.ValueString() == "" {
h.Error(fmt.Sprintf("Custom provider must set their %s", fieldKey), "no %s found for custom provider %s", fieldKey, name)
func ensureRequiredCustomProviderField(h *helpers.Handler, field any, fieldKey, name string) {
switch v := field.(type) {
case types.String:
if v.ValueString() == "" {
h.Error(fmt.Sprintf("Custom provider must set their %s", fieldKey), "no %s found for custom provider %s", fieldKey, name)
}
case []string:
if len(v) == 0 {
h.Error(fmt.Sprintf("Custom provider must set their %s", fieldKey), "no %s found for custom provider %s", fieldKey, name)
}
default:
h.Error(fmt.Sprintf("Invalid field type for %s", fieldKey), "unexpected type for field %s in custom provider %s", fieldKey, name)
}
}

Expand Down Expand Up @@ -126,9 +135,21 @@ func validateSystemProvider(h *helpers.Handler, m *OAuthProviderModel, name stri
}
}

func ensureNoCustomProviderFields(h *helpers.Handler, field types.String, fieldKey, name string) {
if !field.IsUnknown() && !field.IsNull() {
h.Error(fmt.Sprintf("The %s field is reserved for custom providers", fieldKey), "%s is a system provider and cannot specify %s reserved for custom provider", name, fieldKey)
func ensureNoCustomProviderFields(h *helpers.Handler, field any, fieldKey, name string) {
switch v := field.(type) {
case types.String:
if !v.IsUnknown() && !v.IsNull() {
h.Error(fmt.Sprintf("The %s field is reserved for custom providers", fieldKey),
"%s is a system provider and cannot specify %s reserved for custom provider", name, fieldKey)
}
case []string:
if len(v) > 0 {
h.Error(fmt.Sprintf("The %s field is reserved for custom providers", fieldKey),
"%s is a system provider and cannot specify %s reserved for custom provider", name, fieldKey)
}
default:
h.Error(fmt.Sprintf("Invalid field type for %s", fieldKey),
"unexpected type for field %s in system provider %s", fieldKey, name)
}
}

Expand Down

0 comments on commit 4fb4b0c

Please sign in to comment.