public JsonSchemaProperty?Visit(IArrayField field) { var item = Builder.Object(); foreach (var nestedField in field.Fields.ForApi(withHiddenFields)) { var childProperty = nestedField.Accept(this); if (childProperty != null) { childProperty.Description = nestedField.RawProperties.Hints; childProperty.IsRequired = nestedField.RawProperties.IsRequired; item.Properties.Add(nestedField.Name, childProperty); } } return(Builder.ArrayProperty(item)); }
public static JsonSchema BuildJsonSchema(this Schema schema, PartitionResolver partitionResolver, SchemaResolver schemaResolver, bool withHidden = false) { Guard.NotNull(schemaResolver); Guard.NotNull(partitionResolver); var schemaName = schema.Name.ToPascalCase(); var jsonTypeVisitor = new JsonTypeVisitor(schemaResolver, withHidden); var jsonSchema = Builder.Object(); foreach (var field in schema.Fields.ForApi(withHidden)) { var partitionObject = Builder.Object(); var partitioning = partitionResolver(field.Partitioning); foreach (var partitionKey in partitioning.AllKeys) { var partitionItemProperty = field.Accept(jsonTypeVisitor); if (partitionItemProperty != null) { var isOptional = partitioning.IsOptional(partitionKey); var name = partitioning.GetName(partitionKey); partitionItemProperty.Description = name; partitionItemProperty.IsRequired = field.RawProperties.IsRequired && !isOptional; partitionObject.Properties.Add(partitionKey, partitionItemProperty); } } if (partitionObject.Properties.Count > 0) { var propertyReference = schemaResolver($"{schemaName}{field.Name.ToPascalCase()}Property", partitionObject); jsonSchema.Properties.Add(field.Name, CreateProperty(field, propertyReference)); } } return(jsonSchema); }
public static JsonSchema4 BuildJsonSchema(this Schema schema, PartitionResolver partitionResolver, Func <string, JsonSchema4, JsonSchema4> schemaResolver) { Guard.NotNull(schemaResolver, nameof(schemaResolver)); Guard.NotNull(partitionResolver, nameof(partitionResolver)); var schemaName = schema.Name.ToPascalCase(); var jsonTypeVisitor = new JsonTypeVisitor(schemaResolver); var jsonSchema = new JsonSchema4 { Type = JsonObjectType.Object }; foreach (var field in schema.Fields.ForApi()) { var partitionObject = Builder.Object(); var partition = partitionResolver(field.Partitioning); foreach (var partitionItem in partition) { var partitionItemProperty = field.Accept(jsonTypeVisitor); if (partitionItemProperty != null) { partitionItemProperty.Description = partitionItem.Name; partitionItemProperty.IsRequired = field.RawProperties.IsRequired && !partitionItem.IsOptional; partitionObject.Properties.Add(partitionItem.Key, partitionItemProperty); } } if (partitionObject.Properties.Count > 0) { var propertyReference = schemaResolver($"{schemaName}{field.Name.ToPascalCase()}Property", partitionObject); jsonSchema.Properties.Add(field.Name, CreateProperty(field, propertyReference)); } } return(jsonSchema); }
public JsonSchemaProperty?Visit(IField <GeolocationFieldProperties> field) { var geolocationSchema = Builder.Object(); geolocationSchema.Properties.Add("latitude", new JsonSchemaProperty { Type = JsonObjectType.Number, Minimum = -90, Maximum = 90, IsRequired = true }); geolocationSchema.Properties.Add("longitude", new JsonSchemaProperty { Type = JsonObjectType.Number, Minimum = -180, Maximum = 180, IsRequired = true }); var reference = schemaResolver("GeolocationDto", geolocationSchema); return(Builder.ObjectProperty(reference)); }