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

Sc 12283/db validation #86

Merged
merged 13 commits into from Jan 13, 2023
Merged

Sc 12283/db validation #86

merged 13 commits into from Jan 13, 2023

Conversation

daniellemaxwell
Copy link
Contributor

@daniellemaxwell daniellemaxwell commented Jan 10, 2023

Scope of changes

Adds validation method to the db package for the member, project, and topic models that will be used when creating or updating these resources. The members and projects db required two validation methods as they have 2 create methods and one requires a TenantID while another doesn't.

Minor updates to server-side handlers and tests were required.

Note: Validation may be added to the tenant model in the future.

Fixes SC-12283

Type of change

  • new feature
  • bug fix
  • documentation
  • testing
  • technical debt
  • other (describe)

Acceptance criteria

Describe how reviewers can test this change to be sure that it works correctly. Add a checklist if possible.

Author checklist

  • I have manually tested the change and/or added automation in the form of unit tests or integration tests
  • I have updated the dependencies list
  • I have recompiled and included new protocol buffers to reflect changes I made
  • I have added new test fixtures as needed to support added tests
  • Check this box if a reviewer can merge this pull request after approval (leave it unchecked if you want to do it yourself)
  • I have moved the associated Shortcut story to "Ready for Review"

Reviewer(s) checklist

  • Any new user-facing content that has been added for this PR has been QA'ed to ensure correct grammar, spelling, and understandability.

@shortcut-integration
Copy link

Copy link
Contributor

@pdeziel pdeziel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good start! I did have some suggestions in the comments about how we can reduce some of the repeated code, which should make this more maintainable in the future.

pkg/tenant/db/members.go Outdated Show resolved Hide resolved

if strings.ContainsAny(memberName, " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") {
return ErrSpecialCharacters
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are doing similar validation for the models, I'm wondering if we should put these checks into a separate method so that if we have to add or change validation checks in the future we don't have to make the same change to three different places.

Better yet, we could utilize a regular expression like this. This would allow us to more accurately specify what the acceptable names are since regex is basically designed for user input validation. However that can probably be a separate story.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the methods have been added to each model, I decided to keep doing that for now. Also, the models could change.

The link didn't work for me and I wasn't able to find exactly what was referenced. Could you try resending?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a follow up story to focus on refactoring the name checks in a way that reduces the repeated code between the different files? I can add some details on the regex there.

I do think it's a good idea to have different validation methods for the different models (to reduce coupling between them), but we might be able to standardize the input checking a bit.


// Validates data in the member model when a TenantID is required.
// Ex. CreateTenantProject and UpdateProject
func (m *Member) ValidateWithID() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about combining Validate and ValidateWithID into one method and just passing in a boolean which indicates whether or not the ID should be validated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to have a TenantID in the models, but this then causes errors with the server-side CreateMember and CreateProject handlers. I created a ValidateID method that only checks for the ID to eliminate the redundant code. Although, this might not exactly be the most optimal solution at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdeziel For now validation will only be applied to TenantCreateMember, TenantCreateProject, and ProjectTopicCreate.

pkg/tenant/db/members.go Show resolved Hide resolved
err = db.CreateMember(ctx, member)
require.NoError(err, "could not create member")

require.NotEqual("", member.ID, "expected non-zero ulid to be populated")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can also use require.NotEmpty which is slightly cleaner.

Copy link
Contributor

@pdeziel pdeziel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making the changes; this is so much clearer to me as a reader of the code. I have one small suggestion to add a quick test case to make sure the empty string check is working and another suggestion to add a follow on refactoring story but once those are complete feel free to merge!


if memberName == "" {
return ErrMissingMemberName
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Let's add a test case for this and to the other models so that we protect ourselves against regressions when we make future changes to the validation code.

@daniellemaxwell daniellemaxwell merged commit 7799992 into main Jan 13, 2023
@daniellemaxwell daniellemaxwell deleted the sc-12283/db-validation branch January 13, 2023 22:12
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.

None yet

2 participants