Class: Schemacop::V3::StringNode
- Inherits:
-
Node
- Object
- Node
- Schemacop::V3::StringNode
show all
- Defined in:
- lib/schemacop/v3/string_node.rb
Constant Summary
collapse
- ATTRIBUTES =
%i[
min_length
max_length
format
].freeze
Instance Attribute Summary
Attributes inherited from Node
#as, #default, #description, #name, #options, #parent, #require_key, #title
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Node
#children, #create, create, dsl_methods, #dsl_node, #dsl_scm, #initialize, #require_key?, #required?, resolve_class, #schemas, supports_children, supports_children_options, #used_external_schemas, #validate
Class Method Details
.allowed_options ⇒ Object
10
11
12
|
# File 'lib/schemacop/v3/string_node.rb', line 10
def self.allowed_options
super + ATTRIBUTES + %i[format_options pattern allow_blank]
end
|
Instance Method Details
#_validate(data, result:) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/schemacop/v3/string_node.rb', line 26
def _validate(data, result:)
super_data = super
if options[:allow_blank].is_a?(FalseClass) && super_data.blank?
result.error 'String is blank but must not be blank!'
end
return if super_data.nil?
length = super_data.size
if options[:min_length] && length < options[:min_length]
result.error "String is #{length} characters long but must be at least #{options[:min_length]}."
end
if options[:max_length] && length > options[:max_length]
result.error "String is #{length} characters long but must be at most #{options[:max_length]}."
end
if (pattern = options[:pattern])
unless options[:pattern].is_a?(Regexp)
pattern = Regexp.compile(pattern)
end
unless super_data.match?(pattern)
result.error "String does not match pattern #{V3.sanitize_exp(pattern).inspect}."
end
end
if options[:format] && Schemacop.string_formatters.include?(options[:format])
pattern = Schemacop.string_formatters[options[:format]][:pattern]
if pattern && !super_data.match?(pattern)
result.error "String does not match format #{options[:format].to_s.inspect}."
elsif options[:format_options] && Node.resolve_class(options[:format])
node = create(options[:format], **options[:format_options])
node._validate(cast(super_data), result: result)
end
end
end
|
#allowed_types ⇒ Object
14
15
16
|
# File 'lib/schemacop/v3/string_node.rb', line 14
def allowed_types
{ String => :string }
end
|
#as_json ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/schemacop/v3/string_node.rb', line 18
def as_json
json = { type: :string }
if options[:pattern]
json[:pattern] = V3.sanitize_exp(Regexp.compile(options[:pattern]))
end
process_json(ATTRIBUTES, json)
end
|
#cast(value) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/schemacop/v3/string_node.rb', line 71
def cast(value)
if !value.nil?
to_cast = value
elsif default.present?
to_cast = default
else
return nil
end
if (handler = Schemacop.string_formatters.dig(options[:format], :handler))
return handler.call(to_cast)
else
return to_cast
end
end
|