explicit 0.1.0 → 0.2.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.
@@ -15,6 +15,8 @@ module Explicit::Spec::Error
15
15
  translator.call(:agreement)
16
16
  in [:array, index, suberr]
17
17
  translator.call(:array, index:, error: translate(suberr, translator))
18
+ in :bigdecimal
19
+ translator.call(:bigdecimal)
18
20
  in :boolean
19
21
  translator.call(:boolean)
20
22
  in :date_time_iso8601
@@ -42,6 +44,7 @@ module Explicit::Spec::Error
42
44
  in [:literal, value]
43
45
  translator.call(:literal, value: value.inspect)
44
46
  in [:one_of, *errors]
47
+ # TODO
45
48
  errors.map { translate(_1, translator) }.join(" OR ")
46
49
  in :string
47
50
  translator.call(:string)
@@ -11,12 +11,30 @@ module Explicit::Spec::OneOf
11
11
  lambda do |value|
12
12
  subspec_validators.each do |subspec_validator|
13
13
  case subspec_validator.call(value)
14
- in [:ok, validated_value] then return [:ok, validated_value]
15
- in [:error, err] then errors << err
14
+ in [:ok, validated_value]
15
+ return [:ok, validated_value]
16
+ in [:error, err]
17
+ errors << err
18
+ end
19
+ end
20
+
21
+ errors.each do |err|
22
+ if looks_like_at_least_one_attribute_matched?(value, err)
23
+ return [:error, err]
16
24
  end
17
25
  end
18
26
 
19
27
  [:error, [:one_of, *errors]]
20
28
  end
21
29
  end
30
+
31
+ private
32
+ def looks_like_at_least_one_attribute_matched?(value, err)
33
+ return false if !value.is_a?(::Hash)
34
+ return false if !err.is_a?(::Hash)
35
+
36
+ keyset = value.keys - err.keys
37
+
38
+ keyset.empty?
39
+ end
22
40
  end
@@ -9,6 +9,8 @@ module Explicit::Spec::Record
9
9
  end
10
10
 
11
11
  lambda do |data|
12
+ return [:error, :hash] if !data.respond_to?(:[])
13
+
12
14
  validated_data = {}
13
15
  errors = {}
14
16
 
data/lib/explicit/spec.rb CHANGED
@@ -15,6 +15,11 @@ module Explicit::Spec
15
15
  in [:array, itemspec, options]
16
16
  Explicit::Spec::Array.build(itemspec, options)
17
17
 
18
+ in :bigdecimal
19
+ Explicit::Spec::Bigdecimal.build({})
20
+ in [:bigdecimal, options]
21
+ Explicit::Spec::Bigdecimal.build(options)
22
+
18
23
  in :boolean
19
24
  Explicit::Spec::Boolean.build({})
20
25
  in [:boolean, options]
@@ -28,6 +33,9 @@ module Explicit::Spec
28
33
  in [:default, defaultval, subspec]
29
34
  Explicit::Spec::Default.build(defaultval, subspec)
30
35
 
36
+ in [:description, _, subspec]
37
+ Explicit::Spec.build(subspec)
38
+
31
39
  in [:hash, keyspec, valuespec]
32
40
  Explicit::Spec::Hash.build(keyspec, valuespec, {})
