Esempio n. 1
0
        /// <summary>
        /// Determines if the schema should be represented as a SimpleContentExtension in the XSD.
        /// </summary>
        /// <param name="schema">The Json Schema to analyze.</param>
        /// <returns>True if it should be represented as a SimpleContentExtension in the XSD; otherwise, false.</returns>
        protected bool IsValidSimpleContentExtension(JsonSchema schema)
        {
            // Exclude schemas with groupings
            if (schema.HasAnyOfKeywords(
                    typeof(AllOfKeyword),
                    typeof(OneOfKeyword),
                    typeof(AnyOfKeyword),
                    typeof(IfKeyword),
                    typeof(ThenKeyword),
                    typeof(ElseKeyword),
                    typeof(NotKeyword)))
            {
                return(false);
            }

            if (!schema.TryGetKeyword(out PropertiesKeyword properties))
            {
                return(false);
            }

            // One of the properties must be named value
            if (!properties.Properties.TryGetValue("value", out var valuePropertySchema))
            {
                return(false);
            }

            // It must not be marked as attribute
            if (valuePropertySchema.GetKeyword <XsdAttributeKeyword>()?.Value == true)
            {
                return(false);
            }

            // follow any $ref keywords to validate against the actual subschema
            while (valuePropertySchema.TryGetKeyword(out RefKeyword reference))
            {
                valuePropertySchema = FollowReference(reference);
            }

            // And it must be a valid SimpleType or a reference to a valid SimpleType
            if (!IsValidSimpleTypeOrSimpleTypeRestriction(valuePropertySchema))
            {
                return(false);
            }

            // All other properties must be attributes
            var attributePropertiesCount = properties.Properties.Values.Count(prop =>
            {
                var typeSchema = prop;

                // follow any $ref keywords to validate against the actual subschema
                while (typeSchema.TryGetKeyword(out RefKeyword reference))
                {
                    typeSchema = FollowReference(reference);
                }

                return(IsValidSimpleTypeOrSimpleTypeRestriction(typeSchema) &&
                       prop.HasKeyword <XsdAttributeKeyword>(kw => kw.Value));
            });

            if (attributePropertiesCount != (properties.Properties.Count - 1))
            {
                return(false);
            }

            return(true);
        }