servus 1.4.0 → 1.4.1

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: c33b9a419f8cb58f897e91a92b0e9ba4defee296f208fd5138162a809dcae61b
4
- data.tar.gz: 5f3de9543cdf6c58ee2193414604552dd1207e182a163a32fc3e4d31a309bbf9
3
+ metadata.gz: fae044d2710cff4c5ac1c00359cfb13d689e579153e424d9f082f2f49aef0eba
4
+ data.tar.gz: 7c409852991cd2de58ea25d34d637c5f1c9d38d714252a0ca1a4a28418dc0710
5
5
  SHA512:
6
- metadata.gz: 3a200f2f183f0776141aa7ee785b2b2333dd37a25743902585b5b740145c24e59dc61877ea5f9cd022fa22c313d884b22076e556370c634b0b91d1145285cca0
7
- data.tar.gz: 8fe11259732799b057d81735072ec2266ddc58c3db6192fefc02250ac37b7334268b72478d7e10350ea1e1d74cd95971357c096fda71995c3475a1297af691a7
6
+ metadata.gz: 7aa8f051105dd7a2b7b2e73edfeb5a743b335502b9e68bf6060f9fac0c814992f66a4b3290f4994941f3471813895c70794e1b31dd16d66fec8e76493bbc5182
7
+ data.tar.gz: da6a3477573443ffee046c49cbb4e603d30560aa0d756d0cb4cb3cea0a58ea91cd36e06240624ab9f8e4f9f72e9017f4b79138a55ed7f90b4057468b1d98aac7
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servus
4
+ module Support
5
+ module SchemaValidators
6
+ # Validates with the +json-schema+ gem, as Draft 6. A +pattern+ is read
7
+ # as a Ruby regex, so its +^+ and +$+ match at any line.
8
+ #
9
+ # @api private
10
+ class JsonSchema
11
+ # @param schema [Hash] the JSON schema to validate against
12
+ # @param data [Object] the data, already serialized to JSON types
13
+ # @return [Array<String>] one message per error, empty when the data is valid
14
+ #
15
+ # @example
16
+ # JsonSchema.errors({ 'type' => 'string' }, 1)
17
+ # # => ["The property '#/' of type integer did not match the following type: string"]
18
+ def self.errors(schema, data)
19
+ JSON::Validator.fully_validate(schema, data)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Servus
4
+ module Support
5
+ module SchemaValidators
6
+ # Validates with the +json_schemer+ gem, as Draft 6, reading each
7
+ # +pattern+ as ECMAScript so +^...$+ covers the whole value. Everything
8
+ # else is read as {JsonSchema} reads it: Symbols in a schema as strings,
9
+ # and formats checked as +json-schema+ checks them.
10
+ #
11
+ # @api private
12
+ class JsonSchemer
13
+ # Formats +json_schemer+ checks under Draft 6 that +json-schema+ does not.
14
+ UNCHECKED_FORMATS = %w[email hostname uri-reference uri-template json-pointer].freeze
15
+
16
+ class << self
17
+ # @param schema [Hash] the JSON schema to validate against
18
+ # @param data [Object] the data, already serialized to JSON types
19
+ # @return [Array<String>] one message per error, empty when the data is valid
20
+ #
21
+ # @example
22
+ # JsonSchemer.errors({ 'pattern' => '^[0-9]+$' }, "1\nx")
23
+ # # => ["string at root does not match pattern: ^[0-9]+$"]
24
+ def errors(schema, data)
25
+ compiled(schema).validate(data).map { |error| error['error'] }
26
+ end
27
+
28
+ private
29
+
30
+ # The schema compiled once per schema Hash. +stringify+ is what
31
+ # +json-schema+ runs on a schema, so +required: %i[name]+ requires the
32
+ # +"name"+ key serialized data carries.
33
+ #
34
+ # @param schema [Hash] the JSON schema to compile
35
+ # @return [JSONSchemer::Schema]
36
+ #
37
+ # @example
38
+ # compiled({ 'type' => 'string' }).valid?('a') # => true
39
+ def compiled(schema)
40
+ cache[schema] ||= JSONSchemer.schema(
41
+ JSON::Schema.stringify(schema),
42
+ meta_schema: JSONSchemer.draft6,
43
+ regexp_resolver: 'ecma',
44
+ formats: formats
45
+ )
46
+ end
47
+
48
+ # @return [Hash{String => false, Method}] each format checked otherwise than +json_schemer+ would
49
+ #
50
+ # @example
51
+ # formats['email'] # => false
52
+ def formats
53
+ @formats ||= UNCHECKED_FORMATS.to_h { |format| [format, false] }.merge('uri' => method(:uri?))
54
+ end
55
+
56
+ # A +uri+ as +json-schema+ takes it: any reference it parses, +/path+
57
+ # included, where +json_schemer+ wants an absolute URI.
58
+ #
59
+ # @param value [Object] the value under the +format+ keyword
60
+ # @param _format [String] the format's name
61
+ # @return [Boolean] true for a reference, or for a value that is not a String
62
+ #
63
+ # @example
64
+ # uri?('/path', 'uri') # => true
65
+ def uri?(value, _format)
66
+ JSON::Util::URI.parse(value) if value.is_a?(String)
67
+ true
68
+ rescue JSON::Schema::UriError
69
+ false
70
+ end
71
+
72
+ # @return [ObjectSpace::WeakMap] compiled schemas, keyed by the schema Hash they compile
73
+ #
74
+ # @example
75
+ # cache[schema] # => #<JSONSchemer::Schema ...>
76
+ def cache
77
+ @cache ||= ObjectSpace::WeakMap.new
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -31,21 +31,17 @@ 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.
34
+ # The validator each {Servus::Config#schema_validator} names.
36
35
  #
