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
use std::{cell::RefCell, clone::Clone, rc::Rc}; use crate::model::validators::{Validator, ValidatorContext}; use crate::{HitError, ValidationError}; use super::unique_in_parent_plugin::{ UniqueInParentPlugin, UniqueInParentValueIndex, UniqueInParentValueIndexValue, }; static UNIQUE_IN_PARENT: &str = "UNIQUE_IN_PARENT"; pub struct UniqueInParentValidator { property_name: String, index: Rc<RefCell<UniqueInParentPlugin>>, value_index: Rc<RefCell<UniqueInParentValueIndex>>, } impl UniqueInParentValidator { pub fn new( property_name: String, index: Rc<RefCell<UniqueInParentPlugin>>, value_index: Rc<RefCell<UniqueInParentValueIndex>>, ) -> Box<UniqueInParentValidator> { Box::new(UniqueInParentValidator { property_name: property_name, index: index, value_index: value_index, }) } fn get_items( &self, context: &ValidatorContext, ) -> Result<Option<Vec<UniqueInParentValueIndexValue>>, HitError> { let value_index = self.value_index.borrow(); match context.index.get_parent(context.id) { Some(parent) => { let items = value_index.get(context.property, &parent.id, &parent.property); match items { Some(items) => Ok(Some(items.clone())), None => Ok(None), } } None => Ok(None), } } } impl Validator<String> for UniqueInParentValidator { fn validate( &self, value: &String, context: &ValidatorContext, ) -> Result<Option<Vec<ValidationError>>, HitError> { let items = self.get_items(context)?; match items { Some(items) => { for item in items.iter() { if item.id != context.id && item.value == Some(value.to_string()) { return Ok(Some(vec![ValidationError::warning( UNIQUE_IN_PARENT.into(), None, )])); } } } None => {} } Ok(None) } fn on_kernel_init(&mut self, field_name: &str, model_name: &str) -> Result<(), HitError> { self.index .borrow_mut() .property_names .get_or_insert(field_name.to_string()); self.index .borrow_mut() .model_names .get_or_insert(model_name.to_string()); Ok(()) } }