-
Notifications
You must be signed in to change notification settings - Fork 916
[WIP] Add Attribute Constraints (validation) support for GenericObjectDefinition #23654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
putmanoj
wants to merge
7
commits into
ManageIQ:master
Choose a base branch
from
putmanoj:gen-obj-attribute_constraints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,066
−8
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fd6167
add attribute_constraints property for adding attribute rules or cons…
b9a8ffc
Add validation for attribute_constraints in GenericObjectDefinition
16c0509
add spec for attribute_constraints in GenericObjectDefinition
d4db261
add spec for import & export of GenericObjectDefinitions with attribu…
a5326b3
add attribute constraint validation in GenericObject
48f4a09
add 'default' value constraint, for non-require attributes
0439acd
add two new attributes types: Hash and Array
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ class GenericObject < ApplicationRecord | |
| has_many :custom_button_events, :foreign_key => :target_id, :dependent => :destroy | ||
|
|
||
| validates :name, :presence => true | ||
| before_validation :apply_default_values | ||
| validate :validate_property_attribute_constraints | ||
|
|
||
| delegate :property_attribute_defined?, | ||
| :property_defined?, | ||
|
|
@@ -212,4 +214,116 @@ def remove_go_from_all_related_services | |
| remove_from_service(resource.service) if resource.service | ||
| end | ||
| end | ||
|
|
||
| def apply_default_values | ||
| return unless generic_object_definition | ||
|
|
||
| constraints = generic_object_definition.properties[:attribute_constraints] || {} | ||
|
|
||
| constraints.each do |attr_name, attr_constraints| | ||
| # Only apply default if attribute is not set and has a default value | ||
| if properties[attr_name].nil? && attr_constraints.key?(:default) | ||
| default_value = attr_constraints[:default] | ||
| # Type cast the default value | ||
| if property_attribute_defined?(attr_name) | ||
| properties[attr_name] = type_cast(attr_name, default_value) | ||
| end | ||
| end | ||
|
Comment on lines
+225
to
+231
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if there's a way to dry this up relative to _property_setter. |
||
| end | ||
| end | ||
|
|
||
| def validate_property_attribute_constraints | ||
| return unless generic_object_definition | ||
|
|
||
| constraints = generic_object_definition.properties[:attribute_constraints] || {} | ||
| attributes = generic_object_definition.properties[:attributes] || {} | ||
|
|
||
| constraints.each do |attr_name, attr_constraints| | ||
| # Get value from properties hash (where custom attributes are stored) | ||
| value = properties[attr_name] | ||
| attr_type = attributes[attr_name] | ||
|
|
||
| # Skip validation if attribute is not set (nil) unless it's required | ||
| next if value.nil? && !attr_constraints[:required] | ||
|
|
||
| # Type checking for Hash and Array | ||
| if attr_type == :hash && value.present? && !value.is_a?(Hash) | ||
| errors.add(:properties, "attribute '#{attr_name}' must be a Hash") | ||
| next | ||
| end | ||
|
|
||
| if attr_type == :array && value.present? && !value.is_a?(Array) | ||
| errors.add(:properties, "attribute '#{attr_name}' must be an Array") | ||
| next | ||
| end | ||
|
|
||
| attr_constraints.each do |constraint_type, constraint_value| | ||
| # Skip default constraint in validation (it's applied in before_validation) | ||
| next if constraint_type == :default | ||
| validate_constraint(attr_name, value, constraint_type, constraint_value) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def validate_constraint(attr_name, value, constraint_type, constraint_value) | ||
| case constraint_type | ||
| when :required | ||
| if constraint_value && (value.nil? || (value.is_a?(String) && value.strip.empty?)) | ||
| errors.add(:properties, "attribute '#{attr_name}' is required") | ||
| end | ||
| when :min | ||
| if value.present? && value < constraint_value | ||
| errors.add(:properties, "attribute '#{attr_name}' must be greater than or equal to #{constraint_value}") | ||
| end | ||
| when :max | ||
| if value.present? && value > constraint_value | ||
| errors.add(:properties, "attribute '#{attr_name}' must be less than or equal to #{constraint_value}") | ||
| end | ||
| when :min_length | ||
| if value.present? && value.to_s.length < constraint_value | ||
| errors.add(:properties, "attribute '#{attr_name}' must be at least #{constraint_value} characters long") | ||
| end | ||
| when :max_length | ||
| if value.present? && value.to_s.length > constraint_value | ||
| errors.add(:properties, "attribute '#{attr_name}' must be at most #{constraint_value} characters long") | ||
| end | ||
| when :enum | ||
| if value.present? && !constraint_value.include?(value) | ||
| errors.add(:properties, "attribute '#{attr_name}' must be one of: #{constraint_value.join(', ')}") | ||
| end | ||
| when :format | ||
| if value.present? | ||
| regex = constraint_value.is_a?(Regexp) ? constraint_value : Regexp.new(constraint_value) | ||
| unless regex.match?(value.to_s) | ||
| errors.add(:properties, "attribute '#{attr_name}' format is invalid") | ||
| end | ||
| end | ||
| when :min_items | ||
| if value.is_a?(Array) && value.size < constraint_value | ||
| errors.add(:properties, "attribute '#{attr_name}' must have at least #{constraint_value} items") | ||
| end | ||
| when :max_items | ||
| if value.is_a?(Array) && value.size > constraint_value | ||
| errors.add(:properties, "attribute '#{attr_name}' must have at most #{constraint_value} items") | ||
| end | ||
| when :unique_items | ||
| if constraint_value && value.is_a?(Array) && value.uniq.size != value.size | ||
| errors.add(:properties, "attribute '#{attr_name}' must have unique items") | ||
| end | ||
| when :required_keys | ||
| if value.is_a?(Hash) | ||
| missing_keys = constraint_value.map(&:to_s) - value.keys.map(&:to_s) | ||
| if missing_keys.any? | ||
| errors.add(:properties, "attribute '#{attr_name}' is missing required keys: #{missing_keys.join(', ')}") | ||
| end | ||
| end | ||
| when :allowed_keys | ||
| if value.is_a?(Hash) | ||
| extra_keys = value.keys.map(&:to_s) - constraint_value.map(&:to_s) | ||
| if extra_keys.any? | ||
| errors.add(:properties, "attribute '#{attr_name}' has disallowed keys: #{extra_keys.join(', ')}") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how this line can happen, because you have to create a generic object from a generic object definition. I think we can remove it.