Skip to content

Commit

Permalink
Lookup Behaviour w Invalid UUID (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored Feb 9, 2024
1 parent 162f969 commit 92cff65
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 10 deletions.
12 changes: 6 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-12-06 07:57:17 UTC using RuboCop version 1.57.2.
# on 2024-02-09 11:25:12 UTC using RuboCop version 1.59.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Offense count: 3
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 64
Max: 38

# Offense count: 2
# Configuration parameters: IgnoredMetadata.
Expand All @@ -32,17 +32,17 @@ RSpec/ExampleLength:
RSpec/MultipleExpectations:
Max: 2

# Offense count: 2
# Offense count: 6
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 5
Max: 6

# Offense count: 2
RSpec/SubjectStub:
Exclude:
- 'spec/integrations/migrations/mysql/fx_migration_spec.rb'

# Offense count: 3
# Offense count: 4
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Expand Down
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
## [Unreleased]

## [0.1.2] - 2024-02-09

Configure The Behaviours When UUID Is Invalid

```ruby
UuidV7.configure do |config|
config.throw_invalid_uuid = false
end
```

```ruby
record_class.find_by(uuid: "invalid")
=> nil
```

```ruby
UuidV7.configure do |config|
config.throw_invalid_uuid = true
end
```

```ruby
record_class.find_by(uuid: "invalid")
raise_error(UuidV7::Types::InvalidUUID, "invalid is not a valid UUID")
```

## [0.1.1] - 2023-12-11

Fix wrong default field name. Change from :uuid to :id
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ end

**Recommendation:** It's advised to use `:id` as the primary key with Rails for compatibility and convention.

### Behaviours with Invalid UUIDs

```ruby
UuidV7.configure do |config|
config.throw_invalid_uuid = false
end
```

```ruby
record_class.find_by(uuid: "invalid")
=> nil
```

```ruby
UuidV7.configure do |config|
config.throw_invalid_uuid = true
end
```

```ruby
record_class.find_by(uuid: "invalid")
raise_error(UuidV7::Types::InvalidUUID, "invalid is not a valid UUID")
```

### Migrations

#### Helper for Mysql
Expand Down
1 change: 1 addition & 0 deletions lib/generators/uuid_v7/install/templates/uuid_v7.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ require "uuid_v7"
UuidV7.configure do |config|
# config.field_name = :id # default :id
# config.implicit_inclusion_strategy = true # default true
# config.throw_invalid_uuid = false # default false
end
3 changes: 2 additions & 1 deletion lib/uuid_v7/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

module UuidV7
class Configuration
attr_accessor :field_name, :implicit_inclusion_strategy
attr_accessor :field_name, :implicit_inclusion_strategy, :throw_invalid_uuid

def initialize
self.field_name = :id
self.implicit_inclusion_strategy = true
self.throw_invalid_uuid = true
end
end
end
1 change: 0 additions & 1 deletion lib/uuid_v7/patches/mysql/fk_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Alliantist
module Migrations
module UuidFkHelper

# update_fk_uuid_between_table(parent_table_name: :engines, child_table_name: :pistons)
def update_fk_uuid_between_table(parent_table_name:, child_table_name:)
table_name_parent = parent_table_name.to_s.to_sym # :engines
Expand Down
6 changes: 5 additions & 1 deletion lib/uuid_v7/types/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def serialize(value)
# To avoid SQL injection, verify that it looks like a UUID. ActiveRecord
# does not explicity escape the Binary data type. escaping is implicit as
# the Binary data type always converts its value to a hex string.
raise InvalidUUID, "#{value} is not a valid UUID" unless value.match?(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
unless value.match?(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
raise InvalidUUID, "#{value} is not a valid UUID" if UuidV7.configuration.throw_invalid_uuid

return nil
end

return value if value.is_a?(Data)

Expand Down
2 changes: 1 addition & 1 deletion lib/uuid_v7/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module UuidV7
VERSION = "0.1.1"
VERSION = "0.1.2"
end
28 changes: 28 additions & 0 deletions spec/integrations/crud_ar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@
it "finds a record by UUID .where" do
expect(record_class.where(uuid:).take).to eql(record)
end

context "with a UUID that doesn't exist" do
it "return nil when record is not found" do
expect(record_class.find_by(uuid: SecureRandom.uuid_v7)).to be_nil
end

context "with invalid UUID" do
context "when UuidV7.configuration.throw_invalid_uuid is false" do
it "return nil" do
expect(
UuidV7.with(throw_invalid_uuid: false) do
record_class.find_by(uuid: "invalid")
end
).to be_nil
end
end

context "when UuidV7.configuration.throw_invalid_uuid is true" do
it "raises UuidV7::Types::InvalidUUID" do
expect do
UuidV7.with(throw_invalid_uuid: true) do
record_class.find_by(uuid: "invalid")
end
end.to raise_error(UuidV7::Types::InvalidUUID, "invalid is not a valid UUID")
end
end
end
end
end

describe "update" do
Expand Down
20 changes: 20 additions & 0 deletions spec/support/with.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module With
def with(**attributes)
old_values = {}
begin
attributes.each do |key, value|
old_values[key] = UuidV7.configuration.public_send(key)
UuidV7.configuration.public_send(:"#{key}=", value)
end
yield self
ensure
old_values.each do |key, old_value|
UuidV7.configuration.public_send(:"#{key}=", old_value)
end
end
end
end

UuidV7.extend(With)

0 comments on commit 92cff65

Please sign in to comment.