-
Notifications
You must be signed in to change notification settings - Fork 5
/
uuid.rb
37 lines (29 loc) · 856 Bytes
/
uuid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true
class MiniDefender::Rules::Uuid < MiniDefender::Rule
def initialize(version = nil)
raise ArgumentError, 'Expected version to be an integer or nil' unless version.nil? || version.is_a?(Integer)
@version = version
end
def self.signature
'uuid'
end
def self.make(args)
raise ArgumentError, 'Expected exactly one or zero argument' unless args.length <= 1
new(args[0]&.to_i)
end
def coerce(value)
value.downcase
end
def passes?(attribute, value, validator)
value.is_a?(String) &&
/^\h{8}-(\h{4}-){3}\h{12}$/i.match?(value) &&
(@version.nil? || value[14].to_i(16) == @version)
end
def message(attribute, value, validator)
if @version
"The value should be a valid UUID v#{@version}."
else
'The value should be a valid UUID.'
end
end
end