public override JsonSchema AsJsonSchema()
        {
            JsonSchema returnSchema = InitialJsonSchema()
                                      .Type(JsonSchemaType.String);

            if (string.IsNullOrWhiteSpace(Format))
            {
                returnSchema.Format(Format);
            }

            if (MinLength > 0)
            {
                returnSchema.MinLength(MinLength);
            }

            if (MaxLength.HasValue)
            {
                returnSchema.MaxLength(MaxLength.Value);
            }

            if (!string.IsNullOrWhiteSpace(Pattern))
            {
                returnSchema.Pattern(Pattern);
            }

            if (!string.IsNullOrWhiteSpace(Format))
            {
                returnSchema.Format(Format);
            }

            return(returnSchema);
        }
Example #2
0
        private JsonObject ExtractRestrictions(string typeName, JsonSchema jSchema)
        {
            var restriction = new JsonObject();

            if (typeName == null)
            {
                return(restriction);
            }

            string reference;

            do
            {
                reference = jSchema.Ref();
                if (reference != null)
                {
                    jSchema = definitions.GetValueOrDefault(ExtractTypeNameFromDefinitionReference(reference));
                }
            }while (reference != null);

            switch (typeName)
            {
            case "string":
            {
                double?minLength = jSchema.MinLength();
                double?maxLength = jSchema.MaxLength();
                if (minLength != null && minLength == maxLength)
                {
                    AddRestrictionValue(restriction, "length", minLength);
                }
                else
                {
                    AddRestrictionValue(restriction, "minLength", minLength);
                    AddRestrictionValue(restriction, "maxLength", maxLength);
                }

                Regex pattern = jSchema.Pattern();
                if (pattern != null)
                {
                    var pat = new JsonObject();
                    pat.Add("Value", pattern.ToString());

                    restriction.Add("pattern", pat);
                }

                // enum restriction?
                List <JsonValue> enumerations = jSchema.Enum();
                if (enumerations != null && enumerations.Count > 0)
                {
                    string value = string.Empty;
                    foreach (JsonValue enumeration in enumerations)
                    {
                        if (value.Length > 0)
                        {
                            value += ";";
                        }

                        value += enumeration.String;
                    }

                    JsonObject enumerationObject = new JsonObject();
                    enumerationObject.Add("Value", value);

                    restriction.Add("enumeration", enumerationObject);
                }

                break;
            }

            case "integer":
            case "decimal":
            case "positiveInteger":
            case "number":
            {
                int totalDigits = 0;
                if (jSchema.Maximum() != null && jSchema.Minimum() == -jSchema.Maximum())
                {
                    string maxAsString = jSchema.Maximum().ToString();
                    if (maxAsString.Length > 0 && maxAsString.Replace("9", string.Empty).Length == 0)
                    {
                        totalDigits = maxAsString.Length;
                    }
                }

                if (totalDigits != 0)
                {
                    AddRestrictionValue(restriction, "totalDigits", totalDigits);
                }
                else
                {
                    AddRestrictionValue(restriction, "minimum", jSchema.Minimum());
                    AddRestrictionValue(restriction, "maximum", jSchema.Maximum());
                }

                AddRestrictionValue(restriction, "exclusiveMinimum", jSchema.ExclusiveMinimum());
                AddRestrictionValue(restriction, "exclusiveMaximum", jSchema.ExclusiveMaximum());

                break;
            }

            default:
            {
                break;
            }
            }

            return(restriction);
        }