public void GivenPersonNameGroupExceedMaxLengthException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var exception = ElementValidationExceptionFactory.CreatePersonNameGroupExceedMaxLengthException(name, value);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR 'PN': One or more group of person name exceeds maxium length of 64.", exception.Message);
        }
Example #2
0
        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);
            }
        }