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

Workflow validations #156

Merged
merged 8 commits into from
Mar 13, 2023
3 changes: 3 additions & 0 deletions model/callback_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func TestCallbackStateStructLevelValidation(t *testing.T) {
BaseState: BaseState{
Name: "callbackTest",
Type: StateTypeCallback,
End: &End{
Terminate: true,
},
},
CallbackState: &CallbackState{
Action: Action{
Expand Down
3 changes: 3 additions & 0 deletions model/delay_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func TestDelayStateStructLevelValidation(t *testing.T) {
BaseState: BaseState{
Name: "1",
Type: "delay",
End: &End{
Terminate: true,
},
ribeiromiranda marked this conversation as resolved.
Show resolved Hide resolved
},
DelayState: &DelayState{
TimeDelay: "PT5S",
Expand Down
17 changes: 0 additions & 17 deletions model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ package model

import (
"encoding/json"
"reflect"

val "github.com/serverlessworkflow/sdk-go/v2/validator"

validator "github.com/go-playground/validator/v10"
)

// EventKind defines this event as either `consumed` or `produced`
Expand All @@ -34,18 +29,6 @@ const (
EventKindProduced EventKind = "produced"
)

func init() {
val.GetValidator().RegisterStructValidation(EventStructLevelValidation, Event{})
}

// EventStructLevelValidation custom validator for event kind consumed
func EventStructLevelValidation(structLevel validator.StructLevel) {
event := structLevel.Current().Interface().(Event)
if event.Kind == EventKindConsumed && len(event.Type) == 0 {
structLevel.ReportError(reflect.ValueOf(event.Type), "Type", "type", "reqtypeconsumed", "")
}
}

// Event used to define events and their correlations
type Event struct {
Common `json:",inline"`
Expand Down
46 changes: 0 additions & 46 deletions model/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

val "github.com/serverlessworkflow/sdk-go/v2/validator"
)

func TestEventRefStructLevelValidation(t *testing.T) {
type testCase struct {
name string
eventRef EventRef
err string
}

testCases := []testCase{
{
name: "valid resultEventTimeout",
eventRef: EventRef{
TriggerEventRef: "example valid",
ResultEventRef: "example valid",
ResultEventTimeout: "PT1H",
Invoke: InvokeKindSync,
},
err: ``,
},
{
name: "invalid resultEventTimeout",
eventRef: EventRef{
TriggerEventRef: "example invalid",
ResultEventRef: "example invalid red",
ResultEventTimeout: "10hs",
Invoke: InvokeKindSync,
},
err: `Key: 'EventRef.ResultEventTimeout' Error:Field validation for 'ResultEventTimeout' failed on the 'iso8601duration' tag`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := val.GetValidator().Struct(tc.eventRef)

if tc.err != "" {
assert.Error(t, err)
assert.Regexp(t, tc.err, err)
return
}
assert.NoError(t, err)
})
}
}

func TestEventRefUnmarshalJSON(t *testing.T) {
type testCase struct {
desp string
Expand Down
34 changes: 34 additions & 0 deletions model/event_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package model

import (
"reflect"

validator "github.com/go-playground/validator/v10"
val "github.com/serverlessworkflow/sdk-go/v2/validator"
)

func init() {
val.GetValidator().RegisterStructValidation(eventStructLevelValidation, Event{})
}

// eventStructLevelValidation custom validator for event kind consumed
func eventStructLevelValidation(structLevel validator.StructLevel) {
event := structLevel.Current().Interface().(Event)
if event.Kind == EventKindConsumed && len(event.Type) == 0 {
structLevel.ReportError(reflect.ValueOf(event.Type), "Type", "type", "reqtypeconsumed", "")
}
}
66 changes: 66 additions & 0 deletions model/event_validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2021 The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package model

import (
"testing"

val "github.com/serverlessworkflow/sdk-go/v2/validator"
"github.com/stretchr/testify/assert"
)

func TestEventRefStructLevelValidation(t *testing.T) {
type testCase struct {
name string
eventRef EventRef
err string
}

testCases := []testCase{
{
name: "valid resultEventTimeout",
eventRef: EventRef{
TriggerEventRef: "example valid",
ResultEventRef: "example valid",
ResultEventTimeout: "PT1H",
Invoke: InvokeKindSync,
},
err: ``,
},
{
name: "invalid resultEventTimeout",
eventRef: EventRef{
TriggerEventRef: "example invalid",
ResultEventRef: "example invalid red",
ResultEventTimeout: "10hs",
Invoke: InvokeKindSync,
},
err: `Key: 'EventRef.ResultEventTimeout' Error:Field validation for 'ResultEventTimeout' failed on the 'iso8601duration' tag`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := val.GetValidator().Struct(tc.eventRef)

if tc.err != "" {
assert.Error(t, err)
assert.Regexp(t, tc.err, err)
return
}
assert.NoError(t, err)
})
}
}
40 changes: 0 additions & 40 deletions model/foreach_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,12 @@
package model

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"

validator "github.com/go-playground/validator/v10"
"k8s.io/apimachinery/pkg/util/intstr"

val "github.com/serverlessworkflow/sdk-go/v2/validator"
)

func init() {
val.GetValidator().RegisterStructValidationCtx(ForEachStateStructLevelValidation, ForEachState{})
}

// ForEachModeType Specifies how iterations are to be performed (sequentially or in parallel)
type ForEachModeType string

Expand Down Expand Up @@ -86,36 +76,6 @@ func (f *ForEachState) UnmarshalJSON(data []byte) error {
return nil
}

// ForEachStateStructLevelValidation custom validator for ForEachState
func ForEachStateStructLevelValidation(_ context.Context, structLevel validator.StructLevel) {
stateObj := structLevel.Current().Interface().(ForEachState)

if stateObj.Mode != ForEachModeTypeParallel {
return
}

if stateObj.BatchSize == nil {
return
}

switch stateObj.BatchSize.Type {
case intstr.Int:
if stateObj.BatchSize.IntVal <= 0 {
structLevel.ReportError(reflect.ValueOf(stateObj.BatchSize), "BatchSize", "batchSize", "gt0", "")
}
case intstr.String:
v, err := strconv.Atoi(stateObj.BatchSize.StrVal)
if err != nil {
structLevel.ReportError(reflect.ValueOf(stateObj.BatchSize), "BatchSize", "batchSize", "gt0", err.Error())
return
}

if v <= 0 {
structLevel.ReportError(reflect.ValueOf(stateObj.BatchSize), "BatchSize", "batchSize", "gt0", "")
}
}
}

// ForEachStateTimeout defines timeout settings for foreach state
type ForEachStateTimeout struct {
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
Expand Down
Loading