MCPcopy Create free account
hub / github.com/Keats/validator / validate

Method validate

validator_derive_tests/tests/nested.rs:871–898  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

869 // validations are expected to take priority over nested struct validations
870 #[allow(unused_mut)]
871 fn validate(&self) -> Result<(), ValidationErrors> {
872 // First validate the nested vector of structs:
873 let mut result = Ok(());
874 if !ValidationErrors::has_error(&result, "child") {
875 let results: Vec<_> = self
876 .child
877 .iter()
878 .map(|child| {
879 let mut result = Ok(());
880 result = ValidationErrors::merge(result, "child", child.validate());
881 result
882 })
883 .collect();
884 result = ValidationErrors::merge_all(result, "child", results);
885 }
886
887 // Then validate the length of the vector itself:
888 if !self.child.validate_length(Some(2u64), None, None) {
889 let mut err = ValidationError::new("length");
890 err.add_param(Cow::from("min"), &2u64);
891 err.add_param(Cow::from("value"), &&self.child);
892 result = result.and_then(|_| Err(ValidationErrors::new())).map_err(|mut errors| {
893 errors.add("child", err);
894 errors
895 });
896 }
897 result
898 }
899 }
900
901 let instance = ParentWithStructValidationsFirst { child: vec![Child { value: String::new() }] };

Calls 3

validate_lengthMethod · 0.80
add_paramMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected