servus 1.3.1 → 1.4.0
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c33b9a419f8cb58f897e91a92b0e9ba4defee296f208fd5138162a809dcae61b
|
|
4
|
+
data.tar.gz: 5f3de9543cdf6c58ee2193414604552dd1207e182a163a32fc3e4d31a309bbf9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3a200f2f183f0776141aa7ee785b2b2333dd37a25743902585b5b740145c24e59dc61877ea5f9cd022fa22c313d884b22076e556370c634b0b91d1145285cca0
|
|
7
|
+
data.tar.gz: 8fe11259732799b057d81735072ec2266ddc58c3db6192fefc02250ac37b7334268b72478d7e10350ea1e1d74cd95971357c096fda71995c3475a1297af691a7
|
data/lib/servus/config.rb
CHANGED
|
@@ -120,6 +120,40 @@ module Servus
|
|
|
120
120
|
event_payload_enricher.call(payload, event_name:, service:)
|
|
121
121
|
end
|
|
122
122
|
|
|
123
|
+
# The validators {#schema_validator} can name.
|
|
124
|
+
SCHEMA_VALIDATORS = %i[json_schema json_schemer].freeze
|
|
125
|
+
|
|
126
|
+
# Which library validates service arguments, results and event payloads.
|
|
127
|
+
#
|
|
128
|
+
# +:json_schema+ (the default) uses the +json-schema+ gem, which compiles a
|
|
129
|
+
# +pattern+ as a Ruby regex: +^+ and +$+ match at any line, so
|
|
130
|
+
# +^[0-9]+$+ accepts +"1\nanything"+. +:json_schemer+ uses the
|
|
131
|
+
# +json_schemer+ gem, which the application adds to its own Gemfile, and
|
|
132
|
+
# reads each pattern as ECMAScript, as JSON Schema defines it: +^...$+
|
|
133
|
+
# covers the whole value. Both read schemas as Draft 6 and check the same
|
|
134
|
+
# formats; their error messages are worded differently.
|
|
135
|
+
#
|
|
136
|
+
# @return [Symbol] +:json_schema+ or +:json_schemer+
|
|
137
|
+
#
|
|
138
|
+
# @example Read patterns as JSON Schema defines them
|
|
139
|
+
# config.schema_validator = :json_schemer
|
|
140
|
+
attr_reader :schema_validator
|
|
141
|
+
|
|
142
|
+
# Sets which library validates schemas, loading +json_schemer+ when named.
|
|
143
|
+
#
|
|
144
|
+
# @param value [Symbol] one of {SCHEMA_VALIDATORS}
|
|
145
|
+
# @return [Symbol] the new value
|
|
146
|
+
# @raise [ArgumentError] for a validator Servus does not know
|
|
147
|
+
# @raise [LoadError] for +:json_schemer+ when the gem is not installed
|
|
148
|
+
def schema_validator=(value)
|
|
149
|
+
unless SCHEMA_VALIDATORS.include?(value)
|
|
150
|
+
raise ArgumentError, "schema_validator must be one of #{SCHEMA_VALIDATORS.inspect}, got #{value.inspect}"
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
require 'json_schemer' if value == :json_schemer
|
|
154
|
+
@schema_validator = value
|
|
155
|
+
end
|
|
156
|
+
|
|
123
157
|
# The ordered list of routers that resolve invocations for events.
|
|
124
158
|
#
|
|
125
159
|
# The Bus iterates routers in order, collects invocations, deduplicates
|
|
@@ -203,6 +237,7 @@ module Servus
|
|
|
203
237
|
@require_service_arguments_schema = false
|
|
204
238
|
@require_service_result_schema = false
|
|
205
239
|
@require_event_payload_schema = false
|
|
240
|
+
@schema_validator = :json_schema
|
|
206
241
|
@log_filter_parameters = [].freeze
|
|
207
242
|
end
|
|
208
243
|
|
|
@@ -25,6 +25,9 @@ module Servus
|
|
|
25
25
|
# @param finds [Class] the model class to resolve against (e.g., +User+, +Account+)
|
|
26
26
|
# @param by [Symbol] the lookup column (default: +:id+). When +:id+, uses +.find+.
|
|
27
27
|
# Otherwise uses +.find_by!(column: value)+.
|
|
28
|
+
# @param optional [Boolean] when true, resolves to +nil+ instead of raising if the
|
|
29
|
+
# value is +nil+ or finds no record; arrays keep only the records found
|
|
30
|
+
# (default: +false+)
|
|
28
31
|
# @return [void]
|
|
29
32
|
#
|
|
30
33
|
# @example Basic usage with .find
|
|
@@ -40,11 +43,16 @@ module Servus
|
|
|
40
43
|
# lazily :users, finds: User
|
|
41
44
|
# # users: [1, 2, 3] → User.where(id: [1, 2, 3])
|
|
42
45
|
#
|
|
46
|
+
# @example Optional record
|
|
47
|
+
# lazily :referrer, finds: User, optional: true
|
|
48
|
+
# # referrer: nil → nil
|
|
49
|
+
# # referrer: 999 → User.find_by(id: 999) → nil when not found
|
|
50
|
+
#
|
|
43
51
|
# @note Only available when ActiveRecord is loaded (via Railtie)
|
|
44
52
|
# @see Servus::Base.call
|
|
45
|
-
def lazily(name, finds:, by: :id)
|
|
46
|
-
(@lazy_resolvers ||= {})[name] = { klass: finds, by: by }
|
|
47
|
-
define_resolver_method(name, finds, by)
|
|
53
|
+
def lazily(name, finds:, by: :id, optional: false)
|
|
54
|
+
(@lazy_resolvers ||= {})[name] = { klass: finds, by: by, optional: optional }
|
|
55
|
+
define_resolver_method(name, finds, by, optional)
|
|
48
56
|
end
|
|
49
57
|
|
|
50
58
|
# Returns the hash of registered lazy resolvers for this service class.
|
|
@@ -62,8 +70,9 @@ module Servus
|
|
|
62
70
|
# @param name [Symbol] the method/ivar name
|
|
63
71
|
# @param klass [Class] the target model class
|
|
64
72
|
# @param by [Symbol] the lookup column
|
|
73
|
+
# @param optional [Boolean] whether nil values or missing records resolve to nil
|
|
65
74
|
# @api private
|
|
66
|
-
def define_resolver_method(name, klass, by)
|
|
75
|
+
def define_resolver_method(name, klass, by, optional)
|
|
67
76
|
mod = (@_resolver_module ||= Module.new)
|
|
68
77
|
prepend(mod) unless ancestors.include?(mod)
|
|
69
78
|
|
|
@@ -71,7 +80,8 @@ module Servus
|
|
|
71
80
|
@_lazily_resolved ||= {}
|
|
72
81
|
return instance_variable_get(:"@#{name}") if @_lazily_resolved[name]
|
|
73
82
|
|
|
74
|
-
resolved = Resolver.call(instance_variable_get(:"@#{name}"), klass: klass, by: by, name: name
|
|
83
|
+
resolved = Resolver.call(instance_variable_get(:"@#{name}"), klass: klass, by: by, name: name,
|
|
84
|
+
optional: optional)
|
|
75
85
|
@_lazily_resolved[name] = true
|
|
76
86
|
instance_variable_set(:"@#{name}", resolved)
|
|
77
87
|
end
|
|
@@ -16,7 +16,8 @@ module Servus
|
|
|
16
16
|
# Raised when a lazily-resolved record reference is nil.
|
|
17
17
|
#
|
|
18
18
|
# This occurs when a service declares +lazily :user, finds: User+ but
|
|
19
|
-
# receives +user: nil
|
|
19
|
+
# receives +user: nil+ and the resolver is not declared +optional: true+.
|
|
20
|
+
# Declare +lazily :user, finds: User, optional: true+ to resolve to +nil+ instead.
|
|
20
21
|
#
|
|
21
22
|
# @example
|
|
22
23
|
# class MyService < Servus::Base
|
|
@@ -17,15 +17,33 @@ module Servus
|
|
|
17
17
|
# @param klass [Class] the target model class
|
|
18
18
|
# @param by [Symbol] the lookup column
|
|
19
19
|
# @param name [Symbol] the param name (for error messages)
|
|
20
|
-
# @
|
|
21
|
-
#
|
|
22
|
-
|
|
20
|
+
# @param optional [Boolean] resolve to +nil+ instead of raising when the value
|
|
21
|
+
# is nil or finds no record
|
|
22
|
+
# @return [Object, nil] the resolved record or collection, or +nil+ when optional
|
|
23
|
+
# @raise [Errors::NotFoundError] if raw is nil and +optional+ is false
|
|
24
|
+
def self.call(raw, klass:, by:, name:, optional: false)
|
|
23
25
|
return raw if raw.is_a?(klass)
|
|
24
|
-
|
|
26
|
+
|
|
27
|
+
if raw.nil?
|
|
28
|
+
return nil if optional
|
|
29
|
+
|
|
30
|
+
raise Errors::NotFoundError, "Couldn't find #{klass} (#{name} was nil)"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
lookup(raw, klass: klass, by: by, optional: optional)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Looks up a present value. +.where+ already omits values that find no
|
|
37
|
+
# record, so arrays resolve the same way whether optional or not.
|
|
38
|
+
#
|
|
39
|
+
# @return [Object, nil]
|
|
40
|
+
def self.lookup(raw, klass:, by:, optional:)
|
|
25
41
|
return klass.where(by => raw) if raw.is_a?(Array)
|
|
42
|
+
return klass.find_by(by => raw) if optional
|
|
26
43
|
|
|
27
44
|
by == :id ? klass.find(raw) : klass.find_by!(by => raw)
|
|
28
45
|
end
|
|
46
|
+
private_class_method :lookup
|
|
29
47
|
end
|
|
30
48
|
end
|
|
31
49
|
end
|
|
@@ -31,9 +31,21 @@ module Servus
|
|
|
31
31
|
# @api private
|
|
32
32
|
SCHEMA_TYPES = %w[arguments result failure payload].freeze
|
|
33
33
|
|
|
34
|
+
# Formats +json_schemer+ checks under Draft 6 that +json-schema+ does not,
|
|
35
|
+
# left unchecked so the two validators agree on formats.
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
38
|
+
UNCHECKED_FORMATS = %w[email hostname uri-reference uri-template json-pointer]
|
|
39
|
+
.to_h { |format| [format, false] }.freeze
|
|
40
|
+
|
|
34
41
|
# @api private
|
|
35
42
|
@schema_cache = {}
|
|
36
43
|
|
|
44
|
+
# Compiled +json_schemer+ schemas, keyed by the schema Hash they compile.
|
|
45
|
+
#
|
|
46
|
+
# @api private
|
|
47
|
+
@schemers = ObjectSpace::WeakMap.new
|
|
48
|
+
|
|
37
49
|
# Validates service arguments against the service's arguments schema.
|
|
38
50
|
#
|
|
39
51
|
# @param service_class [Class] the service class being validated
|
|
@@ -186,12 +198,48 @@ module Servus
|
|
|
186
198
|
#
|
|
187
199
|
# @api private
|
|
188
200
|
def self.validate_data_against_schema!(data, schema, message_prefix)
|
|
189
|
-
errors =
|
|
201
|
+
errors = schema_errors(schema, data.as_json)
|
|
190
202
|
return if errors.empty?
|
|
191
203
|
|
|
192
204
|
raise Servus::Base::ValidationError, "#{message_prefix}: #{errors.join(', ')}"
|
|
193
205
|
end
|
|
194
206
|
|
|
207
|
+
# The messages for each way data misses a schema, from the configured
|
|
208
|
+
# {Servus::Config#schema_validator}.
|
|
209
|
+
#
|
|
210
|
+
# @param schema [Hash] the JSON schema to validate against
|
|
211
|
+
# @param data [Object] the data, already serialized to JSON types
|
|
212
|
+
# @return [Array<String>] one message per error, empty when the data is valid
|
|
213
|
+
#
|
|
214
|
+
# @example
|
|
215
|
+
# schema_errors({ 'type' => 'string' }, 1) # => ["The property '#/' of type integer did not match ..."]
|
|
216
|
+
#
|
|
217
|
+
# @api private
|
|
218
|
+
def self.schema_errors(schema, data)
|
|
219
|
+
return JSON::Validator.fully_validate(schema, data) if Servus.config.schema_validator == :json_schema
|
|
220
|
+
|
|
221
|
+
schemer(schema).validate(data).map { |error| error['error'] }
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# The schema compiled by +json_schemer+ as Draft 6, with ECMAScript
|
|
225
|
+
# patterns, compiled once per schema Hash.
|
|
226
|
+
#
|
|
227
|
+
# @param schema [Hash] the JSON schema to compile
|
|
228
|
+
# @return [JSONSchemer::Schema] the compiled schema
|
|
229
|
+
#
|
|
230
|
+
# @example
|
|
231
|
+
# schemer({ 'pattern' => '^[0-9]+$' }).valid?("1\nx") # => false
|
|
232
|
+
#
|
|
233
|
+
# @api private
|
|
234
|
+
def self.schemer(schema)
|
|
235
|
+
@schemers[schema] ||= JSONSchemer.schema(
|
|
236
|
+
schema,
|
|
237
|
+
meta_schema: JSONSchemer.draft6,
|
|
238
|
+
regexp_resolver: 'ecma',
|
|
239
|
+
formats: UNCHECKED_FORMATS
|
|
240
|
+
)
|
|
241
|
+
end
|
|
242
|
+
|
|
195
243
|
# Raises if a schema is absent and the corresponding config flag is on.
|
|
196
244
|
#
|
|
197
245
|
# @param schema [Hash, nil] the loaded schema
|
data/lib/servus/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: servus
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sebastian Scholl
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|