interfacets 0.9.99 → 0.99999
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +9 -17
- data/lib/interfacets/client/actor.rb +2 -1
- data/lib/interfacets/client/assets.rb +77 -64
- data/lib/interfacets/client/bus.rb +28 -9
- data/lib/interfacets/client/channels/api.rb +138 -39
- data/lib/interfacets/client/channels/audio.rb +13 -10
- data/lib/interfacets/client/channels/base.rb +2 -2
- data/lib/interfacets/client/channels/pubsub.rb +69 -0
- data/lib/interfacets/client/channels/react/builder.rb +42 -72
- data/lib/interfacets/client/channels/react/channel.rb +7 -5
- data/lib/interfacets/client/channels/react/evaluator.rb +6 -4
- data/lib/interfacets/client/channels/timer.rb +8 -7
- data/lib/interfacets/client/channels/url.rb +39 -6
- data/lib/interfacets/client/registry.rb +3 -2
- data/lib/interfacets/client/slots.rb +45 -0
- data/lib/interfacets/client/system.rb +20 -15
- data/lib/interfacets/client/template.rb +90 -0
- data/lib/interfacets/client/utils/mruby_patches.rb +7 -3
- data/lib/interfacets/client/utils/open_struct.rb +2 -4
- data/lib/interfacets/component_registry.rb +34 -29
- data/lib/interfacets/component_schema_parser.rb +8 -4
- data/lib/interfacets/mruby/build_config.rb +8 -4
- data/lib/interfacets/mruby/entrypoint.rb +2 -2
- data/lib/interfacets/server/api.rb +44 -7
- data/lib/interfacets/server/assets.rb +58 -23
- data/lib/interfacets/server/basic_router.rb +29 -19
- data/lib/interfacets/server/bus.rb +5 -6
- data/lib/interfacets/server/registry.rb +17 -10
- data/lib/interfacets/shared/basic_routable.rb +7 -4
- data/lib/interfacets/shared/entities/bus.rb +619 -185
- data/lib/interfacets/shared/entities/changeset.rb +432 -0
- data/lib/interfacets/shared/entities/collection_proxy.rb +132 -72
- data/lib/interfacets/shared/entities/specs/handlers.rb +58 -37
- data/lib/interfacets/shared/entities/specs.rb +184 -18
- data/lib/interfacets/shared/entity.rb +113 -23
- data/lib/interfacets/shared/entity_dsl.rb +138 -20
- data/lib/interfacets/shared/facet.rb +128 -68
- data/lib/interfacets/shared/generated_store.rb +69 -34
- data/lib/interfacets/shared/refreshable.rb +85 -0
- data/lib/interfacets/shared/utils.rb +36 -32
- data/lib/interfacets/shared/validations.rb +746 -26
- data/lib/interfacets/shared/view.rb +74 -28
- data/lib/interfacets/test/component_registry.rb +31 -12
- data/lib/interfacets/test/js/inline_bus.rb +51 -25
- data/lib/interfacets/test/js/nodo_bus.rb +21 -10
- data/lib/interfacets/test/js/receivers/api.rb +14 -12
- data/lib/interfacets/test/js/receivers/audio.rb +61 -0
- data/lib/interfacets/test/js/receivers/pubsub.rb +50 -0
- data/lib/interfacets/test/js/receivers/react/node/xml_parser.rb +33 -5
- data/lib/interfacets/test/js/receivers/react/node.rb +69 -15
- data/lib/interfacets/test/js/receivers/react.rb +13 -4
- data/lib/interfacets/test/js/receivers/timer.rb +12 -8
- data/lib/interfacets/test/js/receivers/url.rb +47 -11
- data/lib/interfacets/test/ui_simulator.rb +88 -18
- data/lib/interfacets/test/validation_engine.rb +82 -49
- data/lib/interfacets/test.rb +1 -3
- data/lib/interfacets/version.rb +1 -1
- data/lib/interfacets.rb +11 -3
- metadata +31 -28
- data/.rubocop.yml +0 -180
- data/.tmp +0 -5
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -14
- data/lib/interfacets/client/channels/page_visibility.rb +0 -21
- data/lib/interfacets/client/channels/react/dom.rb +0 -91
- data/lib/interfacets/client/channels/speech_to_text.rb +0 -100
- data/lib/interfacets/client/config.rb +0 -22
- data/lib/interfacets/client/delegator.rb +0 -37
- data/lib/interfacets/server/assets/facet.rb +0 -63
- data/lib/interfacets/server/config.rb +0 -87
- data/lib/interfacets/server/facets/deserializer.rb +0 -25
- data/lib/interfacets/server/facets/schema/serializer.rb +0 -54
- data/lib/interfacets/server/facets/serializer.rb +0 -50
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require("nokogiri")
|
|
4
4
|
|
|
5
5
|
module Interfacets
|
|
6
6
|
module Test
|
|
@@ -33,7 +33,12 @@ module Interfacets
|
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
attr_reader
|
|
36
|
+
attr_reader(
|
|
37
|
+
:xml,
|
|
38
|
+
:dispatch,
|
|
39
|
+
:parent,
|
|
40
|
+
:validation_engine
|
|
41
|
+
)
|
|
37
42
|
def initialize(xml:, dispatch:, parent:, validation_engine:)
|
|
38
43
|
@xml = xml
|
|
39
44
|
@dispatch = dispatch
|
|
@@ -50,34 +55,45 @@ module Interfacets
|
|
|
50
55
|
end
|
|
51
56
|
|
|
52
57
|
def component_name
|
|
53
|
-
raise
|
|
58
|
+
raise(StaleNodeError) if stale?
|
|
54
59
|
|
|
55
60
|
xml.name
|
|
56
61
|
end
|
|
57
62
|
|
|
58
63
|
def props
|
|
59
|
-
raise
|
|
64
|
+
raise(StaleNodeError) if stale?
|
|
60
65
|
|
|
61
66
|
xml.attributes.transform_values do |attr|
|
|
62
|
-
attr
|
|
67
|
+
attr
|
|
68
|
+
.value
|
|
69
|
+
.then {
|
|
70
|
+
begin
|
|
71
|
+
JSON.parse(_1)
|
|
72
|
+
rescue
|
|
73
|
+
_1
|
|
74
|
+
end
|
|
75
|
+
}
|
|
76
|
+
.then { captured_to_nodes(_1) }
|
|
63
77
|
end
|
|
64
78
|
end
|
|
65
79
|
|
|
66
80
|
def content
|
|
67
|
-
raise
|
|
81
|
+
raise(StaleNodeError) if stale?
|
|
68
82
|
|
|
69
83
|
xml.content
|
|
70
84
|
end
|
|
71
85
|
|
|
72
86
|
def one(...)
|
|
73
|
-
raise
|
|
87
|
+
raise(StaleNodeError) if stale?
|
|
74
88
|
|
|
75
89
|
all(...)
|
|
76
90
|
.tap {
|
|
77
91
|
if _1.count == 0
|
|
78
|
-
raise
|
|
92
|
+
raise(NoMatchesError)
|
|
79
93
|
elsif _1.count > 1
|
|
80
|
-
raise
|
|
94
|
+
raise(
|
|
95
|
+
MultipleMatchesError.new(_1.map(&:content).join(", "))
|
|
96
|
+
)
|
|
81
97
|
end
|
|
82
98
|
}
|
|
83
99
|
.first
|
|
@@ -86,11 +102,18 @@ module Interfacets
|
|
|
86
102
|
EMPTY_ARG = Object.new
|
|
87
103
|
|
|
88
104
|
def all(*a, content: EMPTY_ARG, **p)
|
|
89
|
-
raise
|
|
105
|
+
raise(StaleNodeError) if stale?
|
|
90
106
|
|
|
91
107
|
xml
|
|
92
108
|
.css(*a, **p)
|
|
93
|
-
.map {
|
|
109
|
+
.map {
|
|
110
|
+
Node.new(
|
|
111
|
+
xml: _1,
|
|
112
|
+
dispatch:,
|
|
113
|
+
parent: self,
|
|
114
|
+
validation_engine:
|
|
115
|
+
)
|
|
116
|
+
}
|
|
94
117
|
.select {
|
|
95
118
|
(
|
|
96
119
|
content == EMPTY_ARG || (
|
|
@@ -106,25 +129,56 @@ module Interfacets
|
|
|
106
129
|
end
|
|
107
130
|
|
|
108
131
|
def attribute(name)
|
|
109
|
-
raise
|
|
132
|
+
raise(StaleNodeError) if stale?
|
|
110
133
|
|
|
111
134
|
xml
|
|
112
135
|
.attribute(name)
|
|
113
136
|
&.value
|
|
114
|
-
&.then {
|
|
137
|
+
&.then {
|
|
138
|
+
begin
|
|
139
|
+
JSON.parse(_1)
|
|
140
|
+
rescue
|
|
141
|
+
_1
|
|
142
|
+
end
|
|
143
|
+
}
|
|
144
|
+
&.then { captured_to_nodes(_1) }
|
|
115
145
|
end
|
|
116
146
|
|
|
117
147
|
def trigger(name, data = {})
|
|
118
|
-
raise
|
|
148
|
+
raise(StaleNodeError) if stale?
|
|
119
149
|
|
|
120
150
|
validation_engine&.validate_event(xml.name, name, data)
|
|
121
151
|
|
|
122
152
|
value = attribute(name.to_s)
|
|
123
|
-
raise
|
|
153
|
+
raise(
|
|
154
|
+
"No event handler registered for: #{name}"
|
|
155
|
+
) unless value
|
|
124
156
|
|
|
125
157
|
value["payload"]["event"] = data
|
|
126
158
|
dispatch.(value)
|
|
127
159
|
end
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
|
|
163
|
+
# Captured elements (`c.capture { ... }`) are passed as props; expose them as Nodes.
|
|
164
|
+
def captured_to_nodes(value)
|
|
165
|
+
if XmlParser.element_node?(value)
|
|
166
|
+
build_child(value)
|
|
167
|
+
elsif value.is_a?(Array) && value.any? && value.all? { XmlParser.element_node?(_1) }
|
|
168
|
+
value.map { build_child(_1) }
|
|
169
|
+
else
|
|
170
|
+
value
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def build_child(raw)
|
|
175
|
+
Node.new(
|
|
176
|
+
xml: XmlParser.parse_node(raw),
|
|
177
|
+
dispatch:,
|
|
178
|
+
parent: self,
|
|
179
|
+
validation_engine:
|
|
180
|
+
)
|
|
181
|
+
end
|
|
128
182
|
end
|
|
129
183
|
end
|
|
130
184
|
end
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require_relative
|
|
3
|
+
require("nokogiri")
|
|
4
|
+
require_relative("./react/node")
|
|
5
5
|
|
|
6
6
|
module Interfacets
|
|
7
7
|
module Test
|
|
8
8
|
module Js
|
|
9
9
|
module Receivers
|
|
10
10
|
class React
|
|
11
|
-
attr_reader
|
|
11
|
+
attr_reader(:node)
|
|
12
|
+
attr_reader(
|
|
13
|
+
:name,
|
|
14
|
+
:validation_engine,
|
|
15
|
+
:actions
|
|
16
|
+
)
|
|
12
17
|
def initialize(name:, validation_engine: nil)
|
|
13
18
|
@name = name
|
|
14
19
|
@validation_engine = validation_engine
|
|
@@ -19,7 +24,11 @@ module Interfacets
|
|
|
19
24
|
@dispatch = dispatch
|
|
20
25
|
@actions = nil
|
|
21
26
|
@node&.stale!
|
|
22
|
-
@node = Node.parse(
|
|
27
|
+
@node = Node.parse(
|
|
28
|
+
json: payload,
|
|
29
|
+
dispatch:,
|
|
30
|
+
validation_engine:
|
|
31
|
+
)
|
|
23
32
|
end
|
|
24
33
|
|
|
25
34
|
def handler
|
|
@@ -5,11 +5,15 @@ module Interfacets
|
|
|
5
5
|
module Js
|
|
6
6
|
module Receivers
|
|
7
7
|
class Timer
|
|
8
|
-
include
|
|
8
|
+
include(Enumerable)
|
|
9
9
|
|
|
10
10
|
class Registration
|
|
11
|
-
attr_reader
|
|
12
|
-
|
|
11
|
+
attr_reader(
|
|
12
|
+
:ms,
|
|
13
|
+
:response_payload,
|
|
14
|
+
:dispatch,
|
|
15
|
+
:notified
|
|
16
|
+
)
|
|
13
17
|
def initialize(ms:, response_payload:, dispatch:)
|
|
14
18
|
@ms = ms
|
|
15
19
|
@response_payload = response_payload
|
|
@@ -18,13 +22,13 @@ module Interfacets
|
|
|
18
22
|
end
|
|
19
23
|
|
|
20
24
|
def notify
|
|
21
|
-
return if
|
|
22
|
-
|
|
25
|
+
return if notified
|
|
26
|
+
dispatch.call({ payload: response_payload })
|
|
23
27
|
@notified = true
|
|
24
28
|
end
|
|
25
29
|
end
|
|
26
30
|
|
|
27
|
-
attr_reader
|
|
31
|
+
attr_reader(:name, :registrations)
|
|
28
32
|
def initialize(name:)
|
|
29
33
|
@name = name
|
|
30
34
|
@registrations = []
|
|
@@ -63,8 +67,8 @@ module Interfacets
|
|
|
63
67
|
registrations.last
|
|
64
68
|
end
|
|
65
69
|
|
|
66
|
-
def each(&
|
|
67
|
-
registrations.each(&
|
|
70
|
+
def each(&)
|
|
71
|
+
registrations.each(&)
|
|
68
72
|
end
|
|
69
73
|
|
|
70
74
|
def clear
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require('uri')
|
|
4
4
|
|
|
5
5
|
module Interfacets
|
|
6
6
|
module Test
|
|
7
7
|
module Js
|
|
8
8
|
module Receivers
|
|
9
9
|
class Url
|
|
10
|
-
class
|
|
10
|
+
class Stream
|
|
11
|
+
attr_reader(:state)
|
|
11
12
|
def initialize(state)
|
|
12
|
-
@state = state
|
|
13
|
+
@state = state || {}
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def url
|
|
16
|
-
url_spec =
|
|
17
|
+
url_spec = state["urlSpec"]
|
|
18
|
+
return unless url_spec
|
|
19
|
+
|
|
17
20
|
base_url = url_spec["url"]
|
|
18
21
|
query_params = url_spec["queryParams"]
|
|
19
22
|
|
|
@@ -25,13 +28,34 @@ module Interfacets
|
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
def redirected_url
|
|
28
|
-
|
|
31
|
+
state["redirectSpec"] || state["redirectUrlSpec"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def rendered_url
|
|
35
|
+
render_spec = state["renderUrlSpec"] || state["navigateUrlSpec"]
|
|
36
|
+
return unless render_spec
|
|
37
|
+
|
|
38
|
+
base_url = render_spec["url"]
|
|
39
|
+
query_params = render_spec["queryParams"]
|
|
40
|
+
|
|
41
|
+
return base_url if query_params.nil? || query_params.empty?
|
|
42
|
+
|
|
43
|
+
uri = URI(base_url)
|
|
44
|
+
uri.query = URI.encode_www_form(query_params)
|
|
45
|
+
uri.to_s
|
|
29
46
|
end
|
|
47
|
+
alias_method(:navigated_url, :rendered_url)
|
|
30
48
|
end
|
|
31
49
|
|
|
32
|
-
attr_reader
|
|
33
|
-
|
|
50
|
+
attr_reader(:server)
|
|
51
|
+
attr_reader(
|
|
52
|
+
:name,
|
|
53
|
+
:router,
|
|
54
|
+
:response_queue
|
|
55
|
+
)
|
|
56
|
+
def initialize(name:, router: nil)
|
|
34
57
|
@name = name
|
|
58
|
+
@router = router
|
|
35
59
|
@response_queue = []
|
|
36
60
|
end
|
|
37
61
|
|
|
@@ -43,13 +67,25 @@ module Interfacets
|
|
|
43
67
|
return if payload.dig("streams", "default").nil?
|
|
44
68
|
return if payload.dig("streams", "default").empty?
|
|
45
69
|
|
|
46
|
-
|
|
47
|
-
|
|
70
|
+
default_stream = payload.dig("streams", "default")
|
|
71
|
+
@handler = Stream.new(default_stream)
|
|
72
|
+
|
|
73
|
+
render_spec = default_stream["renderUrlSpec"] || default_stream["navigateUrlSpec"]
|
|
74
|
+
return unless render_spec && router
|
|
48
75
|
|
|
49
|
-
|
|
50
|
-
|
|
76
|
+
url = render_spec["url"]
|
|
77
|
+
query_params = render_spec["queryParams"]
|
|
78
|
+
if query_params && !query_params.empty?
|
|
79
|
+
uri = URI(url)
|
|
80
|
+
uri.query = URI.encode_www_form(query_params)
|
|
81
|
+
url = uri.to_s
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
response_queue << router.call(url).render
|
|
51
85
|
end
|
|
52
86
|
|
|
87
|
+
attr_reader(:handler)
|
|
88
|
+
|
|
53
89
|
def flush_responses
|
|
54
90
|
response_queue.tap { @response_queue = [] }
|
|
55
91
|
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
|
|
2
3
|
module Interfacets
|
|
3
4
|
module Test
|
|
@@ -8,25 +9,71 @@ module Interfacets
|
|
|
8
9
|
mruby_wasm: Js::NodoBus,
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
def
|
|
12
|
+
# The bus type used when none is given: the mruby/Nodo bus under NODO=1, otherwise inline.
|
|
13
|
+
def self.default_type
|
|
14
|
+
ENV.key?("NODO") ? :nodo : :inline
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.new(type: default_type, **)
|
|
18
|
+
type ||= default_type
|
|
13
19
|
unless TYPES.keys.include?(type)
|
|
14
|
-
raise
|
|
15
|
-
|
|
20
|
+
raise(
|
|
21
|
+
ArgumentError.new(
|
|
22
|
+
"type: #{type.inspect} not allowed. Must be one of #{TYPES.keys}",
|
|
23
|
+
),
|
|
16
24
|
)
|
|
17
25
|
end
|
|
18
26
|
|
|
27
|
+
super(type:, **).tap(&:system_json)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
attr_reader(:facet_name)
|
|
31
|
+
attr_reader(
|
|
32
|
+
:bus,
|
|
33
|
+
:router,
|
|
34
|
+
:type,
|
|
35
|
+
:contract_path,
|
|
36
|
+
:validation
|
|
37
|
+
)
|
|
38
|
+
def initialize(
|
|
39
|
+
bus:,
|
|
40
|
+
router:,
|
|
41
|
+
type:,
|
|
42
|
+
contract_path: nil,
|
|
43
|
+
validation: :strict
|
|
44
|
+
)
|
|
45
|
+
@bus = bus
|
|
19
46
|
@type = type
|
|
20
|
-
# Serialize and deserialize to ensure proper data structure for JS
|
|
21
|
-
@system_json = JSON.parse(bus.client_system_json(only_facets: type == :inline).to_json)
|
|
22
47
|
@router = router
|
|
23
48
|
@contract_path = contract_path
|
|
24
49
|
@validation = validation
|
|
25
50
|
end
|
|
26
51
|
|
|
52
|
+
# Serialize and deserialize to ensure proper data structure for JS
|
|
53
|
+
def system_json
|
|
54
|
+
@system_json ||= JSON.parse(
|
|
55
|
+
bus.client_system_json(only_facets: type == :inline).to_json
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
|
|
27
59
|
def visit(path)
|
|
60
|
+
rendered = router.call(path).render
|
|
61
|
+
@facet_name = rendered[:facet] || rendered["facet"]
|
|
28
62
|
js.init(
|
|
29
|
-
hydrated_facet:
|
|
63
|
+
hydrated_facet: rendered,
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def patch(payload, facet_name: @facet_name, nesting: ["root"])
|
|
68
|
+
facet = facet_name || @facet_name
|
|
69
|
+
dispatch(
|
|
70
|
+
"interfacets:api",
|
|
71
|
+
{
|
|
72
|
+
"facet" => facet,
|
|
73
|
+
"action" => "patch",
|
|
74
|
+
"nesting" => nesting,
|
|
75
|
+
"payload" => payload,
|
|
76
|
+
},
|
|
30
77
|
)
|
|
31
78
|
end
|
|
32
79
|
|
|
@@ -50,24 +97,47 @@ module Interfacets
|
|
|
50
97
|
c("timer")
|
|
51
98
|
end
|
|
52
99
|
|
|
100
|
+
def pubsub
|
|
101
|
+
c("pubsub")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def audio
|
|
105
|
+
c("audio")
|
|
106
|
+
end
|
|
107
|
+
|
|
53
108
|
private
|
|
54
109
|
|
|
55
110
|
def js
|
|
56
111
|
@js ||= (
|
|
57
|
-
validation_engine =
|
|
58
|
-
|
|
59
|
-
|
|
112
|
+
validation_engine = (
|
|
113
|
+
if contract_path
|
|
114
|
+
ValidationEngine.new(
|
|
115
|
+
ComponentRegistry.load(contract_path),
|
|
116
|
+
validation_mode: validation,
|
|
117
|
+
)
|
|
118
|
+
end
|
|
119
|
+
)
|
|
60
120
|
|
|
61
|
-
js_api = Test::Js::Receivers::Api.new(
|
|
62
|
-
|
|
63
|
-
|
|
121
|
+
js_api = Test::Js::Receivers::Api.new(
|
|
122
|
+
name: "interfacets:api",
|
|
123
|
+
router:
|
|
124
|
+
)
|
|
125
|
+
js_react = Test::Js::Receivers::React.new(
|
|
126
|
+
name: "dom",
|
|
127
|
+
validation_engine:
|
|
128
|
+
)
|
|
129
|
+
js_url = Test::Js::Receivers::Url.new(name: "url", router:)
|
|
64
130
|
js_timer = Test::Js::Receivers::Timer.new(name: "timer")
|
|
65
|
-
|
|
131
|
+
js_pubsub = Test::Js::Receivers::Pubsub.new(name: "pubsub")
|
|
132
|
+
js_audio = Test::Js::Receivers::Audio.new(name: "audio")
|
|
133
|
+
receivers = [js_api, js_react, js_url, js_timer, js_pubsub, js_audio]
|
|
66
134
|
|
|
67
|
-
TYPES
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
135
|
+
TYPES
|
|
136
|
+
.fetch(type)
|
|
137
|
+
.new(
|
|
138
|
+
receiver_index: receivers.to_h { [_1.name, _1] },
|
|
139
|
+
client_system_json: system_json,
|
|
140
|
+
)
|
|
71
141
|
)
|
|
72
142
|
end
|
|
73
143
|
end
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require "yaml"
|
|
3
|
+
require("json_schemer")
|
|
4
|
+
require("yaml")
|
|
6
5
|
|
|
7
6
|
module Interfacets
|
|
8
7
|
module Test
|
|
9
8
|
class ValidationEngine
|
|
10
|
-
|
|
9
|
+
MODES = %i[strict warn permissive]
|
|
11
10
|
|
|
12
11
|
@warned_components = Set.new
|
|
13
12
|
|
|
@@ -16,10 +15,21 @@ module Interfacets
|
|
|
16
15
|
end
|
|
17
16
|
|
|
18
17
|
def self.reset_warnings!
|
|
19
|
-
|
|
18
|
+
warned_components.clear
|
|
20
19
|
end
|
|
21
20
|
|
|
22
|
-
def
|
|
21
|
+
def self.new(registry = ComponentRegistry.load, validation_mode: :strict)
|
|
22
|
+
raise(
|
|
23
|
+
ArgumentError.new(
|
|
24
|
+
"Unknown validation_mode: #{validation_mode.inspect} (expected one of #{MODES.inspect})"
|
|
25
|
+
)
|
|
26
|
+
) unless MODES.include?(validation_mode)
|
|
27
|
+
|
|
28
|
+
super
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader(:registry, :validation_mode)
|
|
32
|
+
def initialize(registry, validation_mode:)
|
|
23
33
|
@registry = registry
|
|
24
34
|
@validation_mode = validation_mode
|
|
25
35
|
end
|
|
@@ -41,11 +51,19 @@ module Interfacets
|
|
|
41
51
|
else
|
|
42
52
|
return unless contract_exists?(component_name)
|
|
43
53
|
|
|
44
|
-
raise
|
|
54
|
+
raise(
|
|
55
|
+
ValidationError.new(
|
|
56
|
+
"Missing props schema for custom component: #{component_name}"
|
|
57
|
+
)
|
|
58
|
+
) unless props_config
|
|
45
59
|
end
|
|
46
60
|
|
|
47
61
|
schema = { "type" => "object", "properties" => props_config }
|
|
48
|
-
validate!(
|
|
62
|
+
validate!(
|
|
63
|
+
schema,
|
|
64
|
+
props,
|
|
65
|
+
"properties for #{component_name}"
|
|
66
|
+
)
|
|
49
67
|
end
|
|
50
68
|
|
|
51
69
|
def validate_event(component_name, event_name, payload)
|
|
@@ -61,22 +79,41 @@ module Interfacets
|
|
|
61
79
|
else
|
|
62
80
|
return unless contract_exists?(component_name)
|
|
63
81
|
|
|
64
|
-
raise
|
|
65
|
-
|
|
82
|
+
raise(
|
|
83
|
+
ValidationError.new(
|
|
84
|
+
"Missing events schema for custom component: #{component_name}"
|
|
85
|
+
)
|
|
86
|
+
) unless contract.key?("props")
|
|
87
|
+
raise(
|
|
88
|
+
ValidationError.new(
|
|
89
|
+
"Missing event schema for '#{event_name}' in custom component: #{component_name}"
|
|
90
|
+
)
|
|
91
|
+
) unless event_config
|
|
66
92
|
end
|
|
67
93
|
|
|
68
94
|
schema = event_config.is_a?(Hash) ? (event_config["payload"] || {}) : {}
|
|
69
95
|
|
|
70
|
-
validate!(
|
|
96
|
+
validate!(
|
|
97
|
+
schema,
|
|
98
|
+
payload,
|
|
99
|
+
"event '#{event_name}' for #{component_name}"
|
|
100
|
+
)
|
|
71
101
|
end
|
|
72
102
|
|
|
73
103
|
private
|
|
74
104
|
|
|
75
105
|
def standard_element?(component_name)
|
|
76
|
-
@standard_elements ||=
|
|
77
|
-
|
|
78
|
-
|
|
106
|
+
@standard_elements ||= (
|
|
107
|
+
begin
|
|
108
|
+
yaml_path = File.expand_path(
|
|
109
|
+
"standard_elements.yml",
|
|
110
|
+
__dir__
|
|
111
|
+
)
|
|
112
|
+
Set.new(
|
|
113
|
+
YAML.load_file(yaml_path).map(&:to_s)
|
|
114
|
+
)
|
|
79
115
|
end
|
|
116
|
+
)
|
|
80
117
|
@standard_elements.include?(component_name.to_s)
|
|
81
118
|
end
|
|
82
119
|
|
|
@@ -86,27 +123,27 @@ module Interfacets
|
|
|
86
123
|
|
|
87
124
|
case validation_mode
|
|
88
125
|
when :strict
|
|
89
|
-
raise
|
|
126
|
+
raise(
|
|
127
|
+
MissingComponentContractError.new(
|
|
128
|
+
"Missing component contract for: #{component_name}"
|
|
129
|
+
)
|
|
130
|
+
)
|
|
90
131
|
when :warn
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
132
|
+
first_time = (
|
|
133
|
+
self
|
|
134
|
+
.class
|
|
135
|
+
.warned_components
|
|
136
|
+
.add?(component_name)
|
|
137
|
+
)
|
|
138
|
+
warn(
|
|
139
|
+
"Warning: Missing component contract for: #{component_name}"
|
|
140
|
+
) if first_time
|
|
95
141
|
false
|
|
96
142
|
when :permissive
|
|
97
143
|
false
|
|
98
|
-
else
|
|
99
|
-
raise ArgumentError, "Unknown validation_mode: #{validation_mode}"
|
|
100
144
|
end
|
|
101
145
|
end
|
|
102
146
|
|
|
103
|
-
def find_contract!(name)
|
|
104
|
-
contract = registry.find_component(name)
|
|
105
|
-
return contract if contract
|
|
106
|
-
|
|
107
|
-
raise MissingComponentContractError, "Missing component contract for: #{name}"
|
|
108
|
-
end
|
|
109
|
-
|
|
110
147
|
def validate!(schema, data, context)
|
|
111
148
|
full_schema = schema.dup
|
|
112
149
|
if full_schema["type"] == "object" && !full_schema.key?("additionalProperties")
|
|
@@ -114,26 +151,22 @@ module Interfacets
|
|
|
114
151
|
end
|
|
115
152
|
full_schema["definitions"] = registry.definitions if registry.definitions.any?
|
|
116
153
|
|
|
117
|
-
schemer = JSONSchemer.schema(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
true
|
|
135
|
-
end
|
|
136
|
-
})
|
|
154
|
+
schemer = JSONSchemer.schema(
|
|
155
|
+
|
|
156
|
+
full_schema,
|
|
157
|
+
|
|
158
|
+
keywords: {
|
|
159
|
+
"is_event" => lambda do |instance, keyword_value, pointer|
|
|
160
|
+
return true unless keyword_value
|
|
161
|
+
if instance.is_a?(Hash) && instance.key?("type") && instance.key?("payload")
|
|
162
|
+
true
|
|
163
|
+
else
|
|
164
|
+
[false, "missing 'type' or 'payload'"]
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
)
|
|
137
170
|
|
|
138
171
|
errors = schemer.validate(data).to_a
|
|
139
172
|
|
|
@@ -144,7 +177,7 @@ module Interfacets
|
|
|
144
177
|
message += " - #{error['data_pointer']}: #{error['type']} #{error['details']}\n"
|
|
145
178
|
end
|
|
146
179
|
|
|
147
|
-
raise
|
|
180
|
+
raise(ValidationError.new(message))
|
|
148
181
|
end
|
|
149
182
|
end
|
|
150
183
|
end
|