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 predicates for UUIDv6/7/8 #110

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/dry/logic/predicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def self.uuid_format(version)

UUIDv5 = uuid_format(5)

UUIDv6 = uuid_format(6)

UUIDv7 = uuid_format(7)

UUIDv8 = uuid_format(8)

def [](name)
method(name)
end
Expand Down Expand Up @@ -252,6 +258,18 @@ def uuid_v5?(input)
format?(UUIDv5, input)
end

def uuid_v6?(input)
format?(UUIDv6, input)
end

def uuid_v7?(input)
format?(UUIDv7, input)
end

def uuid_v8?(input)
format?(UUIDv8, input)
end

def uri?(schemes, input)
uri_format = URI::DEFAULT_PARSER.make_regexp(schemes)
format?(uri_format, input)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/predicates/uuid_v5_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
it_behaves_like "a passing predicate"
end

context "with value is not a valid V4 UUID" do
context "with value is not a valid V5 UUID" do
let(:arguments_list) do
[
["not-a-uuid-at-all\nf2d26c57-e07c-5416-a749-57e937930e04"], # V5 with invalid prefix
Expand Down
31 changes: 31 additions & 0 deletions spec/unit/predicates/uuid_v6_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "dry/logic/predicates"

RSpec.describe Dry::Logic::Predicates do
describe "#uuid_v6?" do
let(:predicate_name) { :uuid_v6? }

context "when value is a valid V6 UUID" do
let(:arguments_list) do
[["1ec9414c-232a-6b00-b3c8-9e6bdeced846"]]
end

it_behaves_like "a passing predicate"
end

context "with value is not a valid V6 UUID" do
let(:arguments_list) do
[
["not-a-uuid-at-all\n1ec9414c-232a-6b00-b3c8-9e6bdeced846"], # V6 with invalid prefix
["1ec9414c-232a-6b00-b3c8-9e6bdeced846\nnot-a-uuid-at-all"], # V6 with invalid suffix
["1ec9414c-232a-3b00-b3c8-9e6bdeced846"], # wrong version number (3, not 6)
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
["not-a-uuid-at-all"]
]
end

it_behaves_like "a failing predicate"
end
end
end
31 changes: 31 additions & 0 deletions spec/unit/predicates/uuid_v7_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "dry/logic/predicates"

RSpec.describe Dry::Logic::Predicates do
describe "#uuid_v7?" do
let(:predicate_name) { :uuid_v7? }

context "when value is a valid V7 UUID" do
let(:arguments_list) do
[["017f22e2-79b0-7cc3-98c4-dc0c0c07398f"]]
end

it_behaves_like "a passing predicate"
end

context "with value is not a valid V7 UUID" do
let(:arguments_list) do
[
["not-a-uuid-at-all\n017f22e2-79b0-7cc3-98c4-dc0c0c07398f"], # V6 with invalid prefix
["017f22e2-79b0-7cc3-98c4-dc0c0c07398f\nnot-a-uuid-at-all"], # V6 with invalid suffix
["017f22e2-79b0-4cc3-98c4-dc0c0c07398f"], # wrong version number (4, not 7)
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
["not-a-uuid-at-all"]
]
end

it_behaves_like "a failing predicate"
end
end
end
31 changes: 31 additions & 0 deletions spec/unit/predicates/uuid_v8_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "dry/logic/predicates"

RSpec.describe Dry::Logic::Predicates do
describe "#uuid_v8?" do
let(:predicate_name) { :uuid_v8? }

context "when value is a valid V8 UUID" do
let(:arguments_list) do
[["320c3d4d-cc00-875b-8ec9-32d5f69181c0"]]
end

it_behaves_like "a passing predicate"
end

context "with value is not a valid V8 UUID" do
let(:arguments_list) do
[
["not-a-uuid-at-all\n320c3d4d-cc00-875b-8ec9-32d5f69181c0"], # V6 with invalid prefix
["320c3d4d-cc00-875b-8ec9-32d5f69181c0\nnot-a-uuid-at-all"], # V6 with invalid suffix
["320c3d4d-cc00-475b-8ec9-32d5f69181c0"], # wrong version number (4, not 8)
["20633928-6a07-11e9-a923-1681be663d3e"], # UUID V1
["not-a-uuid-at-all"]
]
end

it_behaves_like "a failing predicate"
end
end
end