Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets properties that was not created from schema.
        /// </summary>
        /// <param name="parserContext">Source parser context.</param>
        /// <param name="schema">Source schema.</param>
        /// <param name="recursive">Recursive search in children schemas.</param>
        /// <returns>Properties that was not created from schema.</returns>
        public static IEnumerable <IProperty> GetPropertiesNotFromSchema(this IXmlParserContext parserContext, ISchema?schema = null, bool recursive = true)
        {
            // Use root schema.
            schema ??= parserContext.Schema;

            IEnumerable <IProperty> properties = schema.GetProperties();

            foreach (IProperty property in properties)
            {
                if (property.GetIsNotFromSchema())
                {
                    yield return(property);
                }

                if (recursive)
                {
                    if (parserContext.GetSchema(property) is { } subSchema)
                    {
                        foreach (IProperty subProperty in parserContext.GetPropertiesNotFromSchema(subSchema, recursive))
                        {
                            yield return(subProperty);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets properties that was not created from schema for the property.
        /// </summary>
        /// <param name="parserContext">Source parser context.</param>
        /// <param name="property">Property to get schema..</param>
        /// <param name="recursive">Recursive search in children schemas.</param>
        /// <returns>Properties that was not created from schema.</returns>
        public static IEnumerable <IProperty> GetPropertiesNotFromSchema(this IXmlParserContext parserContext, IProperty property, bool recursive = true)
        {
            ISchema?schema = parserContext.GetSchema(property);

            if (schema == null)
            {
                return(Array.Empty <IProperty>());
            }

            return(parserContext.GetPropertiesNotFromSchema(schema, recursive: recursive));
        }