camel-component-validator 0.13.0

Validator component for rust-camel (XSD, JSON Schema, YAML)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use tower::Service;
use tracing::debug;

use crate::compiled::CompiledValidator;
use crate::config::ValidatorConfig;
use crate::xsd_bridge::{XsdBridge, XsdBridgeBackend};
use camel_component_api::ComponentContext;
use camel_component_api::{
    BoxProcessor, CamelError, Component, Consumer, Endpoint, Exchange, ProducerContext,
};

pub struct ValidatorComponent {
    xsd_bridge: Arc<dyn XsdBridge>,
    xsd_backend: Option<Arc<XsdBridgeBackend>>,
}

impl ValidatorComponent {
    pub fn new() -> Self {
        let xsd_backend = Arc::new(XsdBridgeBackend::new());
        Self {
            xsd_bridge: Arc::clone(&xsd_backend) as Arc<dyn XsdBridge>,
            xsd_backend: Some(xsd_backend),
        }
    }

    pub fn xsd_bridge_backend(&self) -> Option<Arc<XsdBridgeBackend>> {
        self.xsd_backend.as_ref().map(Arc::clone)
    }

    #[cfg(test)]
    fn with_xsd_bridge(xsd_bridge: Arc<dyn XsdBridge>) -> Self {
        Self {
            xsd_bridge,
            xsd_backend: None,
        }
    }
}

impl Default for ValidatorComponent {
    fn default() -> Self {
        Self::new()
    }
}

impl Component for ValidatorComponent {
    fn scheme(&self) -> &str {
        "validator"
    }

    fn create_endpoint(
        &self,
        uri: &str,
        _ctx: &dyn ComponentContext,
    ) -> Result<Box<dyn Endpoint>, CamelError> {
        let config = ValidatorConfig::from_uri(uri)?;
        let xsd_bridge = Arc::clone(&self.xsd_bridge);
        let compiled = CompiledValidator::compile(&config, xsd_bridge)?;
        Ok(Box::new(ValidatorEndpoint {
            uri: uri.to_string(),
            config,
            compiled: Arc::new(compiled),
        }))
    }
}

/// Validator endpoint that checks message bodies or headers against a schema.
///
/// Supports XML (XSD), JSON Schema, and YAML schema validation.
/// RelaxNG and Schematron are accepted in URI parsing but rejected at endpoint
/// creation with a clear error message.
struct ValidatorEndpoint {
    uri: String,
    config: ValidatorConfig,
    compiled: Arc<CompiledValidator>,
}

impl ValidatorEndpoint {
    /// Returns a human-readable description of the configured schema.
    #[allow(dead_code)]
    pub fn schema_info(&self) -> String {
        format!(
            "{:?} schema: {}",
            self.config.schema_type,
            self.config.schema_path.display()
        )
    }
}

impl Endpoint for ValidatorEndpoint {
    fn uri(&self) -> &str {
        &self.uri
    }

    fn create_consumer(&self) -> Result<Box<dyn Consumer>, CamelError> {
        Err(CamelError::EndpointCreationFailed(
            "validator endpoint does not support consumers".to_string(),
        ))
    }

    fn create_producer(&self, _ctx: &ProducerContext) -> Result<BoxProcessor, CamelError> {
        Ok(BoxProcessor::new(ValidatorProducer {
            uri: self.uri.clone(),
            config: self.config.clone(),
            compiled: Arc::clone(&self.compiled),
        }))
    }
}

#[derive(Clone)]
struct ValidatorProducer {
    uri: String,
    config: ValidatorConfig,
    compiled: Arc<CompiledValidator>,
}

