Skip to content
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

Add presence validation for object in SnapshotItem model #56

Conversation

dmrAnderson
Copy link
Contributor

@dmrAnderson dmrAnderson commented Aug 24, 2024

#51

Improved Data Integrity
By adding validates :object, presence: true, you ensure that the object attribute cannot be null or empty when a SnapshotItem is created or updated. This helps maintain data integrity and prevents situations where object might be inadvertently left empty.

Error Handling for Unsupported Storage Methods
An explicit error is raised if an unsupported storage_method is used, making it clear when configuration issues occur and preventing unexpected behavior.

@dmrAnderson dmrAnderson changed the title Add presence validation for object in SnapshotItem model Add presence validation for object in SnapshotItem model Aug 24, 2024
@westonganger
Copy link
Owner

Can we reduce the scope of this PR to adding the validation only? The other change regarding changes to the object method to add the :raw argument should have its own PR to be reviewed and discussed separately.


[:item_id, :item_type, :snapshot_id].each do |attr|
%i[item_id item_type snapshot_id object].each do |attr|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just keep the original form

Suggested change
%i[item_id item_type snapshot_id object].each do |attr|
[:item_id, :item_type, :snapshot_id, :object].each do |attr|

end

@object = YAML.send(yaml_method, self[:object])
@object = super() ? YAML.public_send(yaml_method, super()) : {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the rest of the changes in this method are no longer relevant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I have to remove the rest or change the error class?

@object = self[:object]
@object = super()
else
raise ArgumentError, "Unsupported storage_method: `#{ActiveSnapshot.config.storage_method}`"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure that ArgumentError is appropriate here as storage_method wasnt a method argument. Maybe just StandardError is fine.

Suggested change
raise ArgumentError, "Unsupported storage_method: `#{ActiveSnapshot.config.storage_method}`"
raise StandardError, "Unhandled ActiveSnapshot.config.storage_method"

elsif ActiveSnapshot.config.storage_method_native_json?
@object = self[:object]
@object = super
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the mentions of super do not seem relevant in this PR. Should they not remain as self[:object]


def object
return @object if @object

if ActiveSnapshot.config.storage_method_json?
@object = JSON.parse(self[:object])
@object = self[:object] ? JSON.parse(self[:object]) : {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this empty object fallback on every line Maybe we can do @object ||= {} after the if else storage method statements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, but the new instance with fresh @object will go to JSON.parse(self[:object]) # => Error

@westonganger westonganger merged commit 7156f56 into westonganger:master Aug 29, 2024
21 checks passed
@dmrAnderson dmrAnderson deleted the add-validation-for-object-in-snapshot-item branch November 8, 2024 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants