From 808f8460614a5e8c6fab086de28975532208730a Mon Sep 17 00:00:00 2001 From: Nicholas Tsim Date: Thu, 12 Sep 2024 15:19:54 +0100 Subject: [PATCH] EES-5494 Re-work schema property validation rendering This change adds missing validations and now renders validations for child items (for arrays). --- lib/api_reference_helpers.rb | 13 +++++++ .../partials/_schema_description.html.md.erb | 39 ++++++++++++------- .../partials/_schema_validations.html.md.erb | 37 ++++++++++++++++++ source/schemas/template.html.md.erb | 2 +- 4 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 source/partials/_schema_validations.html.md.erb diff --git a/lib/api_reference_helpers.rb b/lib/api_reference_helpers.rb index f4cd6c8..3e8449f 100644 --- a/lib/api_reference_helpers.rb +++ b/lib/api_reference_helpers.rb @@ -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] diff --git a/source/partials/_schema_description.html.md.erb b/source/partials/_schema_description.html.md.erb index d2fc496..9b600d6 100644 --- a/source/partials/_schema_description.html.md.erb +++ b/source/partials/_schema_description.html.md.erb @@ -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 %> diff --git a/source/partials/_schema_validations.html.md.erb b/source/partials/_schema_validations.html.md.erb new file mode 100644 index 0000000..9338648 --- /dev/null +++ b/source/partials/_schema_validations.html.md.erb @@ -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 %> diff --git a/source/schemas/template.html.md.erb b/source/schemas/template.html.md.erb index 6604f19..852c312 100644 --- a/source/schemas/template.html.md.erb +++ b/source/schemas/template.html.md.erb @@ -5,7 +5,7 @@ <% end %> <% if schema.enum %> -Available options: +Allowed options: <% schema.enum.each do |item| %> - `<%= item %>`