コード例 #1
0
        public void when_validate_document_with_empty_link_then_validator_should_return_error()
        {
            document = Document.Factory.Create("test", "test", "", 1, 1, new object());
            var result = validator.Validate(document);

            Assert.AreEqual(result.IsValid, false);
        }
コード例 #2
0
        public void LeafElementValidateTest()
        {
            string runOuterXml = "<w:r xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +
                                 "<w:rPr><w:strike /><w:vanish><!-- comments is OK --></w:vanish><w:webHidden><w:invalidChild /></w:webHidden></w:rPr>" +
                                 "<w:t>Run Text.</w:t><w:t><!-- comments is ok -->Text 2</w:t><w:t>Text 3.<invalidElement /></w:t></w:r>";

            var target         = new DocumentValidator(new ValidationCache(FileFormatVersions.Office2007));
            var openxmlElement = new Run(runOuterXml);
            var result         = target.Validate(openxmlElement);

            Assert.Collection(
                result,
                error =>
            {
                Assert.Same(openxmlElement.LastChild, error.Node);
                Assert.Equal("Sch_InvalidChildinLeafElement", error.Id);
                Assert.Equal("The element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:t' is a leaf element and cannot contain children.", error.Description);
            },
                error =>
            {
                Assert.Same(openxmlElement.GetFirstChild <RunProperties>().WebHidden, error.Node);
                Assert.Equal("Sch_InvalidChildinLeafElement", error.Id);
                Assert.Equal("The element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:webHidden' is a leaf element and cannot contain children.", error.Description);
            });
        }
コード例 #3
0
        private IValidationResult Validate(string query, ISchema schema, IEnumerable <IValidationRule> rules)
        {
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(query);
            var validator       = new DocumentValidator();

            return(validator.Validate(query, schema, document, rules));
        }
コード例 #4
0
        private IValidationResult Validate(ValidationTestConfig config)
        {
            var userContext = new GraphQLUserContext {
                User = config.User
            };
            var documentBuilder = new GraphQLDocumentBuilder();
            var document        = documentBuilder.Build(config.Query);
            var validator       = new DocumentValidator();

            return(validator.Validate(config.Query, config.Schema, document, config.Rules, userContext, config.Inputs));
        }
コード例 #5
0
        public void DocumentValidator_ValidateSampleXml_ShouldWork()
        {
            var document = new TaxPubDocument();

            document.Xml = "<article><front></front></article>";

            var validator = new DocumentValidator();
            var reporter  = new LogReporter(new ConsoleLogger());

            validator.Validate(document, reporter).Wait();
        }
コード例 #6
0
        public int SaveDocumentToDb(Document document)
        {
            DocumentValidator validator = new DocumentValidator();
            var result     = validator.Validate(document);
            int documentId = 0;

            if (result.IsValid)
            {
                var documentToAdd = _documentMapper.Map(document);
                _nHibernateUniversalRepository.Create(documentToAdd);
                documentId = documentToAdd.Id;
            }
            return(documentId);
        }
コード例 #7
0
        public void Should_DocumentValidator_Name_NotEmpty()
        {
            results = validator.Validate(document);
            Assert.IsTrue(results.IsValid);

            document.Name = "";
            results       = validator.Validate(document);
            Assert.IsFalse(results.IsValid);
        }
コード例 #8
0
        /// <summary>
        /// Validate the DOM tree under the specified OpenXmlElement.
        /// </summary>
        /// <param name="schemaValidator">The schemaValidator.</param>
        /// <param name="openXmlElement">The root of the sub tree.</param>
        /// <returns>Returns the validation result in ValidationResult.</returns>
        /// <remarks>
        /// Only schema validating.
        /// </remarks>
        public static List <ValidationErrorInfo> Validate(this DocumentValidator schemaValidator, OpenXmlElement openXmlElement)
        {
            Debug.Assert(openXmlElement is not null);

            Debug.Assert(!(openXmlElement is OpenXmlUnknownElement || openXmlElement is OpenXmlMiscNode));

            // Can not just validate AlternateContent / Choice / Fallback
            Debug.Assert(!(openXmlElement is AlternateContentChoice || openXmlElement is AlternateContentFallback));

            var validationContext = new ValidationContext();

            validationContext.Stack.Push(element: openXmlElement);

            schemaValidator.Validate(validationContext);

            return(validationContext.Errors);
        }
