Skip to main content

swf_core/validation/
enum_validators.rs

1use super::{ValidationResult, ValidationRule};
2use crate::models::authentication::{OAuth2ClientAuthenticationMethod, OAuth2RequestEncoding};
3use crate::models::call::AsyncApiProtocol;
4use crate::models::task::ContainerLifetimeDefinition;
5use crate::models::task::{
6    ContainerCleanupPolicy, ExtensionTarget, HttpMethod, HttpOutputFormat, OAuth2GrantType,
7    PullPolicy, ScriptLanguage,
8};
9
10/// Helper to validate a string value against a list of allowed enum values.
11pub(crate) fn validate_enum_value(
12    value: &str,
13    valid_values: &[&str],
14    field_name: &str,
15    prefix: &str,
16    skip_empty: bool,
17    case_insensitive: bool,
18    result: &mut ValidationResult,
19) {
20    if skip_empty && value.is_empty() {
21        return;
22    }
23    let is_valid = if case_insensitive {
24        valid_values.iter().any(|v| v.eq_ignore_ascii_case(value))
25    } else {
26        valid_values.contains(&value)
27    };
28    if !is_valid {
29        let valid_list = valid_values
30            .iter()
31            .map(|v| format!("'{}'", v))
32            .collect::<Vec<_>>()
33            .join(", ");
34        result.add_error(
35            &format!("{}.{}", prefix, field_name),
36            ValidationRule::InvalidValue,
37            &format!(
38                "{} must be one of {}, got '{}'",
39                field_name, valid_list, value
40            ),
41        );
42    }
43}
44
45/// Validates an AsyncAPI protocol value
46pub fn validate_asyncapi_protocol(protocol: &str, prefix: &str, result: &mut ValidationResult) {
47    validate_enum_value(
48        protocol,
49        AsyncApiProtocol::ALL_VALUES,
50        "protocol",
51        prefix,
52        true,
53        false,
54        result,
55    );
56}
57
58/// Validates an HTTP call output format value
59pub fn validate_http_output(output: &str, prefix: &str, result: &mut ValidationResult) {
60    validate_enum_value(
61        output,
62        HttpOutputFormat::ALL_VALUES,
63        "output",
64        prefix,
65        true,
66        false,
67        result,
68    );
69}
70
71/// Validates a container's pull policy value
72pub fn validate_pull_policy(pull_policy: &str, prefix: &str, result: &mut ValidationResult) {
73    validate_enum_value(
74        pull_policy,
75        PullPolicy::ALL_VALUES,
76        "pullPolicy",
77        prefix,
78        false,
79        false,
80        result,
81    );
82}
83
84/// Validates a container's cleanup policy value
85pub fn validate_container_cleanup(cleanup: &str, prefix: &str, result: &mut ValidationResult) {
86    validate_enum_value(
87        cleanup,
88        ContainerCleanupPolicy::ALL_VALUES,
89        "lifetime.cleanup",
90        prefix,
91        false,
92        false,
93        result,
94    );
95}
96
97/// Validates a script's language value
98pub fn validate_script_language(language: &str, prefix: &str, result: &mut ValidationResult) {
99    validate_enum_value(
100        language,
101        ScriptLanguage::ALL_VALUES,
102        "language",
103        prefix,
104        false,
105        false,
106        result,
107    );
108}
109
110/// Validates an OAuth2 client authentication method value
111pub fn validate_oauth2_client_auth_method(
112    method: &str,
113    prefix: &str,
114    result: &mut ValidationResult,
115) {
116    validate_enum_value(
117        method,
118        OAuth2ClientAuthenticationMethod::ALL_VALUES,
119        "client.authentication",
120        prefix,
121        true,
122        false,
123        result,
124    );
125}
126
127/// Validates an OAuth2 token request encoding value
128pub fn validate_oauth2_request_encoding(
129    encoding: &str,
130    prefix: &str,
131    result: &mut ValidationResult,
132) {
133    validate_enum_value(
134        encoding,
135        OAuth2RequestEncoding::ALL_VALUES,
136        "request.encoding",
137        prefix,
138        true,
139        false,
140        result,
141    );
142}
143
144/// Validates an OAuth2 grant type value
145pub fn validate_oauth2_grant_type(grant: &str, prefix: &str, result: &mut ValidationResult) {
146    validate_enum_value(
147        grant,
148        OAuth2GrantType::ALL_VALUES,
149        "grant",
150        prefix,
151        true,
152        false,
153        result,
154    );
155}
156
157/// Validates an extension's task type value
158pub fn validate_extension_task_type(extend: &str, prefix: &str, result: &mut ValidationResult) {
159    validate_enum_value(
160        extend,
161        ExtensionTarget::ALL_VALUES,
162        "extend",
163        prefix,
164        false,
165        false,
166        result,
167    );
168}
169
170/// Validates an HTTP method value (case-insensitive)
171pub fn validate_http_method(method: &str, prefix: &str, result: &mut ValidationResult) {
172    validate_enum_value(
173        method,
174        HttpMethod::ALL_VALUES,
175        "with.method",
176        prefix,
177        false,
178        true,
179        result,
180    );
181}
182
183/// Validates container lifetime: when cleanup is "eventually", the 'after' field is required
184pub fn validate_container_lifetime(
185    lifetime: &ContainerLifetimeDefinition,
186    prefix: &str,
187    result: &mut ValidationResult,
188) {
189    validate_container_cleanup(&lifetime.cleanup, prefix, result);
190    if lifetime.cleanup == "eventually" && lifetime.after.is_none() {
191        result.add_error(
192            &format!("{}.lifetime.after", prefix),
193            ValidationRule::Required,
194            "lifetime.after is required when cleanup is 'eventually'",
195        );
196    }
197}