Skip to content

Commit 4a0a0d4

Browse files
author
苏义超
committed
add StartParamListValidator
1 parent 7fd5808 commit 4a0a0d4

File tree

4 files changed

+85
-41
lines changed

4 files changed

+85
-41
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.api.validator;
19+
20+
import org.apache.dolphinscheduler.plugin.task.api.enums.Direct;
21+
import org.apache.dolphinscheduler.plugin.task.api.model.Property;
22+
23+
import org.apache.commons.collections.CollectionUtils;
24+
import org.apache.commons.lang3.StringUtils;
25+
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Set;
29+
30+
import lombok.extern.slf4j.Slf4j;
31+
32+
import org.springframework.stereotype.Component;
33+
34+
/**
35+
* Validator for the list of start parameters (Property list).
36+
* <p> If startParamList is not valid, an {@link IllegalArgumentException} will be thrown. </p>
37+
*/
38+
@Slf4j
39+
@Component
40+
public class StartParamListValidator implements IValidator<List<Property>> {
41+
42+
@Override
43+
public void validate(List<Property> startParamList) {
44+
if (CollectionUtils.isEmpty(startParamList)) {
45+
return;
46+
}
47+
48+
Set<String> keys = new HashSet<>();
49+
for (Property param : startParamList) {
50+
if (StringUtils.isEmpty(param.getProp())) {
51+
throw new IllegalArgumentException("Parameter key cannot be empty");
52+
}
53+
54+
String key = param.getProp().trim();
55+
if (keys.contains(key)) {
56+
throw new IllegalArgumentException("Duplicate parameter key: " + key);
57+
}
58+
keys.add(key);
59+
60+
if (Direct.IN.equals(param.getDirect()) && StringUtils.isEmpty(param.getValue())) {
61+
throw new IllegalArgumentException("IN parameter value cannot be empty for key: " + key);
62+
}
63+
}
64+
}
65+
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/BackfillWorkflowDTOValidator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.dolphinscheduler.api.validator.workflow;
1919

2020
import org.apache.dolphinscheduler.api.validator.IValidator;
21+
import org.apache.dolphinscheduler.api.validator.StartParamListValidator;
2122
import org.apache.dolphinscheduler.api.validator.TenantExistValidator;
2223
import org.apache.dolphinscheduler.common.enums.CommandType;
2324
import org.apache.dolphinscheduler.common.enums.ReleaseState;
@@ -34,8 +35,12 @@ public class BackfillWorkflowDTOValidator implements IValidator<BackfillWorkflow
3435

3536
private final TenantExistValidator tenantExistValidator;
3637

37-
public BackfillWorkflowDTOValidator(TenantExistValidator tenantExistValidator) {
38+
private final StartParamListValidator startParamListValidator;
39+
40+
public BackfillWorkflowDTOValidator(TenantExistValidator tenantExistValidator,
41+
StartParamListValidator startParamListValidator) {
3842
this.tenantExistValidator = tenantExistValidator;
43+
this.startParamListValidator = startParamListValidator;
3944
}
4045

4146
@Override
@@ -59,6 +64,9 @@ public void validate(final BackfillWorkflowDTO backfillWorkflowDTO) {
5964
if (backfillWorkflowDTO.getWorkflowDefinition().getReleaseState() != ReleaseState.ONLINE) {
6065
throw new IllegalStateException("The workflowDefinition should be online");
6166
}
67+
6268
tenantExistValidator.validate(backfillWorkflowDTO.getTenantCode());
69+
70+
startParamListValidator.validate(backfillWorkflowDTO.getStartParamList());
6371
}
6472
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowDTOValidator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.dolphinscheduler.api.validator.workflow;
1919

2020
import org.apache.dolphinscheduler.api.validator.IValidator;
21+
import org.apache.dolphinscheduler.api.validator.StartParamListValidator;
2122
import org.apache.dolphinscheduler.api.validator.TenantExistValidator;
2223
import org.apache.dolphinscheduler.common.enums.CommandType;
2324
import org.apache.dolphinscheduler.common.enums.ReleaseState;
@@ -32,8 +33,12 @@ public class TriggerWorkflowDTOValidator implements IValidator<TriggerWorkflowDT
3233

3334
private final TenantExistValidator tenantExistValidator;
3435

35-
public TriggerWorkflowDTOValidator(TenantExistValidator tenantExistValidator) {
36+
private final StartParamListValidator startParamListValidator;
37+
38+
public TriggerWorkflowDTOValidator(TenantExistValidator tenantExistValidator,
39+
StartParamListValidator startParamListValidator) {
3640
this.tenantExistValidator = tenantExistValidator;
41+
this.startParamListValidator = startParamListValidator;
3742
}
3843

3944
@Override
@@ -47,6 +52,9 @@ public void validate(final TriggerWorkflowDTO triggerWorkflowDTO) {
4752
if (triggerWorkflowDTO.getWorkflowDefinition().getReleaseState() != ReleaseState.ONLINE) {
4853
throw new IllegalStateException("The workflowDefinition should be online");
4954
}
55+
5056
tenantExistValidator.validate(triggerWorkflowDTO.getTenantCode());
57+
58+
startParamListValidator.validate(triggerWorkflowDTO.getStartParamList());
5159
}
5260
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowRequestTransformer.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,13 @@
2323
import org.apache.dolphinscheduler.api.validator.ITransformer;
2424
import org.apache.dolphinscheduler.dao.entity.WorkflowDefinition;
2525
import org.apache.dolphinscheduler.dao.repository.WorkflowDefinitionDao;
26-
import org.apache.dolphinscheduler.plugin.task.api.enums.Direct;
27-
import org.apache.dolphinscheduler.plugin.task.api.model.Property;
2826
import org.apache.dolphinscheduler.plugin.task.api.utils.PropertyUtils;
2927

30-
import org.apache.commons.collections.CollectionUtils;
31-
32-
import java.util.HashSet;
33-
import java.util.List;
34-
import java.util.Set;
35-
3628
import lombok.extern.slf4j.Slf4j;
3729

3830
import org.springframework.beans.factory.annotation.Autowired;
3931
import org.springframework.stereotype.Component;
4032

41-
import com.alibaba.druid.util.StringUtils;
42-
4333
@Slf4j
4434
@Component
4535
public class TriggerWorkflowRequestTransformer implements ITransformer<WorkflowTriggerRequest, TriggerWorkflowDTO> {
@@ -49,11 +39,6 @@ public class TriggerWorkflowRequestTransformer implements ITransformer<WorkflowT
4939

5040
@Override
5141
public TriggerWorkflowDTO transform(WorkflowTriggerRequest workflowTriggerRequest) {
52-
List<Property> startParamList =
53-
PropertyUtils.startParamsTransformPropertyList(workflowTriggerRequest.getStartParamList());
54-
55-
validateStartParamList(startParamList);
56-
5742
TriggerWorkflowDTO triggerWorkflowDTO = TriggerWorkflowDTO.builder()
5843
.loginUser(workflowTriggerRequest.getLoginUser())
5944
.startNodes(WorkflowUtils.parseStartNodeList(workflowTriggerRequest.getStartNodes()))
@@ -66,7 +51,8 @@ public TriggerWorkflowDTO transform(WorkflowTriggerRequest workflowTriggerReques
6651
.workerGroup(workflowTriggerRequest.getWorkerGroup())
6752
.tenantCode(workflowTriggerRequest.getTenantCode())
6853
.environmentCode(workflowTriggerRequest.getEnvironmentCode())
69-
.startParamList(startParamList)
54+
.startParamList(
55+
PropertyUtils.startParamsTransformPropertyList(workflowTriggerRequest.getStartParamList()))
7056
.dryRun(workflowTriggerRequest.getDryRun())
7157
.build();
7258

@@ -78,27 +64,4 @@ public TriggerWorkflowDTO transform(WorkflowTriggerRequest workflowTriggerReques
7864
triggerWorkflowDTO.setWorkflowDefinition(workflowDefinition);
7965
return triggerWorkflowDTO;
8066
}
81-
82-
private void validateStartParamList(List<Property> startParamList) {
83-
if (CollectionUtils.isEmpty(startParamList)) {
84-
return;
85-
}
86-
87-
Set<String> keys = new HashSet<>();
88-
for (Property param : startParamList) {
89-
if (StringUtils.isEmpty(param.getProp())) {
90-
throw new ServiceException("Parameter key cannot be empty");
91-
}
92-
93-
String key = param.getProp().trim();
94-
if (keys.contains(key)) {
95-
throw new ServiceException("Duplicate parameter key: " + key);
96-
}
97-
keys.add(key);
98-
99-
if (Direct.IN.equals(param.getDirect()) && StringUtils.isEmpty(param.getValue())) {
100-
throw new ServiceException("IN parameter value cannot be empty for key: " + key);
101-
}
102-
}
103-
}
10467
}

0 commit comments

Comments
 (0)