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

Function test_can_validate_array_fields

validator_derive_tests/tests/nested.rs:402–468  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

400
401#[test]
402fn test_can_validate_array_fields() {
403 #[derive(Validate)]
404 struct ParentWithArrayOfChildren {
405 #[validate(length(min = 1))]
406 #[validate(nested)]
407 child: [Child; 4],
408 }
409
410 #[derive(Validate, Serialize)]
411 struct Child {
412 #[validate(length(min = 1))]
413 value: String,
414 }
415
416 let instance = ParentWithArrayOfChildren {
417 child: [
418 Child { value: "valid".to_string() },
419 Child { value: String::new() },
420 Child { value: "valid".to_string() },
421 Child { value: String::new() },
422 ],
423 };
424
425 let res = instance.validate();
426 assert!(res.is_err());
427 let err = res.unwrap_err();
428 let errs = err.errors();
429 assert_eq!(errs.len(), 1);
430 assert!(errs.contains_key("child"));
431 if let ValidationErrorsKind::List(ref errs) = errs["child"] {
432 assert!(errs.contains_key(&1));
433 unwrap_map(&errs[&1], |errs| {
434 assert_eq!(errs.len(), 1);
435 assert!(errs.contains_key("value"));
436 if let ValidationErrorsKind::Field(ref errs) = errs["value"] {
437 assert_eq!(errs.len(), 1);
438 assert_eq!(errs[0].code, "length");
439 } else {
440 panic!("Expected field validation errors");
441 }
442 });
443 assert!(errs.contains_key(&3));
444 unwrap_map(&errs[&3], |errs| {
445 assert_eq!(errs.len(), 1);
446 assert!(errs.contains_key("value"));
447 if let ValidationErrorsKind::Field(ref errs) = errs["value"] {
448 assert_eq!(errs.len(), 1);
449 assert_eq!(errs[0].code, "length");
450 } else {
451 panic!("Expected field validation errors");
452 }
453 });
454 } else {
455 panic!("Expected list validation errors");
456 }
457
458 // A valid struct should not fail
459 let instance = ParentWithArrayOfChildren {

Callers

nothing calls this directly

Calls 4

to_stringMethod · 0.80
errorsMethod · 0.80
unwrap_mapFunction · 0.70
validateMethod · 0.45

Tested by

no test coverage detected