public void GivenInvalidCharactersException_WhenGetMessage_ShouldReturnExpected()
        {
            var name      = "tagname";
            var value     = "tagvalue";
            var vr        = DicomVR.DA;
            var exception = ElementValidationExceptionFactory.CreateInvalidCharactersException(name, vr, value);

            Assert.Equal($"Dicom element '{name}' with value '{value}' failed validation for VR '{vr}': Value contains invalid character.", exception.Message);
        }
        public static void Validate(string value, string name)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            ElementMaxLengthValidation.Validate(value, 64, name, DicomVR.LO);

            if (value.Contains('\\', StringComparison.OrdinalIgnoreCase) || ContainsControlExceptEsc(value))
            {
                throw ElementValidationExceptionFactory.CreateInvalidCharactersException(name, DicomVR.LO, value);
            }
        }
示例#3
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);
            }
        }