Skip to content

Commit

Permalink
fix: remove "required" binding from Deployable bool field (#341)
Browse files Browse the repository at this point in the history
* fix: remove "required" binding from Deployable bool field

If the Deployable bool field is `false`, the validation from the "required" tag will fail,
as it's considered the zero-value for bools. In our case both `true` and `false`
are valid, so there's no need to use the "required" tag.

go-playground/validator#713

* fix: handle duplicated key error for group creation
  • Loading branch information
radnov committed Jul 20, 2023
1 parent 90f25e1 commit 326d1d6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/group/docs.go
Expand Up @@ -2,7 +2,7 @@ package group

// swagger:parameters groupCreate
type _ struct {
// Refresh token request body parameter
// Create group request body parameter
// in: body
// required: true
Body CreateGroupRequest
Expand Down
2 changes: 1 addition & 1 deletion pkg/group/handler.go
Expand Up @@ -33,7 +33,7 @@ type groupService interface {
type CreateGroupRequest struct {
Name string `json:"name" binding:"required"`
Hostname string `json:"hostname" binding:"required"`
Deployable bool `json:"deployable" binding:"required"`
Deployable bool `json:"deployable"`
}

// Create group
Expand Down
8 changes: 7 additions & 1 deletion pkg/group/repository.go
Expand Up @@ -83,7 +83,13 @@ func findAllFromUser(user *model.User, deployable bool) []model.Group {
}

func (r repository) create(group *model.Group) error {
return r.db.Create(&group).Error
err := r.db.Create(&group).Error
if errors.Is(err, gorm.ErrDuplicatedKey) {
// TODO how to check if name or hostname is duplicated?
return errdef.NewDuplicated("group name/hostname already exists: %s", err)
}

return err
}

func (r repository) findOrCreate(group *model.Group) (*model.Group, error) {
Expand Down
4 changes: 3 additions & 1 deletion scripts/users/createGroup.sh
Expand Up @@ -4,8 +4,10 @@ set -euo pipefail

GROUP=$1
HOSTNAME=$2
DEPLOYABLE=${3:-false}

echo "{
\"name\": \"$GROUP\",
\"hostname\": \"$HOSTNAME\"
\"hostname\": \"$HOSTNAME\",
\"deployable\": $DEPLOYABLE
}" | $HTTP post "$IM_HOST/groups" "Authorization: Bearer $ACCESS_TOKEN"
2 changes: 1 addition & 1 deletion swagger/swagger.yaml
Expand Up @@ -2106,7 +2106,7 @@ paths:
description: Create a group...
operationId: groupCreate
parameters:
- description: Refresh token request body parameter
- description: Create group request body parameter
in: body
name: Body
required: true
Expand Down

0 comments on commit 326d1d6

Please sign in to comment.