37
36
  # @api private
38
- UNCHECKED_FORMATS = %w[email hostname uri-reference uri-template json-pointer]
39
- .to_h { |format| [format, false] }.freeze
37
+ SCHEMA_VALIDATORS = {
38
+ json_schema: SchemaValidators::JsonSchema,
39
+ json_schemer: SchemaValidators::JsonSchemer
40
+ }.freeze
40
41
 
41
42
  # @api private
42
43
  @schema_cache = {}
43
44
 
44
- # Compiled +json_schemer+ schemas, keyed by the schema Hash they compile.
45
- #
46
- # @api private
47
- @schemers = ObjectSpace::WeakMap.new
48
-
49
45
  # Validates service arguments against the service's arguments schema.
50
46
  #
51
47
  # @param service_class [Class] the service class being validated
@@ -212,32 +208,11 @@ module Servus
212
208
  # @return [Array<String>] one message per error, empty when the data is valid
213
209
  #
214
210
  # @example
215
- # schema_errors({ 'type' => 'string' }, 1) # => ["The property '#/' of type integer did not match ..."]
211
+ # schema_errors({ 'type' => 'string' }, 'a') # => []
216
212
  #
217
213
  # @api private
218
214
  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
- )
215
+ SCHEMA_VALIDATORS.fetch(Servus.config.schema_validator).errors(schema, data)
241
216
  end
242
217
 
243
218
  # Raises if a schema is absent and the corresponding config flag is on.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Servus
4
- VERSION = '1.4.0'
4
+ VERSION = '1.4.1'
5
5
  end
data/lib/servus.rb CHANGED
@@ -32,6 +32,8 @@ require_relative 'servus/schema'
32
32
  require_relative 'servus/support/logger'
33
33
  require_relative 'servus/support/data_object'
34
34
  require_relative 'servus/support/response'
35
+ require_relative 'servus/support/schema_validators/json_schema'
36
+ require_relative 'servus/support/schema_validators/json_schemer'
35
37
  require_relative 'servus/support/validator'
36
38
  require_relative 'servus/support/errors'
37
39
  require_relative 'servus/support/rescuer'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Scholl
@@ -116,6 +116,8 @@ files:
116
116
  - lib/servus/support/message_resolver.rb
117
117
  - lib/servus/support/rescuer.rb
118
118
  - lib/servus/support/response.rb
119
+ - lib/servus/support/schema_validators/json_schema.rb
120
+ - lib/servus/support/schema_validators/json_schemer.rb
119
121
  - lib/servus/support/validator.rb
120
122
  - lib/servus/testing.rb
121
123
  - lib/servus/testing/example_builders.rb