public void ReadInvalidXml() { string testXml = @" <Person> <FirstName>z</FirstName> <Address> <City>NY</City> <Unknown>1</Unknown> </Address> </Person>"; IObjectSchema personSchema = new PersonSchema().GetObjectSchema(); var container = XDocument.Parse(testXml).ParseXmlToContainer(personSchema); IXmlParserContext xmlParserContext = container.GetMetadata <IXmlParserContext>(); var validationRules = personSchema.Properties.GetValidationRules().ToArray(); var messages = container.Validate(validationRules).ToArray(); messages.Should().HaveCount(2); messages[0].FormattedMessage.Should().Be("FirstName length should be greater then 1 but was 1"); messages[1].FormattedMessage.Should().Be("LastName is marked as required but is not exists."); IObjectSchema addressSchemaStatic = personSchema.GetProperty("Address") !.GetSchema() !.ToObjectSchema(); addressSchemaStatic.Properties.Should().HaveCount(2); IObjectSchema addressSchemaReal = xmlParserContext.GetSchema(personSchema.GetProperty("Address")).ToObjectSchema(); addressSchemaReal.Properties.Should().HaveCount(3); IProperty[] notFromSchema = addressSchemaReal.GetPropertiesNotFromSchema().ToArray(); notFromSchema.Should().HaveCount(1); notFromSchema[0].Name.Should().Be("Unknown"); notFromSchema[0].Type.Should().Be(typeof(string)); }
public void ReadObjectWithListWithSchema() { string testXml = @" <Person> <FirstName>Alex</FirstName> <LastName>Smith</LastName> <Sex>Male</Sex> <Addresses> <Address> <City>NY</City> <Zip>111</Zip> </Address> <Address> <City>Moscow</City> <Zip>222</Zip> </Address> </Addresses> <Address> <City>NY</City> <Zip>333</Zip> </Address> </Person>"; IObjectSchema personSchema = new PersonSchema().GetObjectSchema(); IPropertyContainer?container = XDocument .Parse(testXml, LoadOptions.SetLineInfo) .ParseXmlToContainer(personSchema, new XmlParserSettings(validateOnParse: true)); container.Should().NotBeNull(); IPropertyValue[] values = container.Properties.ToArray(); values[0].PropertyUntyped.Name.Should().Be("FirstName"); values[0].PropertyUntyped.Type.Should().Be(typeof(string)); values[1].PropertyUntyped.Name.Should().Be("LastName"); values[1].PropertyUntyped.Type.Should().Be(typeof(string)); values[2].PropertyUntyped.Name.Should().Be("Sex"); values[2].PropertyUntyped.Type.Should().Be(typeof(Sex)); values[3].PropertyUntyped.Name.Should().Be("Addresses"); values[3].PropertyUntyped.Type.Should().Be(typeof(IPropertyContainer)); values[4].PropertyUntyped.Name.Should().Be("Address"); values[4].PropertyUntyped.Type.Should().Be(typeof(IPropertyContainer)); var addressSchema = container.GetSchema().GetProperty("Address").GetSchema().ToObjectSchema(); addressSchema.GetProperty("Zip").Type.Should().Be(typeof(int)); var address = (values[4].ValueUntyped as IPropertyContainer).Properties.ToArray(); address[0].PropertyUntyped.Name.Should().Be("City"); address[0].PropertyUntyped.Type.Should().Be(typeof(string)); address[1].PropertyUntyped.Name.Should().Be("Zip"); address[1].PropertyUntyped.Type.Should().Be(typeof(int)); address[1].ValueUntyped.Should().Be(333); }
public void ReflectionSchema() { PersonSchema personSchema = new PersonSchema(); var properties = personSchema.GetObjectSchema().GetProperties().ToArray(); properties.Should().HaveCount(5); AddressSchema addressSchema = new AddressSchema(); var city = addressSchema.GetObjectSchema().GetProperties().First(); city.Should().NotBeNull(); city.Name.Should().Be("City"); city.Type.Should().Be(typeof(string)); }
public void ReadXml() { string testXml = @" <Person> <FirstName>Alex</FirstName> <Address> <City>NY</City> </Address> <LastName>Smith</LastName> </Person>"; IObjectSchema personSchema = new PersonSchema().GetObjectSchema(); var container = XDocument.Parse(testXml).ParseXmlToContainer(personSchema); container.GetSchema().Should().NotBeNull(); }