impl Service<Exchange> for ValidatorProducer {
    type Response = Exchange;
    type Error = CamelError;
    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, exchange: Exchange) -> Self::Future {
        let compiled = Arc::clone(&self.compiled);
        let uri = self.uri.clone();
        let config = self.config.clone();
        Box::pin(async move {
            debug!(uri = uri, "validating exchange body");

            // VAL-003: header_name mode — validate header value instead of body
            if let Some(ref header_name) = config.header_name {
                match exchange.input.header(header_name) {
                    Some(value) => {
                        // Convert header Value to a string body for validation
                        let header_str = match value.as_str() {
                            Some(s) => s.to_string(),
                            None => value.to_string(),
                        };
                        let header_body = camel_component_api::Body::Text(header_str);
                        compiled.validate(&header_body).await?;
                    }
                    None => {
                        // VAL-004: failOnNullHeader
                        if config.fail_on_null_header {
                            return Err(CamelError::ProcessorError(format!(
                                "header '{header_name}' is missing and failOnNullHeader is true"
                            )));
                        }
                        // Pass through — no validation
                    }
                }
                return Ok(exchange);
            }

            // VAL-002: failOnNullBody
            if exchange.input.body.is_empty() {
                if config.fail_on_null_body {
                    return Err(CamelError::ProcessorError(
                        "body is empty and failOnNullBody is true".to_string(),
                    ));
                }
                // Pass through — no validation
                return Ok(exchange);
            }

            compiled.validate(&exchange.input.body).await?;
            Ok(exchange)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::ValidatorError;
    use async_trait::async_trait;
    use camel_component_api::{Message, NoOpComponentContext};
    use std::io::Write;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use tower::ServiceExt;

    fn json_schema_file() -> tempfile::NamedTempFile {
        let mut f = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        f.write_all(br#"{"type":"object","required":["id"]}"#)
            .unwrap();
        f
    }

    fn xsd_file() -> tempfile::NamedTempFile {
        let mut f = tempfile::Builder::new().suffix(".xsd").tempfile().unwrap();
        f.write_all(
            br#"<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="order" type="xs:string"/></xs:schema>"#,
        ).unwrap();
        f
    }

    #[derive(Debug)]
    struct MockXsdBridge {
        register_calls: AtomicUsize,
        validate_calls: AtomicUsize,
        register_error: Option<ValidatorError>,
        validate_error: Option<ValidatorError>,
    }

    #[async_trait]
    impl XsdBridge for MockXsdBridge {
        async fn register(&self, _xsd_bytes: Vec<u8>) -> Result<String, ValidatorError> {
            self.register_calls.fetch_add(1, Ordering::SeqCst);
            if let Some(err) = &self.register_error {
                return Err(err.clone());
            }
            Ok("xsd-mock-id".to_string())
        }

        async fn validate(
            &self,
            _schema_id: &str,
            _doc_bytes: Vec<u8>,
        ) -> Result<(), ValidatorError> {
            self.validate_calls.fetch_add(1, Ordering::SeqCst);
            if let Some(err) = &self.validate_error {
                return Err(err.clone());
            }
            Ok(())
        }
    }

    #[test]
    fn scheme_is_validator() {
        assert_eq!(ValidatorComponent::new().scheme(), "validator");
    }

    #[test]
    fn consumer_not_supported() {
        let f = json_schema_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        assert!(ep.create_consumer().is_err());
    }

    #[test]
    fn endpoint_creation_fails_for_missing_schema() {
        let result = ValidatorComponent::new()
            .create_endpoint("validator:/nonexistent/schema.json", &NoOpComponentContext);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn valid_json_body_passes_through() {
        let f = json_schema_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Json(
            serde_json::json!({"id": "1"}),
        )));
        let result = producer.oneshot(exchange).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn invalid_json_body_returns_err() {
        let f = json_schema_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Json(
            serde_json::json!({"name": "x"}),
        )));
        let result = producer.oneshot(exchange).await;
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("validation failed"), "got: {msg}");
    }

    #[tokio::test]
    async fn valid_xml_body_passes() {
        let backend = Arc::new(MockXsdBridge {
            register_calls: AtomicUsize::new(0),
            validate_calls: AtomicUsize::new(0),
            register_error: None,
            validate_error: None,
        });
        let f = xsd_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::with_xsd_bridge(backend)
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Xml(
            "<order>hello</order>".to_string(),
        )));
        assert!(producer.oneshot(exchange).await.is_ok());
    }

    #[tokio::test]
    async fn xsd_bridge_register_and_validate_mock() {
        let backend = Arc::new(MockXsdBridge {
            register_calls: AtomicUsize::new(0),
            validate_calls: AtomicUsize::new(0),
            register_error: None,
            validate_error: None,
        });

        let f = xsd_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::with_xsd_bridge(Arc::clone(&backend) as Arc<dyn XsdBridge>)
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();

        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Xml(
            "<order>ok</order>".to_string(),
        )));
        assert!(producer.oneshot(exchange).await.is_ok());
        assert_eq!(backend.register_calls.load(Ordering::SeqCst), 1);
        assert_eq!(backend.validate_calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn xsd_bridge_register_error_propagates_on_validate() {
        let backend = Arc::new(MockXsdBridge {
            register_calls: AtomicUsize::new(0),
            validate_calls: AtomicUsize::new(0),
            register_error: Some(ValidatorError::CompilationFailed {
                message: "COMPILATION_FAILED".to_string(),
                source: None,
            }),
            validate_error: None,
        });
        let f = xsd_file();
        let uri = format!("validator:{}", f.path().display());
        // Endpoint creation now always succeeds for XSD (registration is deferred).
        let ep = ValidatorComponent::with_xsd_bridge(backend)
            .create_endpoint(&uri, &NoOpComponentContext)
            .expect("endpoint creation should succeed");
        // The error surfaces when the first message is processed (register is called).
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Xml(
            "<order/>".to_string(),
        )));
        let err = producer
            .oneshot(exchange)
            .await
            .expect_err("expected validate to fail due to registration error");
        assert!(err.to_string().contains("COMPILATION_FAILED"));
    }

    #[tokio::test]
    async fn test_validator_rejects_oversized_payload() {
        // Build validator with maxPayloadBytes=100
        let f = json_schema_file();
        let uri = format!("validator:{}?maxPayloadBytes=100", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        // Send a body that is definitely > 100 bytes
        let big_body: String = "x".repeat(200);
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Text(big_body)));
        let result = producer.oneshot(exchange).await;
        assert!(result.is_err(), "expected oversized payload to be rejected");
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("payload too large"),
            "expected 'payload too large' in error, got: {msg}"
        );
    }

    #[tokio::test]
    async fn test_validator_allows_payload_under_limit() {
        let f = json_schema_file();
        let uri = format!("validator:{}?maxPayloadBytes=1024", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Json(
            serde_json::json!({"id": "1"}),
        )));
        let result = producer.oneshot(exchange).await;
        assert!(result.is_ok(), "expected valid payload under limit to pass");
    }

    #[tokio::test]
    async fn test_validator_no_limit_allows_any_size() {
        let f = json_schema_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        // Valid JSON that would exceed a 10-byte limit
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Json(
            serde_json::json!({"id": "this is a longer value that would exceed small limits"}),
        )));
        let result = producer.oneshot(exchange).await;
        assert!(
            result.is_ok(),
            "expected no-limit validator to pass any valid payload"
        );
    }

    #[tokio::test]
    async fn test_fail_on_null_body_default_rejects_empty() {
        let f = json_schema_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Empty));
        let result = producer.oneshot(exchange).await;
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("failOnNullBody"),
            "expected failOnNullBody in error, got: {msg}"
        );
    }

    #[tokio::test]
    async fn test_fail_on_null_body_false_passes_empty() {
        let f = json_schema_file();
        let uri = format!("validator:{}?failOnNullBody=false", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Empty));
        let result = producer.oneshot(exchange).await;
        assert!(
            result.is_ok(),
            "expected empty body to pass with failOnNullBody=false"
        );
    }

    #[tokio::test]
    async fn test_header_name_validation_uses_header_value() {
        let f = json_schema_file();
        let uri = format!("validator:{}?headerName=X-Data", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let mut msg = Message::new(camel_component_api::Body::Empty);
        msg.set_header("X-Data", serde_json::json!({"id": "1"}).to_string());
        let exchange = Exchange::new(msg);
        let result = producer.oneshot(exchange).await;
        // Header value {"id":"1"} is valid JSON matching schema
        assert!(
            result.is_ok(),
            "expected valid header to pass: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn test_header_name_missing_header_fails_by_default() {
        let f = json_schema_file();
        let uri = format!("validator:{}?headerName=X-Missing", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Empty));
        let result = producer.oneshot(exchange).await;
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("X-Missing"),
            "expected header name in error, got: {msg}"
        );
    }

    #[tokio::test]
    async fn test_fail_on_null_header_false_passes_missing_header() {
        let f = json_schema_file();
        let uri = format!(
            "validator:{}?headerName=X-Missing&failOnNullHeader=false",
            f.path().display()
        );
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        let producer = ep.create_producer(&ProducerContext::new()).unwrap();
        let exchange = Exchange::new(Message::new(camel_component_api::Body::Empty));
        let result = producer.oneshot(exchange).await;
        assert!(
            result.is_ok(),
            "expected missing header to pass with failOnNullHeader=false"
        );
    }

    #[test]
    fn test_relaxng_schema_type_rejected_at_creation() {
        let mut f = tempfile::Builder::new().suffix(".rng").tempfile().unwrap();
        use std::io::Write;
        f.write_all(b"<grammar/>").unwrap();
        let uri = format!("validator:{}", f.path().display());
        let result = ValidatorComponent::new().create_endpoint(&uri, &NoOpComponentContext);
        let err = result.err().expect("expected endpoint creation to fail");
        let msg = err.to_string();
        assert!(
            msg.contains("not yet supported"),
            "expected 'not yet supported' in error, got: {msg}"
        );
    }

    #[test]
    fn test_schematron_schema_type_rejected_at_creation() {
        let mut f = tempfile::Builder::new().suffix(".sch").tempfile().unwrap();
        use std::io::Write;
        f.write_all(b"<schema/>").unwrap();
        let uri = format!("validator:{}", f.path().display());
        let result = ValidatorComponent::new().create_endpoint(&uri, &NoOpComponentContext);
        let err = result.err().expect("expected endpoint creation to fail");
        let msg = err.to_string();
        assert!(
            msg.contains("not yet supported"),
            "expected 'not yet supported' in error, got: {msg}"
        );
    }

    #[test]
    fn test_schema_info_returns_description() {
        let f = json_schema_file();
        let uri = format!("validator:{}", f.path().display());
        let ep = ValidatorComponent::new()
            .create_endpoint(&uri, &NoOpComponentContext)
            .unwrap();
        // The endpoint is a Box<dyn Endpoint>, so we can't directly call schema_info().
        // We verify endpoint creation succeeds (schema compiled).
        assert_eq!(ep.uri(), uri);
    }
}