diff --git a/lib/typeid.rb b/lib/typeid.rb index 46484c8..0486817 100644 --- a/lib/typeid.rb +++ b/lib/typeid.rb @@ -54,6 +54,7 @@ def initialize( raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z]*$/) raise Error, "suffix must be #{SUFFIX_LENGTH} characters" unless suffix.length == SUFFIX_LENGTH raise Error, "suffix must only contain the letters in '#{TypeID::UUID::Base32::ALPHABET}'" unless suffix.chars.all? { |char| TypeID::UUID::Base32::ALPHABET.include?(char) } + raise Error, "suffix must start with a 0-7 digit to avoid overflows" unless ("0".."7").cover?(suffix.chars.first) @prefix = prefix @suffix = suffix diff --git a/spec/invalid.yml b/spec/invalid.yml index c1470a2..6a2870b 100644 --- a/spec/invalid.yml +++ b/spec/invalid.yml @@ -4,7 +4,7 @@ # Each example contains an invalid TypeID string. Implementations are expected # to throw an error when attempting to parse/validate these strings. # -# Last updated: 2023-06-29 +# Last updated: 2023-07-05 - name: prefix-uppercase typeid: "PREFIX_00000000000000000000000000" @@ -31,7 +31,7 @@ description: "The prefix can't have any spaces" - name: prefix-64-chars - # 123456789 123456789 123456789 123456789 123456789 123456789 1234 + # 123456789 123456789 123456789 123456789 123456789 123456789 1234 typeid: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl_00000000000000000000000000" description: "The prefix can't be 64 characters, it needs to be 63 characters or less" @@ -81,3 +81,8 @@ # This example would be valid if we were using the crockford hyphenation rules typeid: "prefix_123456789-0123456789-0123456" description: "The suffix can't ignore hyphens as in the crockford encoding" + +- name: suffix-overflow + # This is the first suffix that overflows into 129 bits + typeid: "prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz" + description: "The should encode at most 128-bits" diff --git a/spec/lib/typeid_spec.rb b/spec/lib/typeid_spec.rb index 35f4a30..bf0bcfc 100644 --- a/spec/lib/typeid_spec.rb +++ b/spec/lib/typeid_spec.rb @@ -27,6 +27,12 @@ it { is_expected.to eq string } end + describe ".nil" do + subject(:type_id) { TypeID.nil } + + it { is_expected.to eq "00000000000000000000000000" } + end + describe "#string" do it { is_expected.to eq "#{prefix}_#{suffix}" } end diff --git a/spec/valid.yml b/spec/valid.yml index 964c96f..8f63250 100644 --- a/spec/valid.yml +++ b/spec/valid.yml @@ -17,13 +17,13 @@ # decoding and re-encoding the id, the result is the same as the original. # # In other words, the following property should always hold: -# random_typeid: encode(decode(random_typeid)) +# random_typeid == encode(decode(random_typeid)) # # Finally, while implementations should be able to decode the values below, # note that not all of them are UUIDv7s. When *generating* new random typeids, # implementations should always use UUIDv7s. # -# Last updated: 2023-06-29 +# Last updated: 2023-07-05 - name: nil typeid: "00000000000000000000000000" @@ -50,6 +50,11 @@ prefix: "" uuid: "00000000-0000-0000-0000-000000000020" +- name: max-valid + typeid: "7zzzzzzzzzzzzzzzzzzzzzzzzz" + prefix: "" + uuid: "ffffffff-ffff-ffff-ffff-ffffffffffff" + - name: valid-alphabet typeid: "prefix_0123456789abcdefghjkmnpqrs" prefix: "prefix"