feat(protocol): validate generated gateway contracts
This commit is contained in:
@@ -414,6 +414,9 @@ func (v AllocationPolicy) Validate() error {
|
||||
if v.ReservationLeaseSeconds > 3600 {
|
||||
violations = append(violations, FieldViolation{Field: "reservation_lease_seconds", Code: "maximum"})
|
||||
}
|
||||
if v.MinimumKbps > v.TargetKbps || v.TargetKbps > v.MaximumKbps {
|
||||
violations = append(violations, FieldViolation{Field: "bounds", Code: "invalid_order"})
|
||||
}
|
||||
if len(violations) > 0 {
|
||||
return ValidationError{Violations: violations}
|
||||
}
|
||||
@@ -918,6 +921,9 @@ func (v ChannelFrame) Validate() error {
|
||||
if len(v.Payload) > 87384 {
|
||||
violations = append(violations, FieldViolation{Field: "payload", Code: "max_length"})
|
||||
}
|
||||
if v.FragmentIndex >= v.FragmentCount {
|
||||
violations = append(violations, FieldViolation{Field: "fragment_index", Code: "invalid_order"})
|
||||
}
|
||||
if len(violations) > 0 {
|
||||
return ValidationError{Violations: violations}
|
||||
}
|
||||
@@ -2227,6 +2233,9 @@ func (v GatewayRegistration) Validate() error {
|
||||
if err := v.Capabilities.Validate(); err != nil {
|
||||
violations = append(violations, FieldViolation{Field: "capabilities", Code: "invalid_object"})
|
||||
}
|
||||
if v.ProtocolMinVersion > v.ProtocolMaxVersion {
|
||||
violations = append(violations, FieldViolation{Field: "protocol_version", Code: "invalid_order"})
|
||||
}
|
||||
if len(violations) > 0 {
|
||||
return ValidationError{Violations: violations}
|
||||
}
|
||||
@@ -2484,6 +2493,9 @@ func (v ManifestBounds) Validate() error {
|
||||
if v.MaximumKbps > 100000000 {
|
||||
violations = append(violations, FieldViolation{Field: "maximum_kbps", Code: "maximum"})
|
||||
}
|
||||
if v.MinimumKbps > v.TargetKbps || v.TargetKbps > v.MaximumKbps {
|
||||
violations = append(violations, FieldViolation{Field: "bounds", Code: "invalid_order"})
|
||||
}
|
||||
if len(violations) > 0 {
|
||||
return ValidationError{Violations: violations}
|
||||
}
|
||||
@@ -4043,3 +4055,21 @@ func EncodeVersionNegotiation(value VersionNegotiation) ([]byte, error) {
|
||||
}
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
var ErrNoCapabilityOverlap = errors.New("no capability overlap")
|
||||
|
||||
func IntersectCapabilityProfiles(profiles ...CapabilityProfile) (CapabilityProfile, error) {
|
||||
if len(profiles) == 0 {
|
||||
return CapabilityProfile{}, ErrNoCapabilityOverlap
|
||||
}
|
||||
selected := profiles[0]
|
||||
if err := selected.Validate(); err != nil {
|
||||
return CapabilityProfile{}, ErrNoCapabilityOverlap
|
||||
}
|
||||
for _, profile := range profiles[1:] {
|
||||
if err := profile.Validate(); err != nil || profile != selected {
|
||||
return CapabilityProfile{}, ErrNoCapabilityOverlap
|
||||
}
|
||||
}
|
||||
return selected, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user