grape 0.5.0 → 3.2.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 +7 -0
- data/CHANGELOG.md +1223 -164
- data/CONTRIBUTING.md +148 -0
- data/LICENSE +1 -1
- data/README.md +3531 -675
- data/UPGRADING.md +1851 -0
- data/grape.gemspec +26 -32
- data/grape.png +0 -0
- data/lib/grape/api/instance.rb +194 -0
- data/lib/grape/api.rb +110 -485
- data/lib/grape/content_types.rb +28 -0
- data/lib/grape/cookies.rb +32 -25
- data/lib/grape/declared_params_handler.rb +118 -0
- data/lib/grape/dry_types.rb +54 -0
- data/lib/grape/dsl/callbacks.rb +19 -0
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +65 -0
- data/lib/grape/dsl/headers.rb +21 -0
- data/lib/grape/dsl/helpers.rb +103 -0
- data/lib/grape/dsl/inside_route.rb +293 -0
- data/lib/grape/dsl/logger.rb +19 -0
- data/lib/grape/dsl/middleware.rb +36 -0
- data/lib/grape/dsl/parameters.rb +239 -0
- data/lib/grape/dsl/request_response.rb +168 -0
- data/lib/grape/dsl/routing.rb +288 -0
- data/lib/grape/dsl/settings.rb +67 -0
- data/lib/grape/dsl/validations.rb +51 -0
- data/lib/grape/endpoint.rb +287 -386
- data/lib/grape/env.rb +20 -0
- data/lib/grape/error_formatter/base.rb +56 -19
- data/lib/grape/error_formatter/json.rb +18 -7
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +14 -11
- data/lib/grape/error_formatter/xml.rb +5 -11
- data/lib/grape/error_formatter.rb +15 -0
- data/lib/grape/exceptions/base.rb +31 -47
- data/lib/grape/exceptions/incompatible_option_values.rb +11 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +11 -0
- data/lib/grape/exceptions/invalid_formatter.rb +3 -5
- data/lib/grape/exceptions/invalid_message_body.rb +11 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +11 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +3 -6
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +3 -7
- data/lib/grape/exceptions/method_not_allowed.rb +11 -0
- data/lib/grape/exceptions/missing_group_type.rb +11 -0
- data/lib/grape/exceptions/missing_mime_type.rb +3 -6
- data/lib/grape/exceptions/missing_vendor_option.rb +3 -5
- data/lib/grape/exceptions/request_error.rb +11 -0
- data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
- data/lib/grape/exceptions/unknown_parameter.rb +11 -0
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +3 -4
- data/lib/grape/exceptions/unsupported_group_type.rb +11 -0
- data/lib/grape/exceptions/validation.rb +20 -8
- data/lib/grape/exceptions/validation_array_errors.rb +14 -0
- data/lib/grape/exceptions/validation_errors.rb +59 -0
- data/lib/grape/formatter/base.rb +9 -26
- data/lib/grape/formatter/json.rb +6 -7
- data/lib/grape/formatter/serializable_hash.rb +24 -20
- data/lib/grape/formatter/txt.rb +5 -7
- data/lib/grape/formatter/xml.rb +6 -7
- data/lib/grape/formatter.rb +17 -0
- data/lib/grape/json.rb +10 -0
- data/lib/grape/locale/en.yml +47 -20
- data/lib/grape/middleware/auth/base.rb +25 -0
- data/lib/grape/middleware/auth/dsl.rb +38 -0
- data/lib/grape/middleware/auth/strategies.rb +25 -0
- data/lib/grape/middleware/auth/strategy_info.rb +15 -0
- data/lib/grape/middleware/base.rb +73 -19
- data/lib/grape/middleware/error.rb +104 -44
- data/lib/grape/middleware/filter.rb +5 -3
- data/lib/grape/middleware/formatter.rb +115 -105
- data/lib/grape/middleware/globals.rb +14 -0
- data/lib/grape/middleware/stack.rb +110 -0
- data/lib/grape/middleware/versioner/accept_version_header.rb +11 -40
- data/lib/grape/middleware/versioner/base.rb +76 -0
- data/lib/grape/middleware/versioner/header.rb +61 -78
- data/lib/grape/middleware/versioner/param.rb +9 -21
- data/lib/grape/middleware/versioner/path.rb +16 -31
- data/lib/grape/middleware/versioner.rb +10 -15
- data/lib/grape/namespace.rb +39 -10
- data/lib/grape/params_builder/base.rb +20 -0
- data/lib/grape/params_builder/hash.rb +11 -0
- data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
- data/lib/grape/params_builder/hashie_mash.rb +11 -0
- data/lib/grape/params_builder.rb +15 -0
- data/lib/grape/parser/base.rb +9 -24
- data/lib/grape/parser/json.rb +8 -7
- data/lib/grape/parser/xml.rb +8 -7
- data/lib/grape/parser.rb +15 -0
- data/lib/grape/path.rb +76 -0
- data/lib/grape/presenters/presenter.rb +11 -0
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +182 -0
- data/lib/grape/router/base_route.rb +48 -0
- data/lib/grape/router/greedy_route.rb +26 -0
- data/lib/grape/router/pattern.rb +67 -0
- data/lib/grape/router/route.rb +58 -0
- data/lib/grape/router.rb +167 -0
- data/lib/grape/serve_stream/file_body.rb +36 -0
- data/lib/grape/serve_stream/sendfile_response.rb +21 -0
- data/lib/grape/serve_stream/stream_response.rb +23 -0
- data/lib/grape/util/api_description.rb +58 -0
- data/lib/grape/util/base_inheritable.rb +46 -0
- data/lib/grape/util/cache.rb +18 -0
- data/lib/grape/util/deep_freeze.rb +35 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +107 -0
- data/lib/grape/util/inheritable_values.rb +29 -0
- data/lib/grape/util/lazy/block.rb +29 -0
- data/lib/grape/util/lazy/value.rb +38 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +34 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +70 -0
- data/lib/grape/util/registry.rb +27 -0
- data/lib/grape/util/reverse_stackable_values.rb +15 -0
- data/lib/grape/util/stackable_values.rb +36 -0
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +77 -0
- data/lib/grape/validations/contract_scope.rb +28 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/param_scope_tracker.rb +57 -0
- data/lib/grape/validations/params_documentation.rb +50 -0
- data/lib/grape/validations/params_scope.rb +529 -0
- data/lib/grape/validations/single_attribute_iterator.rb +26 -0
- data/lib/grape/validations/types/array_coercer.rb +60 -0
- data/lib/grape/validations/types/custom_type_coercer.rb +164 -0
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +56 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +59 -0
- data/lib/grape/validations/types/file.rb +31 -0
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +71 -0
- data/lib/grape/validations/types/multiple_type_coercer.rb +57 -0
- data/lib/grape/validations/types/primitive_coercer.rb +46 -0
- data/lib/grape/validations/types/set_coercer.rb +35 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +51 -0
- data/lib/grape/validations/types.rb +198 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +19 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +25 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +18 -0
- data/lib/grape/validations/validators/base.rb +158 -0
- data/lib/grape/validations/validators/coerce_validator.rb +55 -0
- data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
- data/lib/grape/validations/validators/default_validator.rb +31 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +24 -0
- data/lib/grape/validations/validators/except_values_validator.rb +33 -0
- data/lib/grape/validations/validators/length_validator.rb +48 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +34 -0
- data/lib/grape/validations/validators/mutually_exclusive_validator.rb +18 -0
- data/lib/grape/validations/validators/presence_validator.rb +17 -0
- data/lib/grape/validations/validators/regexp_validator.rb +24 -0
- data/lib/grape/validations/validators/same_as_validator.rb +20 -0
- data/lib/grape/validations/validators/values_validator.rb +68 -0
- data/lib/grape/validations.rb +10 -226
- data/lib/grape/version.rb +4 -1
- data/lib/grape/xml.rb +10 -0
- data/lib/grape.rb +79 -77
- metadata +160 -306
- data/.gitignore +0 -38
- data/.rspec +0 -2
- data/.travis.yml +0 -9
- data/.yardopts +0 -2
- data/Gemfile +0 -19
- data/Guardfile +0 -15
- data/Rakefile +0 -64
- data/lib/grape/exceptions/missing_option.rb +0 -13
- data/lib/grape/exceptions/unknown_options.rb +0 -14
- data/lib/grape/http/request.rb +0 -28
- data/lib/grape/middleware/auth/basic.rb +0 -30
- data/lib/grape/middleware/auth/digest.rb +0 -30
- data/lib/grape/middleware/auth/oauth2.rb +0 -72
- data/lib/grape/route.rb +0 -23
- data/lib/grape/util/content_types.rb +0 -17
- data/lib/grape/util/deep_merge.rb +0 -23
- data/lib/grape/util/hash_stack.rb +0 -120
- data/lib/grape/validations/coerce.rb +0 -62
- data/lib/grape/validations/default.rb +0 -24
- data/lib/grape/validations/presence.rb +0 -12
- data/lib/grape/validations/regexp.rb +0 -14
- data/spec/grape/api_spec.rb +0 -2129
- data/spec/grape/endpoint_spec.rb +0 -605
- data/spec/grape/entity_spec.rb +0 -281
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -18
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -18
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -24
- data/spec/grape/exceptions/missing_option_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -18
- data/spec/grape/middleware/auth/basic_spec.rb +0 -31
- data/spec/grape/middleware/auth/digest_spec.rb +0 -47
- data/spec/grape/middleware/auth/oauth2_spec.rb +0 -90
- data/spec/grape/middleware/base_spec.rb +0 -63
- data/spec/grape/middleware/error_spec.rb +0 -49
- data/spec/grape/middleware/exception_spec.rb +0 -164
- data/spec/grape/middleware/formatter_spec.rb +0 -244
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -121
- data/spec/grape/middleware/versioner/header_spec.rb +0 -277
- data/spec/grape/middleware/versioner/param_spec.rb +0 -58
- data/spec/grape/middleware/versioner/path_spec.rb +0 -44
- data/spec/grape/middleware/versioner_spec.rb +0 -22
- data/spec/grape/util/hash_stack_spec.rb +0 -133
- data/spec/grape/validations/coerce_spec.rb +0 -143
- data/spec/grape/validations/default_spec.rb +0 -67
- data/spec/grape/validations/presence_spec.rb +0 -144
- data/spec/grape/validations/regexp_spec.rb +0 -33
- data/spec/grape/validations/zh-CN.yml +0 -9
- data/spec/grape/validations_spec.rb +0 -217
- data/spec/shared/versioning_examples.rb +0 -121
- data/spec/spec_helper.rb +0 -25
- data/spec/support/basic_auth_encode_helpers.rb +0 -4
- data/spec/support/versioned_helpers.rb +0 -48
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Validations
|
|
5
|
+
module Validators
|
|
6
|
+
class MultipleParamsBase < Base
|
|
7
|
+
def validate!(params)
|
|
8
|
+
attributes = MultipleAttributesIterator.new(@attrs, @scope, params)
|
|
9
|
+
array_errors = []
|
|
10
|
+
|
|
11
|
+
attributes.each do |resource_params|
|
|
12
|
+
validate_params!(resource_params)
|
|
13
|
+
rescue Grape::Exceptions::Validation => e
|
|
14
|
+
array_errors << e
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def keys_in_common(resource_params, known_keys = all_keys)
|
|
23
|
+
return [] unless hash_like?(resource_params)
|
|
24
|
+
|
|
25
|
+
known_keys & resource_params.keys.map! { |attr| @scope.full_name(attr) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def all_keys
|
|
29
|
+
@attrs.map { |attr| @scope.full_name(attr) }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Validations
|
|
5
|
+
module Validators
|
|
6
|
+
class MutuallyExclusiveValidator < MultipleParamsBase
|
|
7
|
+
default_message_key :mutual_exclusion
|
|
8
|
+
|
|
9
|
+
def validate_params!(params)
|
|
10
|
+
keys = keys_in_common(params)
|
|
11
|
+
return if keys.length <= 1
|
|
12
|
+
|
|
13
|
+
validation_error!(keys)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Validations
|
|
5
|
+
module Validators
|
|
6
|
+
class PresenceValidator < Base
|
|
7
|
+
default_message_key :presence
|
|
8
|
+
|
|
9
|
+
def validate_param!(attr_name, params)
|
|
10
|
+
return if hash_like?(params) && params.key?(attr_name)
|
|
11
|
+
|
|
12
|
+
validation_error!(attr_name)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Validations
|
|
5
|
+
module Validators
|
|
6
|
+
class RegexpValidator < Base
|
|
7
|
+
default_message_key :regexp
|
|
8
|
+
|
|
9
|
+
def initialize(attrs, options, required, scope, opts)
|
|
10
|
+
super
|
|
11
|
+
@value = option_value
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def validate_param!(attr_name, params)
|
|
15
|
+
return unless hash_like?(params) && params.key?(attr_name)
|
|
16
|
+
|
|
17
|
+
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || scrub(param.to_s).match?(@value) }
|
|
18
|
+
|
|
19
|
+
validation_error!(attr_name)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Validations
|
|
5
|
+
module Validators
|
|
6
|
+
class SameAsValidator < Base
|
|
7
|
+
def initialize(attrs, options, required, scope, opts)
|
|
8
|
+
super
|
|
9
|
+
@value = option_value
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def validate_param!(attr_name, params)
|
|
13
|
+
return if params[attr_name] == params[@value]
|
|
14
|
+
|
|
15
|
+
validation_error!(attr_name, message { translate(:same_as, parameter: @value) })
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Validations
|
|
5
|
+
module Validators
|
|
6
|
+
class ValuesValidator < Base
|
|
7
|
+
default_message_key :values
|
|
8
|
+
|
|
9
|
+
def initialize(attrs, options, required, scope, opts)
|
|
10
|
+
super
|
|
11
|
+
values = option_value
|
|
12
|
+
|
|
13
|
+
# Zero-arity procs return a collection per-request (e.g. DB-backed lists).
|
|
14
|
+
# Non-zero-arity procs are per-element predicates, called directly at validation time.
|
|
15
|
+
# Non-Proc values are wrapped in a zero-arity lambda for a uniform call interface.
|
|
16
|
+
if values.is_a?(Proc)
|
|
17
|
+
@values_call = values
|
|
18
|
+
@values_is_predicate = !values.arity.zero?
|
|
19
|
+
else
|
|
20
|
+
@values_call = -> { values }
|
|
21
|
+
@values_is_predicate = false
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def validate_param!(attr_name, params)
|
|
26
|
+
return unless hash_like?(params)
|
|
27
|
+
|
|
28
|
+
val = scrub(params[attr_name])
|
|
29
|
+
|
|
30
|
+
return if val.nil? && !required_for_root_scope?
|
|
31
|
+
return if val != false && val.blank? && @allow_blank
|
|
32
|
+
return if check_values?(val, attr_name)
|
|
33
|
+
|
|
34
|
+
validation_error!(attr_name)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def check_values?(val, attr_name)
|
|
40
|
+
param_array = val.nil? ? [nil] : Array.wrap(val)
|
|
41
|
+
|
|
42
|
+
if @values_is_predicate
|
|
43
|
+
begin
|
|
44
|
+
param_array.all? { |param| @values_call.call(param) }
|
|
45
|
+
rescue StandardError => e
|
|
46
|
+
warn "Error '#{e}' raised while validating attribute '#{attr_name}'"
|
|
47
|
+
false
|
|
48
|
+
end
|
|
49
|
+
else
|
|
50
|
+
values = @values_call.call
|
|
51
|
+
return true if values.nil?
|
|
52
|
+
|
|
53
|
+
param_array.all? { |param| values.include?(param) }
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def required_for_root_scope?
|
|
58
|
+
return false unless @required
|
|
59
|
+
|
|
60
|
+
scope = @scope
|
|
61
|
+
scope = scope.parent while scope.lateral?
|
|
62
|
+
|
|
63
|
+
scope.root?
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
data/lib/grape/validations.rb
CHANGED
|
@@ -1,237 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Grape
|
|
3
4
|
module Validations
|
|
5
|
+
extend Grape::Util::Registry
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
# All validators must inherit from this class.
|
|
7
|
-
#
|
|
8
|
-
class Validator
|
|
9
|
-
attr_reader :attrs
|
|
10
|
-
|
|
11
|
-
def initialize(attrs, options, required, scope)
|
|
12
|
-
@attrs = Array(attrs)
|
|
13
|
-
@required = required
|
|
14
|
-
@scope = scope
|
|
15
|
-
|
|
16
|
-
if options.is_a?(Hash) && !options.empty?
|
|
17
|
-
raise Grape::Exceptions.UnknownOptions.new(options.keys)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def validate!(params)
|
|
22
|
-
attributes = AttributesIterator.new(self, @scope, params)
|
|
23
|
-
attributes.each do |resource_params, attr_name|
|
|
24
|
-
if @required || resource_params.has_key?(attr_name)
|
|
25
|
-
validate_param!(attr_name, resource_params)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
class AttributesIterator
|
|
31
|
-
include Enumerable
|
|
32
|
-
|
|
33
|
-
def initialize(validator, scope, params)
|
|
34
|
-
@attrs = validator.attrs
|
|
35
|
-
@params = scope.params(params)
|
|
36
|
-
@params = (@params.is_a?(Array) ? @params : [@params])
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def each
|
|
40
|
-
@params.each do |resource_params|
|
|
41
|
-
@attrs.each do |attr_name|
|
|
42
|
-
yield resource_params, attr_name
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
private
|
|
49
|
-
|
|
50
|
-
def self.convert_to_short_name(klass)
|
|
51
|
-
ret = klass.name.gsub(/::/, '/').
|
|
52
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
53
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
54
|
-
tr("-", "_").
|
|
55
|
-
downcase
|
|
56
|
-
File.basename(ret, '_validator')
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
##
|
|
61
|
-
# Base class for all validators taking only one param.
|
|
62
|
-
class SingleOptionValidator < Validator
|
|
63
|
-
def initialize(attrs, options, required, scope)
|
|
64
|
-
@option = options
|
|
65
|
-
super
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
end
|
|
7
|
+
module_function
|
|
69
8
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
class Validator
|
|
73
|
-
def self.inherited(klass)
|
|
74
|
-
short_name = convert_to_short_name(klass)
|
|
75
|
-
Validations::register_validator(short_name, klass)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
9
|
+
def require_validator(short_name)
|
|
10
|
+
raise Grape::Exceptions::UnknownValidator, short_name unless registry.key?(short_name)
|
|
78
11
|
|
|
79
|
-
|
|
80
|
-
attr_accessor :validators
|
|
12
|
+
registry[short_name]
|
|
81
13
|
end
|
|
82
14
|
|
|
83
|
-
|
|
15
|
+
def build_short_name(klass)
|
|
16
|
+
return if klass.name.blank?
|
|
84
17
|
|
|
85
|
-
|
|
86
|
-
validators[short_name] = klass
|
|
18
|
+
klass.name.demodulize.underscore.delete_suffix('_validator')
|
|
87
19
|
end
|
|
88
|
-
|
|
89
|
-
class ParamsScope
|
|
90
|
-
attr_accessor :element, :parent
|
|
91
|
-
|
|
92
|
-
def initialize(api, element, parent, &block)
|
|
93
|
-
@element = element
|
|
94
|
-
@parent = parent
|
|
95
|
-
@api = api
|
|
96
|
-
@declared_params = []
|
|
97
|
-
|
|
98
|
-
instance_eval(&block)
|
|
99
|
-
|
|
100
|
-
configure_declared_params
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def requires(*attrs)
|
|
104
|
-
validations = {:presence => true}
|
|
105
|
-
if attrs.last.is_a?(Hash)
|
|
106
|
-
validations.merge!(attrs.pop)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
push_declared_params(attrs)
|
|
110
|
-
validates(attrs, validations)
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def optional(*attrs)
|
|
114
|
-
validations = {}
|
|
115
|
-
if attrs.last.is_a?(Hash)
|
|
116
|
-
validations.merge!(attrs.pop)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
push_declared_params(attrs)
|
|
120
|
-
validates(attrs, validations)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def group(element, &block)
|
|
124
|
-
ParamsScope.new(@api, element, self, &block)
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
def params(params)
|
|
128
|
-
params = @parent.params(params) if @parent
|
|
129
|
-
params = params[@element] || {} if @element
|
|
130
|
-
params
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def full_name(name)
|
|
134
|
-
return "#{@parent.full_name(@element)}[#{name}]" if @parent
|
|
135
|
-
name.to_s
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
protected
|
|
139
|
-
|
|
140
|
-
def push_declared_params(attrs)
|
|
141
|
-
@declared_params.concat attrs
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
private
|
|
145
|
-
|
|
146
|
-
# Pushes declared params to parent or settings
|
|
147
|
-
def configure_declared_params
|
|
148
|
-
if @parent
|
|
149
|
-
@parent.push_declared_params [element => @declared_params]
|
|
150
|
-
else
|
|
151
|
-
@api.settings.peek[:declared_params] ||= []
|
|
152
|
-
@api.settings[:declared_params].concat @declared_params
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def validates(attrs, validations)
|
|
157
|
-
doc_attrs = { :required => validations.keys.include?(:presence) }
|
|
158
|
-
|
|
159
|
-
# special case (type = coerce)
|
|
160
|
-
if validations[:type]
|
|
161
|
-
validations[:coerce] = validations.delete(:type)
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
if coerce_type = validations[:coerce]
|
|
165
|
-
doc_attrs[:type] = coerce_type.to_s
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
if desc = validations.delete(:desc)
|
|
169
|
-
doc_attrs[:desc] = desc
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
if default = validations[:default]
|
|
173
|
-
doc_attrs[:default] = default
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
full_attrs = attrs.collect{ |name| { :name => name, :full_name => full_name(name)} }
|
|
177
|
-
@api.document_attribute(full_attrs, doc_attrs)
|
|
178
|
-
|
|
179
|
-
# Validate for presence before any other validators
|
|
180
|
-
if validations.has_key?(:presence) && validations[:presence]
|
|
181
|
-
validate('presence', validations[:presence], attrs, doc_attrs)
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
# Before we run the rest of the validators, lets handle
|
|
185
|
-
# whatever coercion so that we are working with correctly
|
|
186
|
-
# type casted values
|
|
187
|
-
if validations.has_key? :coerce
|
|
188
|
-
validate('coerce', validations[:coerce], attrs, doc_attrs)
|
|
189
|
-
validations.delete(:coerce)
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
validations.each do |type, options|
|
|
193
|
-
validate(type, options, attrs, doc_attrs)
|
|
194
|
-
end
|
|
195
|
-
end
|
|
196
|
-
|
|
197
|
-
def validate(type, options, attrs, doc_attrs)
|
|
198
|
-
validator_class = Validations::validators[type.to_s]
|
|
199
|
-
|
|
200
|
-
if validator_class
|
|
201
|
-
(@api.settings.peek[:validations] ||= []) << validator_class.new(attrs, options, doc_attrs[:required], self)
|
|
202
|
-
else
|
|
203
|
-
raise Grape::Exceptions::UnknownValidator.new(type)
|
|
204
|
-
end
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
# This module is mixed into the API Class.
|
|
210
|
-
module ClassMethods
|
|
211
|
-
def reset_validations!
|
|
212
|
-
settings.peek[:declared_params] = []
|
|
213
|
-
settings.peek[:validations] = []
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
def params(&block)
|
|
217
|
-
ParamsScope.new(self, nil, nil, &block)
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
def document_attribute(names, opts)
|
|
221
|
-
@last_description ||= {}
|
|
222
|
-
@last_description[:params] ||= {}
|
|
223
|
-
Array(names).each do |name|
|
|
224
|
-
@last_description[:params][name[:full_name].to_s] ||= {}
|
|
225
|
-
@last_description[:params][name[:full_name].to_s].merge!(opts)
|
|
226
|
-
end
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
end
|
|
230
|
-
|
|
231
20
|
end
|
|
232
21
|
end
|
|
233
|
-
|
|
234
|
-
# Load all defined validations.
|
|
235
|
-
Dir[File.expand_path('../validations/*.rb', __FILE__)].each do |path|
|
|
236
|
-
require(path)
|
|
237
|
-
end
|
data/lib/grape/version.rb
CHANGED
data/lib/grape/xml.rb
ADDED
data/lib/grape.rb
CHANGED
|
@@ -1,90 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'logger'
|
|
2
|
-
require '
|
|
3
|
-
require '
|
|
4
|
-
require '
|
|
5
|
-
require '
|
|
6
|
-
require '
|
|
7
|
-
require '
|
|
8
|
-
require '
|
|
4
|
+
require 'active_support'
|
|
5
|
+
require 'active_support/version'
|
|
6
|
+
require 'active_support/isolated_execution_state'
|
|
7
|
+
require 'active_support/core_ext/array/conversions' # to_xml
|
|
8
|
+
require 'active_support/core_ext/array/wrap'
|
|
9
|
+
require 'active_support/core_ext/hash/conversions' # to_xml
|
|
10
|
+
require 'active_support/core_ext/hash/deep_merge'
|
|
11
|
+
require 'active_support/core_ext/hash/deep_transform_values'
|
|
9
12
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
10
|
-
require 'active_support/
|
|
11
|
-
require 'active_support/core_ext/
|
|
12
|
-
require 'active_support/core_ext/
|
|
13
|
-
require '
|
|
14
|
-
require '
|
|
15
|
-
require '
|
|
16
|
-
require '
|
|
17
|
-
require '
|
|
18
|
-
require '
|
|
13
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
|
14
|
+
require 'active_support/core_ext/module/delegation' # delegate_missing_to
|
|
15
|
+
require 'active_support/core_ext/object/blank'
|
|
16
|
+
require 'active_support/core_ext/object/deep_dup'
|
|
17
|
+
require 'active_support/core_ext/object/duplicable'
|
|
18
|
+
require 'active_support/deprecation'
|
|
19
|
+
require 'active_support/inflector'
|
|
20
|
+
require 'active_support/ordered_options'
|
|
21
|
+
require 'active_support/notifications'
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
autoload :MissingMimeType, 'grape/exceptions/missing_mime_type'
|
|
36
|
-
autoload :MissingOption, 'grape/exceptions/missing_option'
|
|
37
|
-
autoload :InvalidFormatter, 'grape/exceptions/invalid_formatter'
|
|
38
|
-
autoload :InvalidVersionerOption, 'grape/exceptions/invalid_versioner_option'
|
|
39
|
-
autoload :UnknownValidator, 'grape/exceptions/unknown_validator'
|
|
40
|
-
autoload :UnknownOptions, 'grape/exceptions/unknown_options'
|
|
41
|
-
autoload :InvalidWithOptionForRepresent, 'grape/exceptions/invalid_with_option_for_represent'
|
|
42
|
-
end
|
|
23
|
+
require 'English'
|
|
24
|
+
require 'bigdecimal'
|
|
25
|
+
require 'date'
|
|
26
|
+
require 'dry-types'
|
|
27
|
+
require 'dry-configurable'
|
|
28
|
+
require 'forwardable'
|
|
29
|
+
require 'json'
|
|
30
|
+
require 'mustermann/grape'
|
|
31
|
+
require 'pathname'
|
|
32
|
+
require 'rack'
|
|
33
|
+
require 'rack/auth/basic'
|
|
34
|
+
require 'rack/builder'
|
|
35
|
+
require 'rack/head'
|
|
36
|
+
require 'singleton'
|
|
37
|
+
require 'zeitwerk'
|
|
43
38
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
loader = Zeitwerk::Loader.for_gem
|
|
40
|
+
loader.inflector.inflect(
|
|
41
|
+
'api' => 'API',
|
|
42
|
+
'dsl' => 'DSL'
|
|
43
|
+
)
|
|
44
|
+
railtie = "#{__dir__}/grape/railtie.rb"
|
|
45
|
+
loader.do_not_eager_load(railtie)
|
|
46
|
+
loader.setup
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
autoload :Base, 'grape/formatter/base'
|
|
53
|
-
autoload :Json, 'grape/formatter/json'
|
|
54
|
-
autoload :SerializableHash, 'grape/formatter/serializable_hash'
|
|
55
|
-
autoload :Txt, 'grape/formatter/txt'
|
|
56
|
-
autoload :Xml, 'grape/formatter/xml'
|
|
57
|
-
end
|
|
48
|
+
I18n.load_path << File.expand_path('grape/locale/en.yml', __dir__)
|
|
58
49
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
autoload :Json, 'grape/parser/json'
|
|
62
|
-
autoload :Xml, 'grape/parser/xml'
|
|
63
|
-
end
|
|
50
|
+
module Grape
|
|
51
|
+
extend Dry::Configurable
|
|
64
52
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
autoload :Versioner, 'grape/middleware/versioner'
|
|
68
|
-
autoload :Formatter, 'grape/middleware/formatter'
|
|
69
|
-
autoload :Error, 'grape/middleware/error'
|
|
53
|
+
setting :param_builder, default: :hash_with_indifferent_access
|
|
54
|
+
setting :lint, default: false
|
|
70
55
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
56
|
+
HTTP_SUPPORTED_METHODS = [
|
|
57
|
+
Rack::GET,
|
|
58
|
+
Rack::POST,
|
|
59
|
+
Rack::PUT,
|
|
60
|
+
Rack::PATCH,
|
|
61
|
+
Rack::DELETE,
|
|
62
|
+
Rack::HEAD,
|
|
63
|
+
Rack::OPTIONS
|
|
64
|
+
].freeze
|
|
76
65
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
66
|
+
# Rack errors that should be rescued and wrapped as Grape::Exceptions::RequestError.
|
|
67
|
+
# Rack 3.1.0 introduced Rack::BadRequest as a marker module included by all bad request
|
|
68
|
+
# exception classes, allowing a single rescue entry to cover them all.
|
|
69
|
+
# Before, these errors are raised as individual exception classes.
|
|
70
|
+
RACK_ERRORS =
|
|
71
|
+
if Gem::Version.new(Rack.release) >= Gem::Version.new('3.1.0')
|
|
72
|
+
[EOFError, Rack::BadRequest]
|
|
73
|
+
else
|
|
74
|
+
[
|
|
75
|
+
EOFError,
|
|
76
|
+
Rack::Multipart::MultipartPartLimitError,
|
|
77
|
+
Rack::Multipart::MultipartTotalPartLimitError,
|
|
78
|
+
Rack::Utils::ParameterTypeError,
|
|
79
|
+
Rack::Utils::InvalidParameterError,
|
|
80
|
+
Rack::QueryParser::ParamsTooDeepError
|
|
81
|
+
]
|
|
82
|
+
end.freeze
|
|
84
83
|
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
def self.deprecator
|
|
85
|
+
@deprecator ||= ActiveSupport::Deprecation.new('2.0', 'Grape')
|
|
87
86
|
end
|
|
88
87
|
end
|
|
89
88
|
|
|
90
|
-
|
|
89
|
+
# https://api.rubyonrails.org/classes/ActiveSupport/Deprecation.html
|
|
90
|
+
# adding Grape.deprecator to Rails App if any
|
|
91
|
+
require 'grape/railtie' if defined?(Rails::Railtie)
|
|
92
|
+
loader.eager_load
|