This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
/
artifact_validator.go
104 lines (84 loc) · 2.39 KB
/
artifact_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package validators
import (
"fmt"
"github.com/lyft/datacatalog/pkg/common"
datacatalog "github.com/lyft/datacatalog/protos/gen"
)
const (
artifactID = "artifactID"
artifactDataEntity = "artifactData"
artifactEntity = "artifact"
)
func ValidateGetArtifactRequest(request datacatalog.GetArtifactRequest) error {
if request.QueryHandle == nil {
return NewMissingArgumentError(fmt.Sprintf("one of %s/%s", artifactID, tagName))
}
switch request.QueryHandle.(type) {
case *datacatalog.GetArtifactRequest_ArtifactId:
if request.Dataset != nil {
err := ValidateDatasetID(request.Dataset)
if err != nil {
return err
}
}
if err := ValidateEmptyStringField(request.GetArtifactId(), artifactID); err != nil {
return err
}
case *datacatalog.GetArtifactRequest_TagName:
if err := ValidateDatasetID(request.Dataset); err != nil {
return err
}
if err := ValidateEmptyStringField(request.GetTagName(), tagName); err != nil {
return err
}
default:
return NewInvalidArgumentError("QueryHandle", "invalid type")
}
return nil
}
func ValidateEmptyArtifactData(artifactData []*datacatalog.ArtifactData) error {
if len(artifactData) == 0 {
return NewMissingArgumentError(artifactDataEntity)
}
return nil
}
func ValidateArtifact(artifact *datacatalog.Artifact) error {
if artifact == nil {
return NewMissingArgumentError(artifactEntity)
}
if err := ValidateDatasetID(artifact.Dataset); err != nil {
return err
}
if err := ValidateEmptyStringField(artifact.Id, artifactID); err != nil {
return err
}
if err := ValidateEmptyArtifactData(artifact.Data); err != nil {
return err
}
return nil
}
// Validate the list request and format the request with proper defaults if not provided
func ValidateListArtifactRequest(request *datacatalog.ListArtifactsRequest) error {
if err := ValidateDatasetID(request.Dataset); err != nil {
return err
}
if err := ValidateArtifactFilterTypes(request.Filter.GetFilters()); err != nil {
return err
}
if request.Pagination != nil {
err := ValidatePagination(*request.Pagination)
if err != nil {
return err
}
}
return nil
}
// Artifacts cannot be filtered across Datasets
func ValidateArtifactFilterTypes(filters []*datacatalog.SinglePropertyFilter) error {
for _, filter := range filters {
if filter.GetDatasetFilter() != nil {
return NewInvalidFilterError(common.Artifact, common.Dataset)
}
}
return nil
}