33
41
  in [:hash, keyspec, valuespec, options]
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Explicit::TestHelper::ExampleRecorder
4
+ class << self
5
+ def start_service
6
+ ::DRb.start_service("drbunix:", instance).uri
7
+ end
8
+
9
+ def set_remote_instance(uri)
10
+ ::DRb.stop_service
11
+
12
+ @remote_instance = ::DRbObject.new_with_uri(uri)
13
+ end
14
+
15
+ def instance
16
+ return @remote_instance if defined?(@remote_instance)
17
+
18
+ @local_instance ||= new
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ @examples = Concurrent::Map.new { |hash, key| hash[key] = [] }
24
+ end
25
+
26
+ def add(request_gid:, params:, headers:, response:)
27
+ @examples[request_gid] << Explicit::Request::Example.new(
28
+ params:,
29
+ headers:,
30
+ response:
31
+ )
32
+ end
33
+
34
+ def save!
35
+ total_examples_count = @examples.sum { |_, examples| examples.size }
36
+ file_path = Explicit.configuration.request_examples_file_path
37
+
38
+ puts "" if Explicit.configuration.test_runner == :rspec
39
+ puts ""
40
+ puts " [Explicit] ========="
41
+ puts " [Explicit] Saving request examples to #{file_path}"
42
+ puts " [Explicit] #{total_examples_count} requests recorded"
43
+ puts " [Explicit] ========="
44
+ puts "" if Explicit.configuration.test_runner == :minitest
45
+
46
+ ::File.write(file_path, @examples.to_json, mode: "w")
47
+ end
48
+ end
@@ -1,11 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Explicit::TestHelper
4
- Response = ::Data.define(:status, :data) do
5
- def dig(...) = data.dig(...)
4
+ extend ActiveSupport::Concern
5
+
6
+ if Explicit.configuration.request_examples_persistance_enabled?
7
+ if Explicit.configuration.test_runner == :rspec
8
+ ::RSpec.configure do |config|
9
+ config.after(:suite) do
10
+ Explicit::TestHelper::ExampleRecorder.instance.save!
11
+ end
12
+ end
13
+ else
14
+ ::Minitest.after_run do
15
+ Explicit::TestHelper::ExampleRecorder.instance.save!
16
+ end
17
+ end
6
18
  end
7
19
 
8
- def fetch(request, params:, headers: nil)
20
+ included do
21
+ if Explicit.configuration.test_runner == :minitest
22
+ # TODO: improve this logic
23
+ running_in_parallel = Minitest.parallel_executor.is_a?(ActiveSupport::Testing::ParallelizeExecutor)
24
+
25
+ if running_in_parallel
26
+ uri = Explicit::TestHelper::ExampleRecorder.start_service
27
+
28
+ parallelize_setup do
29
+ Explicit::TestHelper::ExampleRecorder.set_remote_instance(uri)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def fetch(request, params: {}, headers: {}, **opts)
9
36
  route = request.send(:routes).first
10
37
 
11
38
  if route.nil?
@@ -23,21 +50,29 @@ module Explicit::TestHelper
23
50
 
24
51
  process(route.method, route.path, params:, headers:)
25
52
 