コード例 #9
0
        private void EditButton_OnClick(object sender, RoutedEventArgs e)
        {
            var validator = new DocumentValidator();

            if (validator.Validate(Document))
            {
                var dao = new DocumentDao();
                dao.InsertUpdate(Document);
                Close();
            }
            else
            {
                MessageBox.Show(
                    this,
                    string.Format("Данные не прошли проверку.\nСообщение об ошибке: \"{0}\"", validator.Message),
                    "Сообщение");
            }
        }
コード例 #10
0
        internal IValidationResult Validate(string queryString)
        {
            var document = _documentBuilder.Build(queryString);

            return(_documentValidator.Validate(queryString, _schema, document));
        }
コード例 #11
0
        public void AcbSyntaxValidationTest()
        {
            var validator  = new DocumentValidator(new ValidationCache(FileFormatVersions.Office2007));
            var element    = new Run();
            var acFallback = new AlternateContentFallback();
            var ac         = element.AppendChild(new AlternateContent());
            var errors     = validator.Validate(ac);

            // Error case: must have one choice, can not have AlternateContent as ID
            Assert.Single(errors);
            Assert.Equal("Sch_IncompleteContentExpectingComplex", errors[0].Id);

            ac.AddNamespaceDeclaration("o15", "http://o15.com");

            // ac.NamespaceDeclarations
            ac.AppendChild(new AlternateContentChoice()
            {
                Requires = "o15"
            });
            errors = validator.Validate(ac);
            Assert.Empty(errors);

            ac.AddNamespaceDeclaration("o14", "http://o14.com");
            ac.PrependChild(new AlternateContentChoice()
            {
                Requires = "o14"
            });
            errors = validator.Validate(ac);
            Assert.Empty(errors);

            ac.AppendChild(acFallback);
            errors = validator.Validate(ac);
            Assert.Empty(errors);

            // Error case: should not contains AlternateContent directly as child.
            ac.AppendChild(new AlternateContent());
            errors = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", errors[0].Id);
            ac.RemoveChild(ac.LastChild);

            // Error case: can only contains one Fallback.
            ac.AppendChild(new AlternateContentFallback());
            errors = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", errors[0].Id);
            ac.RemoveChild(ac.LastChild);

            ac.RemoveChild(acFallback);

            // Error case: wrong sequence
            ac.PrependChild(acFallback);
            errors = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Equal("Sch_IncompleteContentExpectingComplex", errors[0].Id);
            ac.RemoveChild(acFallback);
            ac.Append(acFallback);

            var langAttribute = new OpenXmlAttribute("xml:lang", "http://www.w3.org/XML/1998/namespace", "en-us");

            ac.SetAttribute(langAttribute);
            errors = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Equal("MC_InvalidXmlAttribute", errors[0].Id);
            Assert.Equal("The AlternateContent element should not have an xml:lang or xml:space attribute.", errors[0].Description);
            ac.RemoveAttribute(langAttribute.LocalName, langAttribute.NamespaceUri);

            ac.FirstChild.SetAttribute(langAttribute);
            errors = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Same(ac.FirstChild, errors[0].Node);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Equal("MC_InvalidXmlAttribute", errors[0].Id);
            Assert.Equal("The Choice element should not have an xml:lang or xml:space attribute.", errors[0].Description);
            ac.FirstChild.RemoveAttribute(langAttribute.LocalName, langAttribute.NamespaceUri);

            ac.LastChild.SetAttribute(langAttribute);
            errors = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Same(ac.LastChild, errors[0].Node);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Equal("MC_InvalidXmlAttribute", errors[0].Id);
            Assert.Equal("The Fallback element should not have an xml:lang or xml:space attribute.", errors[0].Description);
            ac.LastChild.RemoveAttribute(langAttribute.LocalName, langAttribute.NamespaceUri);

            AlternateContentChoice choice1 = ac.FirstChild as AlternateContentChoice;

            choice1.Requires = "o17 o15";
            errors           = validator.Validate(ac);
            Assert.Single(errors);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Same(choice1, errors[0].Node);
            Assert.Equal("MC_InvalidRequiresAttribute", errors[0].Id);
            Assert.Equal("The Requires attribute is invalid - The value 'o17 o15' contains an invalid prefix that is not defined.", errors[0].Description);

            choice1.Requires = null;
            errors           = validator.Validate(ac);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, errors[0].ErrorType);
            Assert.Single(errors);
            Assert.Same(choice1, errors[0].Node);
            Assert.Equal("MC_MissedRequiresAttribute", errors[0].Id);
            Assert.Equal("All Choice elements must have a Requires attribute whose value contains a whitespace delimited list of namespace prefixes.", errors[0].Description);
        }
