public void GivenPersonNameExceedMaxComponentsException_WhenGetMessage_ShouldReturnExpected() { var name = "tagname"; var value = "tagvalue"; var exception = ElementValidationExceptionFactory.CreatePersonNameExceedMaxComponentsException(name, value); Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR 'PN': Value contains more than 5 components.", exception.Message); }
public override void Validate(DicomElement dicomElement) { base.Validate(dicomElement); string value = dicomElement.Get <string>(); string name = dicomElement.Tag.GetFriendlyName(); DicomVR vr = dicomElement.ValueRepresentation; if (string.IsNullOrEmpty(value)) { // empty values allowed return; } var groups = value.Split('='); if (groups.Length > 3) { throw ElementValidationExceptionFactory.CreatePersonNameExceedMaxGroupsException(name, value); } foreach (var group in groups) { try { ElementMaxLengthValidation.Validate(group, 64, name, dicomElement.ValueRepresentation); } catch (ElementValidationException ex) when(ex.ErrorCode == ValidationErrorCode.ExceedMaxLength) { // Reprocess the exception to make more meaningful message throw ElementValidationExceptionFactory.CreatePersonNameGroupExceedMaxLengthException(name, value); } if (ContainsControlExceptEsc(group)) { throw ElementValidationExceptionFactory.CreateInvalidCharactersException(name, vr, value); } } var groupcomponents = groups.Select(group => group.Split('^').Length); if (groupcomponents.Any(l => l > 5)) { throw ElementValidationExceptionFactory.CreatePersonNameExceedMaxComponentsException(name, value); } }