Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIにバリデーションを追加 #99

Merged
merged 1 commit into from Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -10,6 +10,7 @@ require (
github.com/99designs/gqlgen v0.13.0
github.com/getsentry/sentry-go v0.11.0
github.com/go-chi/chi v3.3.2+incompatible
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible // indirect
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.1.2
github.com/joho/godotenv v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -122,6 +122,8 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
Expand Down
33 changes: 33 additions & 0 deletions graph/model/validate.go
@@ -0,0 +1,33 @@
package model

import validation "github.com/go-ozzo/ozzo-validation"

func (r UpdateUser) Validate() error {
return validation.ValidateStruct(&r,
validation.Field(
&r.DisplayName,
validation.Required.Error("名前の入力は必須です"),
validation.RuneLength(1, 30).Error("名前は最大30文字までです"),
),
)
}

func (r NewItem) Validate() error {
return validation.ValidateStruct(&r,
validation.Field(
&r.Title,
validation.Required.Error("タイトルの入力は必須です"),
validation.RuneLength(1, 100).Error("タイトルは最大100文字までです"),
),
)
}

func (r UpdateItem) Validate() error {
return validation.ValidateStruct(&r,
validation.Field(
&r.Title,
validation.Required.Error("タイトルの入力は必須です"),
validation.RuneLength(1, 100).Error("タイトルは最大100文字までです"),
),
)
}
12 changes: 12 additions & 0 deletions graph/schema.resolvers.go
Expand Up @@ -35,6 +35,10 @@ func (r *mutationResolver) CreateAuthUser(ctx context.Context, input model.NewAu
}

func (r *mutationResolver) UpdateUser(ctx context.Context, input model.UpdateUser) (*model.User, error) {
if err := input.Validate(); err != nil {
return nil, ce.CustomError(err)
}

g, err := NewGraph(ctx, r.App, r.FirestoreClient)
if err != nil {
return nil, ce.CustomError(err)
Expand All @@ -49,6 +53,10 @@ func (r *mutationResolver) UpdateUser(ctx context.Context, input model.UpdateUse
}

func (r *mutationResolver) CreateItem(ctx context.Context, input model.NewItem) (*model.Item, error) {
if err := input.Validate(); err != nil {
return nil, ce.CustomError(err)
}

g, err := NewGraph(ctx, r.App, r.FirestoreClient)
if err != nil {
return nil, ce.CustomError(err)
Expand All @@ -63,6 +71,10 @@ func (r *mutationResolver) CreateItem(ctx context.Context, input model.NewItem)
}

func (r *mutationResolver) UpdateItem(ctx context.Context, input model.UpdateItem) (*model.Item, error) {
if err := input.Validate(); err != nil {
return nil, ce.CustomError(err)
}

g, err := NewGraph(ctx, r.App, r.FirestoreClient)
if err != nil {
return nil, ce.CustomError(err)
Expand Down