コード例 #12
0
        public void CompatibilityRuleAttributesValidationTest()
        {
            var validator = new DocumentValidator(new ValidationCache(FileFormatVersions.Office2007));
            var element   = new Paragraph();
            var run       = new Run();

            element.AppendChild(run);

            var result = validator.Validate(element);

            Assert.Empty(result);

            element.AddNamespaceDeclaration("o15", "http://o15.com");
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes           = new MarkupCompatibilityAttributes();
            run.MCAttributes.Ignorable = "o15";
            result = validator.Validate(element);
            Assert.Empty(result);

            run.AddNamespaceDeclaration("w15", "http://w15.com");
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.Ignorable = "o15 w15";
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.PreserveAttributes = "  o15:id w15:*";
            run.MCAttributes.PreserveElements   = "o15:*  w15:*  ";
            run.MCAttributes.ProcessContent     = "  o15:newE   w15:newW  ";
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.PreserveElements = "x15:* ";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result[0].Id);

            run.MCAttributes.Ignorable          = null;
            run.MCAttributes.PreserveAttributes = null;
            run.MCAttributes.PreserveElements   = "w15:*";
            run.MCAttributes.ProcessContent     = string.Empty;
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result[0].Id);

            run.MCAttributes.Ignorable          = "o15";
            run.MCAttributes.PreserveAttributes = string.Empty;
            run.MCAttributes.PreserveElements   = "w15:*";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result[0].Id);
            run.MCAttributes.PreserveElements = null;

            run.MCAttributes.ProcessContent = "w14:newW";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidProcessContentAttribute", result[0].Id);
            run.MCAttributes.ProcessContent = null;

            var spaceAttribute = new OpenXmlAttribute("xml:space", "http://www.w3.org/XML/1998/namespace", "preserve");

            run.MCAttributes.ProcessContent = "o15:newP";
            run.SetAttribute(spaceAttribute);
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidXmlAttributeWithProcessContent", result[0].Id);
            run.MCAttributes.ProcessContent = null;

            run.SetAttribute(new OpenXmlAttribute("o15:id", "http://o15.com", "1"));
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.Ignorable = "o15 w15 x15";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidIgnorableAttribute", result[0].Id);

            run.MCAttributes.Ignorable = "o15 w15";
            run.SetAttribute(new OpenXmlAttribute("x15:id", "http://x15.com", "1"));
            result = validator.Validate(element);
            Assert.Single(result);
        }
コード例 #13
0
        private void Validate_ValidData_ValidationPasses(string contentType)
        {
            var document = new Document(Stream.Null, contentType, "name");

            Assert.True(_validator.Validate(document).IsValid);
        }