use crate::schema::types::SchemaEnforcementMode;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaEnforcementConfig {
pub mode: SchemaEnforcementMode,
pub validate_on_write: bool,
pub validate_on_read: bool,
pub auto_create_indexes: bool,
pub log_warnings: bool,
pub allow_unknown_properties: bool,
pub allow_schema_drift: bool,
}
impl Default for SchemaEnforcementConfig {
fn default() -> Self {
Self {
mode: SchemaEnforcementMode::Advisory,
validate_on_write: true,
validate_on_read: false,
auto_create_indexes: false,
log_warnings: true,
allow_unknown_properties: false,
allow_schema_drift: false,
}
}
}
impl SchemaEnforcementConfig {
#[allow(dead_code)] pub fn strict() -> Self {
Self {
mode: SchemaEnforcementMode::Strict,
validate_on_write: true,
validate_on_read: true,
auto_create_indexes: true,
log_warnings: true,
allow_unknown_properties: false,
allow_schema_drift: false,
}
}
#[allow(dead_code)] pub fn advisory() -> Self {
Self {
mode: SchemaEnforcementMode::Advisory,
validate_on_write: true,
validate_on_read: false,
auto_create_indexes: false,
log_warnings: true,
allow_unknown_properties: true,
allow_schema_drift: true,
}
}
#[allow(dead_code)] pub fn disabled() -> Self {
Self {
mode: SchemaEnforcementMode::Disabled,
validate_on_write: false,
validate_on_read: false,
auto_create_indexes: false,
log_warnings: false,
allow_unknown_properties: true,
allow_schema_drift: true,
}
}
#[allow(dead_code)] pub fn should_validate_write(&self) -> bool {
self.mode != SchemaEnforcementMode::Disabled && self.validate_on_write
}
#[allow(dead_code)] pub fn should_validate_read(&self) -> bool {
self.mode != SchemaEnforcementMode::Disabled && self.validate_on_read
}
#[allow(dead_code)] pub fn should_block_on_error(&self) -> bool {
self.mode == SchemaEnforcementMode::Strict
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_strict_config() {
let config = SchemaEnforcementConfig::strict();
assert_eq!(config.mode, SchemaEnforcementMode::Strict);
assert!(config.validate_on_write);
assert!(config.validate_on_read);
assert!(config.should_block_on_error());
}
#[test]
fn test_advisory_config() {
let config = SchemaEnforcementConfig::advisory();
assert_eq!(config.mode, SchemaEnforcementMode::Advisory);
assert!(config.validate_on_write);
assert!(!config.validate_on_read);
assert!(!config.should_block_on_error());
}
#[test]
fn test_disabled_config() {
let config = SchemaEnforcementConfig::disabled();
assert_eq!(config.mode, SchemaEnforcementMode::Disabled);
assert!(!config.should_validate_write());
assert!(!config.should_validate_read());
}
}