/// <summary> /// Valida o XML de acordo com o schema. /// </summary> /// <param name="xml">A mensagem XML que deve ser verificada.</param> /// <param name="schema">O schema que será usado na verificação.</param> /// <returns>Se estiver tudo OK retorna null, caso contrário as mensagens de alertas e erros.</returns> protected RetornoWebservice ValidarSchema(string xml, string schema) { schema = Path.Combine(Config.Geral.PathSchemas, Name, schema); string[] errosSchema; string[] alertasSchema; if (XmlSchemaValidation.ValidarXml(xml, schema, out errosSchema, out alertasSchema)) { return(null); } var retLote = new RetornoWebservice { Assincrono = true, XmlEnvio = xml }; foreach (var erro in errosSchema.Select(descricao => new Evento { Codigo = "0", Descricao = descricao })) { retLote.Erros.Add(erro); } foreach (var alerta in alertasSchema.Select(descricao => new Evento { Codigo = "0", Descricao = descricao })) { retLote.Alertas.Add(alerta); } return(retLote); }
public void ValidateSchemaForDynamicObject() { //Arrange var source = new MemorySource(); dynamic n1 = new ExpandoObject(); n1.Xml = _validXml; source.DataAsList.Add(n1); dynamic n2 = new ExpandoObject(); n2.Xml = _validXml; source.DataAsList.Add(n2); dynamic n3 = new ExpandoObject(); n3.Xml = _invalidXml; source.DataAsList.Add(n3); MemoryDestination dest = new MemoryDestination(); MemoryDestination <ETLBoxError> error = new MemoryDestination <ETLBoxError>(); //Act XmlSchemaValidation schemaValidation = new XmlSchemaValidation(); schemaValidation.XmlSelector = row => { dynamic r = row as ExpandoObject; return(r.Xml); }; schemaValidation.XmlSchema = xsdMarkup; source.LinkTo(schemaValidation); schemaValidation.LinkTo(dest); schemaValidation.LinkErrorTo(error); source.Execute(); dest.Wait(); error.Wait(); //Assert Assert.True(dest.Data.Count == 2); Assert.True(error.Data.Count == 1); }
public void ValidateSchemaForArray() { //Arrange var source = new MemorySource <string[]>(); source.DataAsList.Add(new string[] { _validXml }); source.DataAsList.Add(new string[] { _invalidXml }); source.DataAsList.Add(new string[] { _validXml }); MemoryDestination <string[]> dest = new MemoryDestination <string[]>(); MemoryDestination <ETLBoxError> error = new MemoryDestination <ETLBoxError>(); //Act XmlSchemaValidation <string[]> schemaValidation = new XmlSchemaValidation <string[]>(); schemaValidation.XmlSelector = row => row[0]; schemaValidation.XmlSchema = xsdMarkup; source.LinkTo(schemaValidation); schemaValidation.LinkTo(dest); schemaValidation.LinkErrorTo(error); source.Execute(); dest.Wait(); error.Wait(); //Assert Assert.True(dest.Data.Count == 2); Assert.True(error.Data.Count == 1); }
/// <summary> /// Função para validar a menssagem a ser enviada para o webservice. /// </summary> /// <param name="xml"></param> /// <param name="schema"></param> protected virtual void ValidateMessage(string xml, TSchemas schema) { var schemaFile = Configuracoes.Arquivos.GetSchema(schema); XmlSchemaValidation.ValidarXml(xml, schemaFile, out var erros, out _); Guard.Against <ACBrDFeValidationException>(erros.Any(), "Erros de validação do xml." + $"{(Configuracoes.Geral.ExibirErroSchema ? Environment.NewLine + erros.AsString() : "")}"); }
/// <summary> /// Valida o XML de acordo com o schema. /// </summary> /// <param name="retorno"></param> /// <param name="schema">O schema que será usado na verificação.</param> /// <returns>Se estiver tudo OK retorna null, caso contrário as mensagens de alertas e erros.</returns> protected void ValidarSchema(RetornoWebservice retorno, string schema) { schema = Path.Combine(Configuracoes.Arquivos.PathSchemas, Name, schema); if (XmlSchemaValidation.ValidarXml(retorno.XmlEnvio, schema, out var errosSchema, out var alertasSchema)) { return; } foreach (var erro in errosSchema.Select(descricao => new Evento { Codigo = "0", Descricao = descricao })) { retorno.Erros.Add(erro); } foreach (var alerta in alertasSchema.Select(descricao => new Evento { Codigo = "0", Descricao = descricao })) { retorno.Alertas.Add(alerta); } }
public void ValidateSchemaForEachRow() { //Arrange var source = new MemorySource <MyXmlRow>(); source.DataAsList.Add(new MyXmlRow() { Xml = _validXml }); source.DataAsList.Add(new MyXmlRow() { Xml = _invalidXml }); source.DataAsList.Add(new MyXmlRow() { Xml = _validXml }); MemoryDestination <MyXmlRow> dest = new MemoryDestination <MyXmlRow>(); MemoryDestination <ETLBoxError> error = new MemoryDestination <ETLBoxError>(); //Act XmlSchemaValidation <MyXmlRow> schemaValidation = new XmlSchemaValidation <MyXmlRow>(); schemaValidation.XmlSelector = r => r.Xml; schemaValidation.XmlSchema = xsdMarkup; source.LinkTo(schemaValidation); schemaValidation.LinkTo(dest); schemaValidation.LinkErrorTo(error); source.Execute(); dest.Wait(); error.Wait(); //Assert Assert.True(dest.Data.Count == 2); Assert.True(error.Data.Count == 1); }
public void Validate_ValidSchema() { var xsdContent = @"<?xml version=""1.0"" encoding=""utf-8""?> <xs:schema id=""typepipe"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""targetNamespace"" elementFormDefault=""qualified"" attributeFormDefault=""unqualified""> <xs:element name=""tag""> <xs:complexType> <xs:attribute name=""attribute"" type=""xs:boolean"" use=""required"" /> </xs:complexType> </xs:element> </xs:schema>"; var validFragment = @"<tag xmlns=""targetNamespace"" attribute=""true"" />"; var invalidFragment = @"<tag xmlns=""targetNamespace"" attribute=""null"" />"; Assert.That(() => XmlSchemaValidation.Validate(validFragment, xsdContent), Throws.Nothing); Assert.That( () => XmlSchemaValidation.Validate(invalidFragment, xsdContent), Throws.Exception.With.Message.StartsWith("Validation of the xml fragment did not succeed for schema")); }
public void ErrorIntoCsv() { var source = new MemorySource <MyXmlRow>(); source.DataAsList.Add(new MyXmlRow() { Xml = _validXml }); source.DataAsList.Add(new MyXmlRow() { Xml = _invalidXml }); source.DataAsList.Add(new MyXmlRow() { Xml = _validXml }); XmlSchemaValidation <MyXmlRow> valid = new XmlSchemaValidation <MyXmlRow>(xsdMarkup, i => i.Xml); CsvDestination <ETLBoxError> errDest = new CsvDestination <ETLBoxError>("res/XmlValidation/Error.csv"); CsvDestination <MyXmlRow> dest = new CsvDestination <MyXmlRow>("res/XmlValidation/NoError.csv"); source.LinkTo(valid); valid.LinkTo(dest); valid.LinkErrorTo(errDest); source.Execute(); dest.Wait(); errDest.Wait(); Assert.Equal(File.ReadAllText("res/XmlValidation/ValidXml.csv"), File.ReadAllText("res/XmlValidation/NoError.csv"), ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); Assert.StartsWith(@"ErrorText,ReportTime,ExceptionType,RecordAsJson The element 'Root' has invalid child element 'Child3'. List of possible elements expected: 'Child2'.", File.ReadAllText("res/XmlValidation/Error.csv")); }
public void Validate_InvalidSchema() { var invalidSchema = @"<xs:invalid xmlns:xs=""http://www.w3.org/2001/XMLSchema"" />"; XmlSchemaValidation.Validate("does not matter", invalidSchema); }
/// <summary> /// Valida a CTe de acordo com o Schema. /// </summary> public void Validar() { var listaErros = new List <string>(); if (CTe.Any()) { var pathSchemaCTe = Parent.Configuracoes.Arquivos.GetSchema(SchemaCTe.CTe); foreach (var cte in CTe) { var xml = cte.GetXml(); XmlSchemaValidation.ValidarXml(xml, pathSchemaCTe, out var erros, out _); listaErros.AddRange(erros); if (cte.InfCTe.Ide.TpCTe == CTeTipo.Anulacao || cte.InfCTe.Ide.TpCTe == CTeTipo.Complemento) { continue; } var xmlModal = ((CTeNormal)cte.InfCTe.InfoCTe).InfModal.Modal.GetXml(DFeSaveOptions.DisableFormatting); SchemaCTe schema; switch (((CTeNormal)cte.InfCTe.InfoCTe).InfModal.Modal) { case CTeAereoModal _: schema = SchemaCTe.CTeModalAereo; break; case CTeAquavModal _: schema = SchemaCTe.CTeModalAquaviario; break; case CTeDutoModal _: schema = SchemaCTe.CTeModalDutoviario; break; case CTeFerrovModal _: schema = SchemaCTe.CTeModalFerroviario; break; case CTeMultimodal _: schema = SchemaCTe.CTeMultiModal; break; case CTeRodoModal _: schema = SchemaCTe.CTeModalRodoviario; break; default: continue; } var pathSchemaModal = Parent.Configuracoes.Arquivos.GetSchema(schema); XmlSchemaValidation.ValidarXml(xmlModal, pathSchemaModal, out var errosModal, out _); listaErros.AddRange(errosModal); } } if (CTeOS.Any()) { var pathSchemaCTeOS = Parent.Configuracoes.Arquivos.GetSchema(SchemaCTe.CTeOS); foreach (var cte in CTeOS) { var xml = cte.GetXml(); XmlSchemaValidation.ValidarXml(xml, pathSchemaCTeOS, out var erros, out _); listaErros.AddRange(erros); if (cte.InfCTe.Ide.TpCTe == CTeTipo.Anulacao || cte.InfCTe.Ide.TpCTe == CTeTipo.Complemento) { continue; } var xmlModal = ((CTeNormalOS)cte.InfCTe.InfoCTeOS).InfModal.Modal.GetXml(DFeSaveOptions.DisableFormatting); SchemaCTe schema; switch (((CTeNormalOS)cte.InfCTe.InfoCTeOS).InfModal.Modal) { case CTeRodoModalOS _: schema = SchemaCTe.CTeModalRodoviarioOS; break; default: continue; } var pathSchemaModal = Parent.Configuracoes.Arquivos.GetSchema(schema); XmlSchemaValidation.ValidarXml(xmlModal, pathSchemaModal, out var errosModal, out _); listaErros.AddRange(errosModal); } } Guard.Against <ACBrDFeValidationException>(listaErros.Any(), "Erros de validação do xml." + $"{(Parent.Configuracoes.Geral.ExibirErroSchema ? Environment.NewLine + listaErros.AsString() : "")}"); }