-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add Valid() methods for TagID and Evidence #45
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
Changes from 1 commit
8afdc02
011f777
dd45316
3e76bd6
54797d7
9edaa4b
dc7d4b5
c54309d
c89d821
cbbe7b8
ceef354
67d21e8
53be192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,11 @@ | |
|
|
||
| package swid | ||
|
|
||
| import "time" | ||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // Evidence models a evidence-entry | ||
| type Evidence struct { | ||
|
|
@@ -53,3 +57,34 @@ func (e *Evidence) AddProcess(p Process) error { | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Valid validates the Evidence receiver to ensure it has valid required fields | ||
| func (e Evidence) Valid() error { | ||
| if e.DeviceID == "" { | ||
| return errors.New("evidence device-id is empty") | ||
| } | ||
|
|
||
| if e.Date.IsZero() { | ||
| return errors.New("evidence date is zero") | ||
| } | ||
|
|
||
| // Validate Files if present | ||
| if e.Files != nil { | ||
| for i, file := range *e.Files { | ||
| if file.FileSystemItem.FsName == "" { | ||
| return fmt.Errorf("evidence file[%d] fs-name is empty", i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Validate Processes if present | ||
| if e.Processes != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for process, may be we are ok to check here as there is not much to check for validity of the process elements.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok sir |
||
| for i, process := range *e.Processes { | ||
| if process.ProcessName == "" { | ||
| return fmt.Errorf("evidence process[%d] process-name is empty", i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||
| // Copyright 2020 Contributors to the Veraison project. | ||||||
|
||||||
| // Copyright 2020 Contributors to the Veraison project. | |
| // Copyright 2020-2025 Contributors to the Veraison project. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,28 @@ func (t TagID) URI() string { | |
| } | ||
| } | ||
|
|
||
| // Valid validates the TagID receiver to ensure it has a valid value | ||
| func (t TagID) Valid() error { | ||
| if t.val == nil { | ||
| return errors.New("tag-id value is nil") | ||
| } | ||
|
|
||
| switch v := t.val.(type) { | ||
| case string: | ||
| if v == "" { | ||
| return errors.New("tag-id string value is empty") | ||
| } | ||
| case uuid.UUID: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should call the Validate method on UUID..?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @yogeshbdeshpande, thank you for the review! I investigated the UUID validation you mentioned. The
UUIDs in TagID are already validated during creation via If you'd like additional validation (e.g., checking Version() or Variant()), I'm happy to add that. Could you clarify what specific validation you had in mind?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks sir, @yogeshbdeshpande Sorry, You're absolutely right! Thank you for the link. I was looking at v1.3.0 (our current version) which doesn't have Validate, but the newer versions do have uuid.Validate(s string) as a package function. However, since we're dealing with an already-parsed uuid.UUID (not a string), we can't use uuid.Validate() directly. But I can enhance the validation to be more thorough. Would you like me to:
Thanks for catching this - great learning moment!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am looking into this further, will update you shortly!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, it is ok, to just check for the Variant here. Please see:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure sir @yogeshbdeshpande thank you for review and merging, thanks to all mentors also |
||
| if v == uuid.Nil { | ||
| return errors.New("tag-id UUID value is nil UUID") | ||
| } | ||
| default: | ||
| return fmt.Errorf("tag-id value must be string or uuid.UUID, got %T", v) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // MarshalXMLAttr encodes the TagID receiver as XML attribute | ||
| func (t TagID) MarshalXMLAttr(name xml.Name) (xml.Attr, error) { | ||
| return xml.Attr{Name: name, Value: t.String()}, nil | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The correct implementation is to invoke Method Valid() on the File Object and let File Object, in the file
file.goto implement the Valid Method that can be invoked from line 71. In the Valid for file, check for validity of Mandatory (like FsName) and check for presence of optional elements in the file object. If Optional Elements are present, then please check their validity!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure sir checking it @yogeshbdeshpande