26
- response = Response.new(
53
+ response = Explicit::Request::Response.new(
27
54
  status: @response.status,
28
55
  data: @response.parsed_body.deep_symbolize_keys
29
56
  )
30
57
 
31
58
  ensure_response_matches_spec!(request, response)
32
59
 
60
+ if opts[:save_as_example]
61
+ ExampleRecorder.instance.add(
62
+ request_gid: request.gid,
63
+ params:,
64
+ headers:,
65
+ response:
66
+ )
67
+ end
68
+
33
69
  response
34
70
  end
35
71
 
36
72
  def ensure_response_matches_spec!(request, response)
37
- allowed_responses = request.send(:responses)
38
- response_validator = Explicit::Spec.build([:one_of, *allowed_responses])
73
+ validator = request.send(:responses_validator, status: response.status)
39
74
 
40
- case response_validator.call({ status: response.status, data: response.data.with_indifferent_access })
75
+ case validator.call(response.data.with_indifferent_access)
41
76
  in [:ok, _] then :all_good
42
77
  in [:error, err] then raise Explicit::Request::InvalidResponseError.new(response, err)
43
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Explicit
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/explicit.rb CHANGED
@@ -1,15 +1,25 @@
1
- require "explicit/documentation"
1
+ require "explicit/configuration"
2
2
  require "explicit/engine"
3
- require "explicit/test_helper"
4
3
  require "explicit/version"
5
4
 
5
+ require "explicit/documentation"
6
+ require "explicit/documentation/markdown"
7
+ require "explicit/documentation/property"
8
+
9
+ require "explicit/test_helper"
10
+ require "explicit/test_helper/example_recorder"
11
+
6
12
  require "explicit/request"
7
- require "explicit/request/invalid_params"
13
+ require "explicit/request/example"
14
+ require "explicit/request/invalid_params_error"
8
15
  require "explicit/request/invalid_response_error"
16
+ require "explicit/request/response"
17
+ require "explicit/request/route"
9
18
 
10
19
  require "explicit/spec"
11
20
  require "explicit/spec/agreement"
12
21
  require "explicit/spec/array"
22
+ require "explicit/spec/bigdecimal"
13
23
  require "explicit/spec/boolean"
14
24
  require "explicit/spec/date_time_iso8601"
15
25
  require "explicit/spec/date_time_posix"
@@ -23,6 +33,3 @@ require "explicit/spec/nilable"
23
33
  require "explicit/spec/one_of"
24
34
  require "explicit/spec/record"
25
35
  require "explicit/spec/string"
26
-
27
- module Explicit
28
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: explicit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Vasconcellos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-25 00:00:00.000000000 Z
11
+ date: 2024-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,8 +24,22 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 7.2.1
27
- description: explicit allows you to document, test and specify requests and responses
28
- schemas
27
+ - !ruby/object:Gem::Dependency
28
+ name: commonmarker
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: Explicit is a validation and documentation library for JSON APIs that
42
+ enforces documented specs at runtime
29
43
  email:
30
44
  - luizpvasc@gmail.com
31
45
  executables: []
@@ -38,17 +52,25 @@ files:
38
52
  - app/controllers/explicit/application_controller.rb
39
53
  - app/helpers/explicit/application_helper.rb
40
54
  - app/views/explicit/application/_documentation.html.erb
55
+ - app/views/explicit/application/_request.html.erb
41
56
  - config/locales/en.yml
42
57
  - config/routes.rb
43
58
  - lib/explicit.rb
59
+ - lib/explicit/configuration.rb
44
60
  - lib/explicit/documentation.rb
61
+ - lib/explicit/documentation/markdown.rb
62
+ - lib/explicit/documentation/property.rb
45
63
  - lib/explicit/engine.rb
46
64
  - lib/explicit/request.rb
47
- - lib/explicit/request/invalid_params.rb
65
+ - lib/explicit/request/example.rb
66
+ - lib/explicit/request/invalid_params_error.rb
48
67
  - lib/explicit/request/invalid_response_error.rb
68
+ - lib/explicit/request/response.rb
69
+ - lib/explicit/request/route.rb
49
70
  - lib/explicit/spec.rb
50
71
  - lib/explicit/spec/agreement.rb
51
72
  - lib/explicit/spec/array.rb
73
+ - lib/explicit/spec/bigdecimal.rb
52
74
  - lib/explicit/spec/boolean.rb
53
75
  - lib/explicit/spec/date_time_iso8601.rb
54
76
  - lib/explicit/spec/date_time_posix.rb
@@ -63,6 +85,7 @@ files:
63
85
  - lib/explicit/spec/record.rb
64
86
  - lib/explicit/spec/string.rb
65
87
  - lib/explicit/test_helper.rb
88
+ - lib/explicit/test_helper/example_recorder.rb
66
89
  - lib/explicit/version.rb
67
90
  - lib/tasks/schema/api_tasks.rake
68
91
  homepage: https://github.com/luizpvas/explicit
@@ -90,5 +113,5 @@ requirements: []
90
113
  rubygems_version: 3.4.22
91
114
  signing_key:
92
115
  specification_version: 4
93
- summary: explicit is a documentation and validation library for JSON APIs
116
+ summary: Explicit is a validation and documentation library for JSON APIs
94
117
  test_files: []