dpp_domain/domain/validation/validator.rs
1//! Runtime-registered sector validator trait and registry — the extensibility
2//! seam for sectors not known to this crate at compile time.
3
4use crate::domain::field_error::FieldError;
5
6/// Trait for runtime-registered sector validators.
7///
8/// Register an implementation in [`SectorValidatorRegistry`] to provide JSON
9/// Schema + cross-field validation for sectors that are not known to this crate
10/// at compile time (e.g., plugin-defined sectors carrying `SectorData::Other`).
11pub trait SectorValidator: Send + Sync {
12 /// Validate the sector payload (the inner data, without the `"sector"` tag key).
13 fn validate(&self, data: &serde_json::Value) -> Result<(), Vec<FieldError>>;
14}
15
16/// Registry of runtime sector validators, keyed by catalog sector key.
17///
18/// An empty registry (the default) causes `SectorData::Other` to fail
19/// validation with an "unknown sector" error — silent pass-through is not safe.
20#[derive(Default)]
21pub struct SectorValidatorRegistry {
22 validators: std::collections::HashMap<String, std::sync::Arc<dyn SectorValidator>>,
23}
24
25impl SectorValidatorRegistry {
26 pub fn new() -> Self {
27 Self::default()
28 }
29
30 pub fn register(
31 &mut self,
32 key: impl Into<String>,
33 validator: std::sync::Arc<dyn SectorValidator>,
34 ) {
35 self.validators.insert(key.into(), validator);
36 }
37
38 pub(super) fn get(&self, key: &str) -> Option<&dyn SectorValidator> {
39 self.validators.get(key).map(std::sync::Arc::as_ref)
40 }
41}