domainic-type 0.1.0.alpha.3.0.2 → 0.1.0.alpha.3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/domainic/type/behavior/string_behavior/matching_behavior.rb +115 -0
- data/lib/domainic/type/behavior/string_behavior.rb +2 -111
- data/lib/domainic/type/behavior/uri_behavior.rb +97 -0
- data/lib/domainic/type/behavior.rb +21 -1
- data/lib/domainic/type/config/registry.yml +15 -0
- data/lib/domainic/type/definitions.rb +182 -1
- data/lib/domainic/type/types/core/array_type.rb +1 -1
- data/lib/domainic/type/types/core/float_type.rb +1 -1
- data/lib/domainic/type/types/core/hash_type.rb +1 -1
- data/lib/domainic/type/types/core/integer_type.rb +1 -1
- data/lib/domainic/type/types/core/string_type.rb +1 -1
- data/lib/domainic/type/types/core/symbol_type.rb +1 -1
- data/lib/domainic/type/types/identifier/cuid_type.rb +140 -0
- data/lib/domainic/type/types/identifier/uuid_type.rb +513 -0
- data/lib/domainic/type/types/network/email_address_type.rb +149 -0
- data/lib/domainic/type/types/network/hostname_type.rb +107 -0
- data/lib/domainic/type/types/network/uri_type.rb +224 -0
- data/sig/domainic/type/behavior/string_behavior/matching_behavior.rbs +84 -0
- data/sig/domainic/type/behavior/string_behavior.rbs +2 -82
- data/sig/domainic/type/behavior/uri_behavior.rbs +95 -0
- data/sig/domainic/type/behavior.rbs +9 -0
- data/sig/domainic/type/definitions.rbs +149 -1
- data/sig/domainic/type/types/identifier/cuid_type.rbs +110 -0
- data/sig/domainic/type/types/identifier/uuid_type.rbs +469 -0
- data/sig/domainic/type/types/network/email_address_type.rbs +127 -0
- data/sig/domainic/type/types/network/hostname_type.rbs +76 -0
- data/sig/domainic/type/types/network/uri_type.rbs +159 -0
- metadata +20 -6
@@ -33,7 +33,7 @@ module Domainic
|
|
33
33
|
include Behavior
|
34
34
|
include Behavior::NumericBehavior
|
35
35
|
|
36
|
-
|
36
|
+
intrinsically_constrain :self, :type, Float, abort_on_failure: true, description: :not_described
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -42,7 +42,7 @@ module Domainic
|
|
42
42
|
include Behavior
|
43
43
|
include Behavior::EnumerableBehavior
|
44
44
|
|
45
|
-
|
45
|
+
intrinsically_constrain :self, :type, Hash, abort_on_failure: true, description: :not_described
|
46
46
|
|
47
47
|
# Validate that the hash contains specific keys
|
48
48
|
#
|
@@ -32,7 +32,7 @@ module Domainic
|
|
32
32
|
include Behavior
|
33
33
|
include Behavior::NumericBehavior
|
34
34
|
|
35
|
-
|
35
|
+
intrinsically_constrain :self, :type, Integer, abort_on_failure: true, description: :not_described
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -45,7 +45,7 @@ module Domainic
|
|
45
45
|
include Behavior
|
46
46
|
include Behavior::StringBehavior
|
47
47
|
|
48
|
-
|
48
|
+
intrinsically_constrain :self, :type, String, abort_on_failure: true, description: :not_described
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
@@ -45,7 +45,7 @@ module Domainic
|
|
45
45
|
include Behavior
|
46
46
|
include Behavior::StringBehavior
|
47
47
|
|
48
|
-
|
48
|
+
intrinsically_constrain :self, :type, Symbol, abort_on_failure: true, description: :not_described
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'domainic/type/behavior'
|
4
|
+
|
5
|
+
module Domainic
|
6
|
+
module Type
|
7
|
+
# A type for validating CUIDs according to official specifications
|
8
|
+
#
|
9
|
+
# This type provides comprehensive CUID validation following the official
|
10
|
+
# CUID specifications. It supports both v1 and v2 formats, with proper
|
11
|
+
# format-specific validation.
|
12
|
+
#
|
13
|
+
# Key features:
|
14
|
+
# - CUID v1 validation (25 characters, c-prefix)
|
15
|
+
# - CUID v2 validation (variable length 14-24 chars)
|
16
|
+
# - Length validation
|
17
|
+
# - Character set validation
|
18
|
+
#
|
19
|
+
# @example Basic usage
|
20
|
+
# type = CUIDType.new
|
21
|
+
# type.validate("ch72gsb320000udocl363eofy") # => true
|
22
|
+
# type.validate("invalid") # => false
|
23
|
+
#
|
24
|
+
# @example With version constraints
|
25
|
+
# type = CUIDType.new.being_version(2)
|
26
|
+
# type.validate("clh3am1f30000udocbhqg4151") # => true
|
27
|
+
#
|
28
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
29
|
+
# @since 0.1.0
|
30
|
+
class CUIDType
|
31
|
+
# @rbs! extend Behavior::ClassMethods
|
32
|
+
|
33
|
+
include Behavior
|
34
|
+
|
35
|
+
# Contains CUID validation regular expressions and version-specific modules
|
36
|
+
#
|
37
|
+
# This module provides validation patterns for both CUID v1 and v2 formats,
|
38
|
+
# following the official specification.
|
39
|
+
#
|
40
|
+
# @since 0.1.0
|
41
|
+
module CUID
|
42
|
+
# Regular expression for CUID v1 format
|
43
|
+
#
|
44
|
+
# Validates CUIDs in v1 format: c-prefix + 24 lowercase alphanumeric chars
|
45
|
+
# Example: "ch72gsb320000udocl363eofy"
|
46
|
+
#
|
47
|
+
# @since 0.1.0
|
48
|
+
V1_REGEXP = /\Ac[a-z0-9]{24}\z/ #: Regexp
|
49
|
+
|
50
|
+
# Regular expression for CUID v2 format
|
51
|
+
#
|
52
|
+
# Validates CUIDs in v2 format: k-p prefix + 13-23 lowercase alphanumeric chars
|
53
|
+
# Example: "clh3am1f30000udocbhqg4151"
|
54
|
+
#
|
55
|
+
# @since 0.1.0
|
56
|
+
V2_REGEXP = /\A[k-p][a-z0-9]{13,23}\z/ #: Regexp
|
57
|
+
|
58
|
+
# Regular expression for any valid CUID (v1 or v2)
|
59
|
+
ALL_REGEXP = Regexp.union(V1_REGEXP, V2_REGEXP) #: Regexp
|
60
|
+
end
|
61
|
+
|
62
|
+
# Core CUID constraints
|
63
|
+
intrinsically_constrain :self, :type, String, description: :not_described
|
64
|
+
|
65
|
+
# Accept either v1 or v2 format by default
|
66
|
+
intrinsically_constrain :self, :match_pattern, CUID::ALL_REGEXP,
|
67
|
+
description: 'CUID format', concerning: :format
|
68
|
+
|
69
|
+
# Length constraint covers both v1 (25 chars) and v2 (14-24 chars)
|
70
|
+
intrinsically_constrain :length, :range, { minimum: 14, maximum: 25 },
|
71
|
+
description: 'having', concerning: :size
|
72
|
+
|
73
|
+
# Constrain CUID to specific version
|
74
|
+
#
|
75
|
+
# Creates a constraint ensuring the CUID matches a specific version (1 or 2).
|
76
|
+
#
|
77
|
+
# @example
|
78
|
+
# type.being_version(1)
|
79
|
+
# type.validate("ch72gsb320000udocl363eofy") # => true
|
80
|
+
#
|
81
|
+
# @param version [Integer] CUID version (1 or 2)
|
82
|
+
# @return [self] self for method chaining
|
83
|
+
# @raise [ArgumentError] if version is neither 1 nor 2
|
84
|
+
# @rbs (Integer version) -> self
|
85
|
+
def being_version(version)
|
86
|
+
case version
|
87
|
+
when 1 then being_version_one
|
88
|
+
when 2 then being_version_two
|
89
|
+
else
|
90
|
+
raise ArgumentError, "Invalid version: #{version}. Must be 1 or 2"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
alias version being_version
|
94
|
+
|
95
|
+
# Constrain CUID to Version 1
|
96
|
+
#
|
97
|
+
# Creates a constraint ensuring the CUID is a Version 1 CUID.
|
98
|
+
# Version 1 CUIDs are 25 characters long and start with 'c'.
|
99
|
+
#
|
100
|
+
# @example
|
101
|
+
# type.being_version_one
|
102
|
+
# type.validate("ch72gsb320000udocl363eofy") # => true
|
103
|
+
#
|
104
|
+
# @return [self] self for method chaining
|
105
|
+
# @rbs () -> self
|
106
|
+
def being_version_one
|
107
|
+
constrain :length, :range, { minimum: 25, maximum: 25 },
|
108
|
+
concerning: :size, description: 'having v1 length'
|
109
|
+
constrain :self, :match_pattern, CUID::V1_REGEXP,
|
110
|
+
description: 'v1 CUID format',
|
111
|
+
concerning: :version
|
112
|
+
end
|
113
|
+
alias being_v1 being_version_one
|
114
|
+
alias v1 being_version_one
|
115
|
+
alias version_one being_version_one
|
116
|
+
|
117
|
+
# Constrain CUID to Version 2
|
118
|
+
#
|
119
|
+
# Creates a constraint ensuring the CUID is a Version 2 CUID.
|
120
|
+
# Version 2 CUIDs are 14-24 characters and start with k-p.
|
121
|
+
#
|
122
|
+
# @example
|
123
|
+
# type.being_version_two
|
124
|
+
# type.validate("clh3am1f30000udocbhqg4151") # => true
|
125
|
+
#
|
126
|
+
# @return [self] self for method chaining
|
127
|
+
# @rbs () -> self
|
128
|
+
def being_version_two
|
129
|
+
constrain :length, :range, { minimum: 14, maximum: 24 },
|
130
|
+
concerning: :size, description: 'having v2 length'
|
131
|
+
constrain :self, :match_pattern, CUID::V2_REGEXP,
|
132
|
+
description: 'v2 CUID format',
|
133
|
+
concerning: :version
|
134
|
+
end
|
135
|
+
alias being_v2 being_version_two
|
136
|
+
alias v2 being_version_two
|
137
|
+
alias version_two being_version_two
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|