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

Adopt struct validator #717

Merged

Conversation

jihoon-seo
Copy link
Member

@jihoon-seo jihoon-seo commented Sep 8, 2021

  • 본격적으로 적용하기 전에, 먼저 일부분에 대해 적용해 보았습니다.
  • Adopt struct validator
    • TbImageReq struct의 Name, ConnectionName, CspImageId 필드를 required 로 설정했습니다.
      • swag-go 가 Swagger doc 에도 잘 반영해 주는 것을 볼 수 있습니다.
    • TbImageReq struct validation 시, Name 필드에 대해서는 CheckString() 함수도 실행하도록 하였습니다.
  • Remove JSON tags from TbImageReq struct
    • Name, ConnectionName, CspImageId 필드의 JSON tag를 제거했습니다.

[테스트 결과]

  1. required 인 필드를 비워서 보냈을 때

[Req body]

{}

[Resp] - 눈에 잘 안 들어오기는 합니다.

{
    "message": "Key: 'TbImageReq.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'TbImageReq.ConnectionName' Error:Field validation for 'ConnectionName' failed on the 'required' tag\nKey: 'TbImageReq.CspImageId' Error:Field validation for 'CspImageId' failed on the 'required' tag\nKey: 'TbImageReq.Name' Error:Field validation for 'Name' failed on the 'InvalidName' tag"
}

[CB-Tumblebug console output]

[CLOUD-BARISTA].[ERROR]: 2021-09-08 14:31:44 image.go:61, github.com/cloud-barista/cb-tumblebug/src/api/rest/server/mcir.RestPostImage() - Key: 'TbImageReq.Name' Error:Field validation for 'Name' failed on the 'required' tag
Key: 'TbImageReq.ConnectionName' Error:Field validation for 'ConnectionName' failed on the 'required' tag
Key: 'TbImageReq.CspImageId' Error:Field validation for 'CspImageId' failed on the 'required' tag
Key: 'TbImageReq.Name' Error:Field validation for 'Name' failed on the 'InvalidName' tag
{"time":"2021-09-08T14:31:44.631276336+09:00","id":"","remote_ip":"192.168.1.3","host":"192.168.1.6:1323","method":"POST","uri":"/tumblebug/ns/tb01/resources/image?action=registerWithId","user_agent":"PostmanRuntime/7.26.8","status":500,"error":"","latency":498348,"latency_human":"498.348µs","bytes_in":0,"bytes_out":396}

  1. Name 필드를 이상하게 적어서, CheckString() 함수를 통과하지 못하도록 했을 때

[Req body]

{
    "connectionName": "aws-ap-se-1",
    "name": "${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}",
    "cspImageId": "${IMAGE_NAME[$INDEX,$REGION]}",
    "description": "Canonical, Ubuntu, 18.04 LTS, amd64 bionic",
    "guestOS": "Ubuntu"
}

(주: 이 PR에서 JSON 태그를 제거했기 때문에,
원칙적으로는 Req JSON body에도 struct field name과 동일하게
Name, ConnectionName, CspImageId, Description 으로 적는 것이 맞겠지만,
name, connectionName, cspImageId, description 으로 적어도 잘 인식하는 것을 볼 수 있습니다.)

[Resp]

{
    "message": "Key: 'TbImageReq.Name' Error:Field validation for 'Name' failed on the 'NotObeyingNamingConvention' tag"
}

[CB-Tumblebug console output]

[CLOUD-BARISTA].[ERROR]: 2021-09-08 14:44:01 image.go:61, github.com/cloud-barista/cb-tumblebug/src/api/rest/server/mcir.RestPostImage() - Key: 'TbImageReq.Name' Error:Field validation for 'Name' failed on the 'NotObeyingNamingConvention' tag
{"time":"2021-09-08T14:44:01.563402492+09:00","id":"","remote_ip":"192.168.1.3","host":"192.168.1.6:1323","method":"POST","uri":"/tumblebug/ns/tb01/resources/image?action=registerWithId","user_agent":"PostmanRuntime/7.26.8","status":500,"error":"","latency":208823,"latency_human":"208.823µs","bytes_in":243,"bytes_out":118}

@seokho-son
Copy link
Member

감사합니다!

@seokho-son
Copy link
Member

/approve

@seokho-son seokho-son merged commit ae4486c into cloud-barista:main Sep 8, 2021
@jihoon-seo jihoon-seo deleted the 210908_Adopt_struct_validator branch September 8, 2021 07:01
@seokho-son
Copy link
Member

ref #706

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants