private XmlSchemaSimpleTypeRestriction ExtractStringFacets(JsonSchema jSchema)
        {
            XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction
            {
                BaseTypeName = new XmlQualifiedName("string", XML_SCHEMA_NS),
            };

            EnumKeyword enumKeyword = jSchema.Get<EnumKeyword>();
            if (enumKeyword != null)
            {
                foreach (JsonValue enumValue in GetterExtensions.Enum(jSchema))
                {
                    XmlSchemaEnumerationFacet enumFacet = new XmlSchemaEnumerationFacet
                    {
                        Value = enumValue.String,
                    };
                    content.Facets.Add(enumFacet);
                }
            }

            MinLengthKeyword minLength = jSchema.Get<MinLengthKeyword>();
            MaxLengthKeyword maxLength = jSchema.Get<MaxLengthKeyword>();

            if (minLength != null && maxLength != null && minLength.Value == maxLength.Value)
            {
                // special rule that maps equal min and max lengths to xsd length facet
                XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet
                {
                    Value = minLength.Value.ToString(),
                };
                content.Facets.Add(lengthFacet);
            }
            else
            {
                if (minLength != null)
                {
                    XmlSchemaMinLengthFacet minLengthFacet = new XmlSchemaMinLengthFacet
                    {
                        Value = minLength.Value.ToString(),
                    };
                    content.Facets.Add(minLengthFacet);
                }

                if (maxLength != null)
                {
                    XmlSchemaMaxLengthFacet maxLengthFacet = new XmlSchemaMaxLengthFacet
                    {
                        Value = maxLength.Value.ToString(),
                    };
                    content.Facets.Add(maxLengthFacet);
                }
            }

            PatternKeyword pattern = jSchema.Get<PatternKeyword>();
            if (pattern != null)
            {
                XmlSchemaPatternFacet patternFacet = new XmlSchemaPatternFacet
                {
                    Value = pattern.Value.ToString(),
                };

                content.Facets.Add(patternFacet);
            }

            FormatKeyword format = jSchema.Get<FormatKeyword>();
            if (format != null && format.Value != null && !string.IsNullOrEmpty(format.Value.Key))
            {
                content.BaseTypeName = ExtractBaseTypeNameFromFormat(format.Value.Key);
            }

            return content;
        }
Exemple #2
0
 private static void KeywordEqual(PatternKeyword expected, PatternKeyword actual)
 {
     Assert.Equal(expected.Value.ToString(), actual.Value.ToString());
 }