protected JsonSchema InitialJsonSchema()
        {
            JsonSchema returnSchema = new JsonSchema()
                                      .Title(Name)
                                      .Description(Description)
                                      .Comment($"Generated with JsonSchemaBuilder");

            if (Examples != null)
            {
                List <JsonValue> examplesOut = new List <JsonValue>();
                foreach (T item in Examples)
                {
                    examplesOut.Add(TAsJsonValue(item));
                }
                returnSchema.Examples(examplesOut.ToArray());
            }
            if (Enums != null)
            {
                List <JsonValue> enumsOut = new List <JsonValue>();
                foreach (T item in Enums)
                {
                    enumsOut.Add(TAsJsonValue(item));
                }
                returnSchema.Enum(enumsOut.ToArray());
            }

            return(returnSchema);
        }
Esempio n. 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);
        }