Class: Schemacop::V3::Node
- Inherits:
-
Object
- Object
- Schemacop::V3::Node
show all
- Defined in:
- lib/schemacop/v3/node.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(**options, &block) ⇒ Node
Returns a new instance of Node.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/schemacop/v3/node.rb', line 76
def initialize(**options, &block)
disallowed_options = options.keys - self.class.allowed_options
if disallowed_options.any?
fail Schemacop::Exceptions::InvalidSchemaError, "Options #{disallowed_options.inspect} are not allowed for this node."
end
@name = options.delete(:name)
@name = @name.to_s unless @name.nil? || @name.is_a?(Regexp)
@as = options.delete(:as)
@required = !!options.delete(:required)
@default = options.delete(:default)
@title = options.delete(:title)
@description = options.delete(:description)
@examples = options.delete(:examples)
@enum = options.delete(:enum)&.to_set
@require_key = !!options.delete(:require_key)
@parent = options.delete(:parent)
@options = options
@schemas = {}
init
if block_given?
unless self.class.supports_children_options
fail Schemacop::Exceptions::InvalidSchemaError, "Node #{self.class} does not support blocks."
end
scope = DslScope.new(self)
env = ScopedEnv.new(self, self.class.dsl_methods, scope, :dsl_)
env.instance_exec(&block)
end
begin
validate_self
rescue StandardError => e
fail Exceptions::InvalidSchemaError, e.message
end
end
|
Instance Attribute Details
#as ⇒ Object
Returns the value of attribute as.
5
6
7
|
# File 'lib/schemacop/v3/node.rb', line 5
def as
@as
end
|
#default ⇒ Object
Returns the value of attribute default.
6
7
8
|
# File 'lib/schemacop/v3/node.rb', line 6
def default
@default
end
|
#description ⇒ Object
Returns the value of attribute description.
8
9
10
|
# File 'lib/schemacop/v3/node.rb', line 8
def description
@description
end
|
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/schemacop/v3/node.rb', line 4
def name
@name
end
|
#options ⇒ Object
Returns the value of attribute options.
9
10
11
|
# File 'lib/schemacop/v3/node.rb', line 9
def options
@options
end
|
#parent ⇒ Object
Returns the value of attribute parent.
10
11
12
|
# File 'lib/schemacop/v3/node.rb', line 10
def parent
@parent
end
|
#require_key ⇒ Object
Returns the value of attribute require_key.
11
12
13
|
# File 'lib/schemacop/v3/node.rb', line 11
def require_key
@require_key
end
|
#title ⇒ Object
Returns the value of attribute title.
7
8
9
|
# File 'lib/schemacop/v3/node.rb', line 7
def title
@title
end
|
Class Method Details
.allowed_options ⇒ Object
54
55
56
|
# File 'lib/schemacop/v3/node.rb', line 54
def self.allowed_options
i[name required default description examples enum parent options title as require_key]
end
|
.create(type = self, **options, &block) ⇒ Object
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
|
# File 'lib/schemacop/v3/node.rb', line 28
def self.create(type = self, **options, &block)
klass = resolve_class(type)
fail "Could not find node for type #{type.inspect}." unless klass
options = Schemacop.v3_default_options.slice(*klass.allowed_options).merge(options)
node = klass.new(**options, &block)
if options.delete(:cast_str)
format = NodeRegistry.name(klass)
one_of_options = {
required: options.delete(:required),
treat_blank_as_nil: true,
name: options.delete(:name),
as: options.delete(:as),
description: options.delete(:description)
}
node = create(:one_of, **one_of_options) do
self.node node
str format: format, format_options: options
end
node.instance_variable_set(:@cast_str_wrapper, true)
end
return node
end
|
.dsl_methods ⇒ Object
58
59
60
|
# File 'lib/schemacop/v3/node.rb', line 58
def self.dsl_methods
i[dsl_scm dsl_node]
end
|
.resolve_class(type) ⇒ Object
24
25
26
|
# File 'lib/schemacop/v3/node.rb', line 24
def self.resolve_class(type)
NodeRegistry.find(type)
end
|
.supports_children(name: false) ⇒ Object
16
17
18
|
# File 'lib/schemacop/v3/node.rb', line 16
def self.supports_children(name: false)
self._supports_children = { name: name }
end
|
.supports_children_options ⇒ Object
20
21
22
|
# File 'lib/schemacop/v3/node.rb', line 20
def self.supports_children_options
_supports_children
end
|
Instance Method Details
#allowed_types ⇒ Object
62
63
64
|
# File 'lib/schemacop/v3/node.rb', line 62
def allowed_types
{}
end
|
#as_json ⇒ Object
152
153
154
|
# File 'lib/schemacop/v3/node.rb', line 152
def as_json
process_json([], {})
end
|
#cast(value) ⇒ Object
156
157
158
|
# File 'lib/schemacop/v3/node.rb', line 156
def cast(value)
value || default
end
|
#cast_str_wrapper? ⇒ Boolean
140
141
142
|
# File 'lib/schemacop/v3/node.rb', line 140
def cast_str_wrapper?
!!@cast_str_wrapper
end
|
#children ⇒ Object
72
73
74
|
# File 'lib/schemacop/v3/node.rb', line 72
def children
[]
end
|
#create(type, **options, &block) ⇒ Object
121
122
123
124
|
# File 'lib/schemacop/v3/node.rb', line 121
def create(type, **options, &block)
options[:parent] = self
return Node.create(type, **options, &block)
end
|
#dsl_node(node, *_args, **_kwargs) ⇒ Object
132
133
134
|
# File 'lib/schemacop/v3/node.rb', line 132
def dsl_node(node, *_args, **_kwargs)
add_child node
end
|
#dsl_scm(name, type = :hash, **options, &block) ⇒ Object
128
129
130
|
# File 'lib/schemacop/v3/node.rb', line 128
def dsl_scm(name, type = :hash, **options, &block)
@schemas[name] = create(type, **options, &block)
end
|
#init ⇒ Object
126
|
# File 'lib/schemacop/v3/node.rb', line 126
def init; end
|
#require_key? ⇒ Boolean
148
149
150
|
# File 'lib/schemacop/v3/node.rb', line 148
def require_key?
@require_key
end
|
#required? ⇒ Boolean
144
145
146
|
# File 'lib/schemacop/v3/node.rb', line 144
def required?
@required
end
|
#schemas ⇒ Object
136
137
138
|
# File 'lib/schemacop/v3/node.rb', line 136
def schemas
(parent&.schemas || {}).merge(@schemas)
end
|
#used_external_schemas(encountered_nodes = Set.new) ⇒ Object
66
67
68
69
70
|
# File 'lib/schemacop/v3/node.rb', line 66
def used_external_schemas(encountered_nodes = Set.new)
return [] if encountered_nodes.include?(self)
return children.map { |c| c.used_external_schemas(encountered_nodes) }.flatten.uniq
end
|
#validate(data) ⇒ Object
160
161
162
163
164
|
# File 'lib/schemacop/v3/node.rb', line 160
def validate(data)
result = Result.new(self, data)
_validate(data, result: result)
return result
end
|