|
| 1 | +// Copyright 2022 The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package model |
| 16 | + |
| 17 | +import ( |
| 18 | + validator "github.com/go-playground/validator/v10" |
| 19 | + |
| 20 | + val "github.com/serverlessworkflow/sdk-go/v2/validator" |
| 21 | +) |
| 22 | + |
| 23 | +func init() { |
| 24 | + val.GetValidator().RegisterStructValidationCtx(ValidationWrap(actionStructLevelValidationCtx), Action{}) |
| 25 | + val.GetValidator().RegisterStructValidationCtx(ValidationWrap(functionRefStructLevelValidation), FunctionRef{}) |
| 26 | +} |
| 27 | + |
| 28 | +func actionStructLevelValidationCtx(ctx ValidatorContext, structLevel validator.StructLevel) { |
| 29 | + action := structLevel.Current().Interface().(Action) |
| 30 | + |
| 31 | + if action.FunctionRef == nil && action.EventRef == nil && action.SubFlowRef == nil { |
| 32 | + structLevel.ReportError(action.FunctionRef, "FunctionRef", "FunctionRef", "required_without", "") |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + values := []bool{ |
| 37 | + action.FunctionRef != nil, |
| 38 | + action.EventRef != nil, |
| 39 | + action.SubFlowRef != nil, |
| 40 | + } |
| 41 | + |
| 42 | + if validationNotExclusiveParamters(values) { |
| 43 | + structLevel.ReportError(action.FunctionRef, "FunctionRef", "FunctionRef", val.TagExclusive, "") |
| 44 | + structLevel.ReportError(action.EventRef, "EventRef", "EventRef", val.TagExclusive, "") |
| 45 | + structLevel.ReportError(action.SubFlowRef, "SubFlowRef", "SubFlowRef", val.TagExclusive, "") |
| 46 | + } |
| 47 | + |
| 48 | + if action.RetryRef != "" && !ctx.ExistRetry(action.RetryRef) { |
| 49 | + structLevel.ReportError(action.RetryRef, "RetryRef", "RetryRef", val.TagExists, "") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func functionRefStructLevelValidation(ctx ValidatorContext, structLevel validator.StructLevel) { |
| 54 | + functionRef := structLevel.Current().Interface().(FunctionRef) |
| 55 | + if !ctx.ExistFunction(functionRef.RefName) { |
| 56 | + structLevel.ReportError(functionRef.RefName, "RefName", "RefName", val.TagExists, functionRef.RefName) |
| 57 | + } |
| 58 | +} |
0 commit comments