Skip to content

Commit

Permalink
EES-5494 Re-work schema property validation rendering
Browse files Browse the repository at this point in the history
This change adds missing validations and now renders validations for
child items (for arrays).
  • Loading branch information
ntsim committed Sep 12, 2024
1 parent 709a7f1 commit 808f846
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
13 changes: 13 additions & 0 deletions lib/api_reference_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ def is_primitive_schema(schema)
!is_complex_schema(schema) && !schema.type.nil? && schema.type != "object" && schema.type != "array"
end

# @return [Boolean]
def has_schema_validations(schema)
schema.min_length != 0 ||
schema.max_length != nil ||
schema.min_items != 0 ||
schema.max_items != nil ||
schema.min_properties != 0 ||
schema.max_properties != nil ||
schema.minimum != nil ||
schema.maximum != nil ||
schema.unique_items?
end

class Utils
# @param [Openapi3Parser::Node::Schema] schema
# @return [String, Number, Boolean]
Expand Down
39 changes: 25 additions & 14 deletions source/partials/_schema_description.html.md.erb
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
<%= description %>

<% is_referenced_schema = schema.node_context.document_location != schema.node_context.source_location %>
<% if schema.default %>
Defaults to: `<%= schema.default %>`
<% end %>

<% if has_schema_validations(schema) %>
Validation constraints:

<%= partial("partials/schema_validations", :locals => { schema: schema }) %>
<% end %>

<% is_referenced_schema =
schema.node_context.document_location != schema.node_context.source_location %>

<% if !is_referenced_schema && schema.enum %>
Available options:
Allowed options:

<% schema.enum.each do |item| %>
- `<%= item %>`
<% end %>
<% end %>

<%# TODO: Add other validation rules %>
<% unless schema.minimum.nil? && schema.maximum.nil? && schema.format.nil? %>
<% if schema.minimum %>
- <%= schema.exclusive_minimum? ? 'Minimum (exclusive)' : 'Minimum' %>: `<%= schema.minimum %>`
<% end %>
<% if schema.type == "array" && schema.items != nil %>
<% if has_schema_validations(schema.items) %>
Validation constraints for child items:

<% if schema.maximum %>
- <%= schema.exclusive_maximum? ? 'Maximum (exclusive)' : 'Maximum' %>: `<%= schema.maximum %>`
<%= partial("partials/schema_validations", :locals => { schema: schema.items }) %>
<% end %>

<% if schema.format %>
- Format: `<%= schema.format %>`
<% is_referenced_child_schema =
schema.items.node_context.document_location != schema.items.node_context.source_location %>

<% if !is_referenced_child_schema && schema.items.enum %>
Allowed options for child items:

<% schema.items.enum.each do |item| %>
- `<%= item %>`
<% end %>
<% end %>

<% if schema.default %>
Defaults to: `<%= schema.default %>`
<% end %>
37 changes: 37 additions & 0 deletions source/partials/_schema_validations.html.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<% if schema.format %>
- Format: `<%= schema.format %>`
<% end %>

<% unless schema.max_length.nil? %>
- Maximum length: `<%= schema.max_length %>`
<% end %>
<% if schema.min_length != 0 %>
- Minimum length: `<%= schema.min_length %>`
<% end %>

<% unless schema.max_items.nil? %>
- Maximum items: `<%= schema.max_items %>`
<% end %>
<% if schema.min_items != 0 %>
- Minimum items: `<%= schema.min_items %>`
<% end %>

<% if schema.unique_items? %>
- Items must be unique
<% end %>

<% unless schema.minimum.nil? && schema.maximum.nil? && schema.format.nil? %>
<% if schema.minimum %>
- <%= schema.exclusive_minimum? ? 'Minimum (exclusive)' : 'Minimum' %>: `<%= schema.minimum %>`
<% end %>
<% if schema.maximum %>
- <%= schema.exclusive_maximum? ? 'Maximum (exclusive)' : 'Maximum' %>: `<%= schema.maximum %>`
<% end %>
<% end %>

<% unless schema.max_properties.nil? %>
- Maximum properties: `<%= schema.max_properties %>`
<% end %>
<% if schema.min_properties != 0 %>
- Minimum properties: `<%= schema.min_properties %>`
<% end %>
2 changes: 1 addition & 1 deletion source/schemas/template.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<% end %>

<% if schema.enum %>
Available options:
Allowed options:

<% schema.enum.each do |item| %>
- `<%= item %>`
Expand Down

0 comments on commit 808f846

Please sign in to comment.