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

Add DID static validation without tests #291

Merged
merged 1 commit into from
Mar 3, 2022
Merged

Conversation

askolesov
Copy link
Contributor

Signed-off-by: Andrew Nikitin [email protected]

@ankurdotb
Copy link
Contributor

Copy link
Contributor Author

@askolesov askolesov left a comment

Choose a reason for hiding this comment

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

This is a rocket, really. Just a few changes to apply

x/cheqd/types/did_validation.go Show resolved Hide resolved
x/cheqd/types/did_validation.go Show resolved Hide resolved
x/cheqd/types/did_validation.go Show resolved Hide resolved
match := SplitDIDRegexp.FindStringSubmatch(did)
if len(match) > 0 {
return match[1], match[3], match[4]
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's throw an error otherwise

return ErrStaticDIDBadMethod.Wrap(method)
}
// check namespaces
if !DidNamespaceRegexp.MatchString(namespace) || !utils.Contains(allowedNamespaces, namespace) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's move regexp matches to the split method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So validate will only compare namespace / method with passed parameters

Copy link
Contributor

Choose a reason for hiding this comment

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

Will it be cool to throw the exception while splitting the DID? From my point of view it's a phase of validation.

return ErrStaticDIDNamespaceNotAllowed.Wrap(namespace)
}
// check unique-id
err := ValidateUniqueId(unique_id)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one too

return err == nil
}

// SplitDID panics if did is not valid
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't panic :) Return an error

"regexp"
)

var SplitDIDRegexp, _ = regexp.Compile(`did:([^:]+?)(:([^:]+?))?:([^:]+)$`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
var SplitDIDRegexp, _ = regexp.Compile(`did:([^:]+?)(:([^:]+?))?:([^:]+)$`)
var SplitDIDRegexp, _ = regexp.Compile(`^did:([^:]+?)(:([^:]+?))?:([^:]+)$`)


// SplitDIDUrl panics if did cannot be splitted properly
func SplitDIDUrl(didUrl string) (did string, path string , query string, fragment string) {
match := SplitDIDURL.FindStringSubmatch(didUrl)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's check for matches count

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't we check for matches count?

Copy link
Contributor

Choose a reason for hiding this comment

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

It matches only once for Golang regexps... I checked it

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW, The last changes in regexp doesn't match in case of fragment being #key123???#####

return err
}
// Validate path
err = ValidatePath(path)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And let's move those checks to the split method as well

Copy link
Contributor

Choose a reason for hiding this comment

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

Why we need to mix validation and splitting?

@askolesov askolesov merged commit 00e222a into DEV-705 Mar 3, 2022
@askolesov askolesov deleted the DEV-705-did-validation branch March 3, 2022 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants