-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
182 lines (147 loc) · 4.99 KB
/
validation.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gossamer
import (
"errors"
)
func ValidateMandatoryProperties(entity SensorThing) error {
switch entity.GetType() {
case ENTITY_THINGS:
e := (entity).(*ThingEntity)
if e.Description == "" {
return errors.New("Missing mandatory property for Thing entity: 'description'")
}
case ENTITY_OBSERVATIONS:
e := (entity).(*ObservationEntity)
if e.PhenomenonTime == nil {
return errors.New("Missing mandatory property for Observation entity: 'phenomenonTime'")
}
if e.PhenomenonTime.IsZero() {
return errors.New("Missing mandatory property for Observation entity: 'phenomenonTime'")
}
if e.ResultTime == nil {
return errors.New("Missing mandatory property for Observation entity: 'resultTime'")
}
if e.ResultTime.IsZero() {
return errors.New("Missing mandatory property for Observation entity: 'resultTime'")
}
if e.Result == nil {
return errors.New("Missing mandatory property for Observation entity: 'result'")
}
case ENTITY_HISTORICALLOCATIONS:
e := (entity).(*HistoricalLocationEntity)
if e.Time == nil {
return errors.New("Missing mandatory property for HistoricalLocation entity: 'time'")
}
if e.Time.IsZero() {
return errors.New("Missing mandatory property for HistoricalLocation entity: 'time'")
}
case ENTITY_SENSORS:
e := (entity).(*SensorEntity)
if e.Description == "" {
return errors.New("Missing mandatory property for Sensor entity: 'description'")
}
if e.EncodingType == "" {
return errors.New("Missing mandatory property for Sensor entity: 'encodingType'")
}
if e.Metadata == nil || e.Metadata == "" {
return errors.New("Missing mandatory property for Sensor entity: 'metadata'")
}
case ENTITY_LOCATIONS:
e := (entity).(*LocationEntity)
if e.Description == "" {
return errors.New("Missing mandatory property for Location entity: 'description'")
}
if e.EncodingType == "" {
return errors.New("Missing mandatory property for Location entity: 'encodingType'")
}
if e.Location == nil {
return errors.New("Missing mandatory property for Location entity: 'location'")
}
case ENTITY_FEATURESOFINTEREST:
e := (entity).(*FeatureOfInterestEntity)
if e.Description == "" {
return errors.New("Missing mandatory property for FeaturesOfInterest entity: 'description'")
}
if e.EncodingType == "" {
return errors.New("Missing mandatory property for FeaturesOfInterest entity: 'encodingType'")
}
if e.Feature == nil {
return errors.New("Missing mandatory property for FeaturesOfInterest entity: 'feature'")
}
case ENTITY_DATASTREAMS:
e := (entity).(*DatastreamEntity)
if e.Description == "" {
return errors.New("Missing mandatory property for Datastream entity: 'description'")
}
if e.UnitOfMeasurement == nil {
return errors.New("Missing mandatory property for Datastream entity: 'unitOfMeasurement'")
}
if e.ObservationType == "" {
return errors.New("Missing mandatory property for Datastream entity: 'observationType'")
}
case ENTITY_OBSERVEDPROPERTIES:
e := (entity).(*ObservedPropertyEntity)
if e.Name == "" {
return errors.New("Missing mandatory property for ObservedProperty entity: 'name'")
}
if e.Definition == "" {
return errors.New("Missing mandatory property for ObservedProperty entity: 'definition'")
}
if e.Description == "" {
return errors.New("Missing mandatory property for ObservedProperty entity: 'description'")
}
}
return nil
}
func ValidateIntegrityConstraints(entity SensorThing) error {
switch entity.GetType() {
case ENTITY_OBSERVATIONS:
e := (entity).(*ObservationEntity)
if e.IdDatastream == "" && e.Datastream == nil {
return errors.New("Missing constrains for Observation Entity: 'Datastream'")
}
case ENTITY_DATASTREAMS:
e := (entity).(*DatastreamEntity)
if e.IdThing == "" && e.Thing == nil {
return errors.New("Missing constrains for Datastream Entity: 'Thing'")
}
if e.IdSensor == "" && e.Sensor == nil {
return errors.New("Missing constrains for Datastream Entity: 'Sensor'")
}
if e.IdObservedProperty == "" && e.ObservedProperty == nil {
return errors.New("Missing constrains for Datastream Entity: 'ObservedProperty'")
}
}
return nil
}
// TODO: Refactor this into whitelists
func ValidatePostRequestUrl(req Request) error {
rp := req.GetResourcePath()
last := rp.Last()
lastEntity := last.GetEntity()
cont := rp.Containing()
if cont == nil {
if last.GetEntity() == ENTITY_HISTORICALLOCATIONS {
return errors.New("Not allowed")
}
return nil
}
contEntity := cont.GetEntity()
if contEntity == ENTITY_THINGS && lastEntity == ENTITY_LOCATIONS ||
contEntity == ENTITY_FEATURESOFINTEREST && lastEntity == ENTITY_DATASTREAMS ||
contEntity == ENTITY_DATASTREAMS && lastEntity == ENTITY_OBSERVATIONS {
return nil
}
return errors.New("Not allowed")
}
func ValidateGetRequestUrl(req Request) error {
return nil
}
func ValidatePutRequestUrl(req Request) error {
return nil
}
func ValidateDeleteRequestUrl(req Request) error {
return nil
}
func ValidatePatchRequestUrl(req Request) error {
return nil
}