Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autoload types and validators #2222

Merged
merged 8 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#2222](/~https://github.com/ruby-grape/grape/pull/2222): autoload types and validators - [@ericproulx](/~https://github.com/ericproulx).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize Autoload

* Your contribution here.

### 1.6.2 (2021/12/30)
Expand Down
83 changes: 57 additions & 26 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
require 'active_support'
require 'active_support/version'
require 'active_support/isolated_execution_state' if ActiveSupport::VERSION::MAJOR > 6
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/object/blank'
require 'active_support/dependencies/autoload'
require 'active_support/notifications'
require 'i18n'
Expand All @@ -45,6 +45,7 @@ module Grape
autoload :Env, 'grape/util/env'
autoload :Json, 'grape/util/json'
autoload :Xml, 'grape/util/xml'
autoload :DryTypes
end

module Http
Expand Down Expand Up @@ -218,6 +219,57 @@ module ServeStream
autoload :StreamResponse
end
end

module Validations
extend ::ActiveSupport::Autoload

eager_autoload do
autoload :AttributesIterator
autoload :MultipleAttributesIterator
autoload :SingleAttributeIterator
autoload :Types
autoload :ParamsScope
autoload :ValidatorFactory
end

module Types
extend ::ActiveSupport::Autoload

eager_autoload do
autoload :InvalidValue
autoload :DryTypeCoercer
autoload :ArrayCoercer
autoload :SetCoercer
autoload :PrimitiveCoercer
autoload :CustomTypeCoercer
autoload :CustomTypeCollectionCoercer
autoload :MultipleTypeCoercer
autoload :VariantCollectionCoercer
end
end

module Validators
extend ::ActiveSupport::Autoload

eager_autoload do
autoload :Base
autoload :MultipleParamsBase
autoload :AllOrNoneOfValidator
autoload :AllowBlankValidator
autoload :AsValidator
autoload :AtLeastOneOfValidator
autoload :CoerceValidator
autoload :DefaultValidator
autoload :ExactlyOneOfValidator
autoload :ExceptValuesValidator
autoload :MutualExclusionValidator
autoload :PresenceValidator
autoload :RegexpValidator
autoload :SameAsValidator
autoload :ValuesValidator
end
end
end
end

require 'grape/config'
Expand All @@ -227,25 +279,4 @@ module ServeStream
require 'grape/util/lazy_block'
require 'grape/util/endpoint_configuration'

require 'grape/validations/validators/base'
require 'grape/validations/attributes_iterator'
require 'grape/validations/single_attribute_iterator'
require 'grape/validations/multiple_attributes_iterator'
require 'grape/validations/validators/allow_blank'
require 'grape/validations/validators/as'
require 'grape/validations/validators/at_least_one_of'
require 'grape/validations/validators/coerce'
require 'grape/validations/validators/default'
require 'grape/validations/validators/exactly_one_of'
require 'grape/validations/validators/mutual_exclusion'
require 'grape/validations/validators/presence'
require 'grape/validations/validators/regexp'
require 'grape/validations/validators/same_as'
require 'grape/validations/validators/values'
require 'grape/validations/validators/except_values'
require 'grape/validations/params_scope'
require 'grape/validations/validators/all_or_none'
require 'grape/validations/types'
require 'grape/validations/validator_factory'

require 'grape/version'
12 changes: 12 additions & 0 deletions lib/grape/dry_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'dry-types'

module Grape
module DryTypes
# Call +Dry.Types()+ to add all registered types to +DryTypes+ which is
# a container in this case. Check documentation for more information
# https://dry-rb.org/gems/dry-types/1.2/getting-started/
include Dry.Types()
end
end
2 changes: 2 additions & 0 deletions lib/grape/util/json.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'json'

module Grape
if Object.const_defined? :MultiJson
Json = ::MultiJson
Expand Down
28 changes: 16 additions & 12 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# frozen_string_literal: true

require 'grape/validations/attributes_iterator'
require 'grape/validations/single_attribute_iterator'
require 'grape/validations/multiple_attributes_iterator'
require 'grape/validations/params_scope'
require 'grape/validations/types'

module Grape
# Registry to store and locate known Validators.
module Validations
class << self
attr_accessor :validators
end
module_function

self.validators = {}
def validators
@validators ||= {}
end

# Register a new validator, so it can be used to validate parameters.
# @param short_name [String] all lower-case, no spaces
# @param klass [Class] the validator class. Should inherit from
# Validations::Base.
def self.register_validator(short_name, klass)
def register_validator(short_name, klass)
validators[short_name] = klass
end

def self.deregister_validator(short_name)
def deregister_validator(short_name)
validators.delete(short_name)
end

# Find a validator and if not found will try to load it
def require_validator(short_name)
str_name = short_name.to_s
validators.fetch(str_name) do
Grape::Validations::Validators.const_get("#{str_name.camelize}Validator")
end
rescue NameError
raise Grape::Exceptions::UnknownValidator.new(short_name)
end
end
end
6 changes: 1 addition & 5 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,13 @@ def check_incompatible_option_values(default, values, except_values, excepts)
end

def validate(type, options, attrs, doc_attrs, opts)
validator_class = Validations.validators[type.to_s]

raise Grape::Exceptions::UnknownValidator.new(type) unless validator_class

validator_options = {
attributes: attrs,
options: options,
required: doc_attrs[:required],
params_scope: self,
opts: opts,
validator_class: validator_class
validator_class: Validations.require_validator(type)
}
@api.namespace_stackable(:validations, validator_options)
end
Expand Down
Loading