-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and extend coercion and type validation
Addresses #1164, #690, #689, #693. Depends on solnic/virtus#343 `Grape::ParameterTypes` is renamed `Grape::Validations::Types` to reflect that it should probably be bundled with an eventual `grape-validations` gem. It is expanded to include two new categories of types, 'special' and 'recognized' (see 'lib/grape/validations/types.rb'). `CoerceValidator` now makes use of `Virtus::Attribute::value_coerced?`, simplifying its internals. `CustomTypeCoercer` is introduced, attempting to standardize support for custom types by decoupling coercion and type-checking logic from the `type` class supplied to `Grape::Dsl::Parameters::requires`. The process for inferring which logic to use for each type and coercion method is encoded in `lib/grape/validations/types/build_coercer.rb`. `JSON`, `Array[JSON]` and `Rack::Multipart::UploadedFile (a.k.a `File`) are designated 'special' types, for which special implementations of `Virtus::Attribute` are provided. Instances of `Virtus::Attribute` built with `Virtus::Attribute.build` may now also be passed as the `type` parameter for `requires`. A number of pre-rolled attributes are available providing coercion for `Date` and `DateTime` objects from various formats in `lib/grape/validations/formats/date.rb` and `date_time.rb`.
- Loading branch information
Showing
20 changed files
with
834 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative 'formats/dates' | ||
require_relative 'formats/date_times' | ||
|
||
module Grape | ||
module Validations | ||
# Contains collections of constants that may be passed | ||
# as the +type+ parameter of {Grape::Dsl::Parameters#requires} | ||
# or {Grape::Dsl::Parameters#optional}, providing | ||
# parameter coercion from a range of standard formats | ||
# to a number of standard types. | ||
module Formats | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'time' | ||
require_relative '../types/custom_type_coercer' | ||
|
||
module Grape | ||
module Validations | ||
module Formats | ||
# This module provides a set of ready-made +Virtus::Attribute+ | ||
# constants, suitable for use as the +type+ option for | ||
# {Grape::Dsl::Parameters#requires} and {Grape::Dsl::Parameters#optional}. | ||
# These definitions will coerce input strings to the standard | ||
# ruby +DateTime+ type using the standard parsing methods defined | ||
# on that class. | ||
# | ||
# This module is not required by default. | ||
module DateTimes | ||
# Parses timestamps using +DateTime.httpdate+ | ||
HttpDate = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:httpdate)) | ||
|
||
# Parses timestamps using +DateTime.iso8601+ | ||
Iso8601 = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:iso8601)) | ||
|
||
# Parses timestamps using +DateTime.jisx0301+ | ||
Jisx0301 = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:jisx0301)) | ||
|
||
# Parses julian dates using +DateTime.jd+. | ||
# Time of day is not supported. | ||
JulianDay = Types::CustomTypeCoercer.build(::DateTime, lambda do |val| | ||
fail Grape::Exceptions::Validations, 'julian date must be an integer' unless val =~ /^\d*$/ | ||
|
||
::DateTime.jd val.to_i | ||
end) | ||
|
||
# Parses timestamps using +DateTime.rfc2822+ | ||
Rfc2822 = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:rfc2822)) | ||
|
||
# Parses timestamps using +DateTime.rfc3339+ | ||
Rfc3339 = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:rfc3339)) | ||
|
||
# Parses timestamps using +DateTime.rfc822+ | ||
Rfc822 = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:rfc822)) | ||
|
||
# Parses timestamps using +DateTime.xmlschema+ | ||
XmlSchema = Types::CustomTypeCoercer.build(::DateTime, ::DateTime.method(:xmlschema)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'date' | ||
require_relative '../types/custom_type_coercer' | ||
|
||
module Grape | ||
module Validations | ||
module Formats | ||
# This module provides a set of ready-made +Virtus::Attribute+ | ||
# constants, suitable for use as the +type+ option for | ||
# {Grape::Dsl::Parameters#requires} and {Grape::Dsl::Parameters#optional}. | ||
# These definitions will coerce input strings to the standard | ||
# ruby +Date+ type using the standard parsing methods defined | ||
# on that class. | ||
# | ||
# This module is not required by default. | ||
module Dates | ||
# Parses dates using +Date.httpdate+ | ||
HttpDate = Types::CustomTypeCoercer.build(::Date, ::Date.method(:httpdate)) | ||
|
||
# Parses dates using +Date.iso8601+ | ||
Iso8601 = Types::CustomTypeCoercer.build(::Date, ::Date.method(:iso8601)) | ||
|
||
# Parses dates using +Date.jisx0301+ | ||
Jisx0301 = Types::CustomTypeCoercer.build(::Date, ::Date.method(:jisx0301)) | ||
|
||
# Parses dates using +Date.jd+ | ||
JulianDay = Types::CustomTypeCoercer.build(::Date, lambda do |val| | ||
fail Grape::Exceptions::Validations, 'julian date must be an integer' unless val =~ /^\d*$/ | ||
|
||
::Date.jd val.to_i | ||
end) | ||
|
||
# Parses dates using +Date.rfc2822+ | ||
Rfc2822 = Types::CustomTypeCoercer.build(::Date, ::Date.method(:rfc2822)) | ||
|
||
# Parses dates using +Date.rfc3339+ | ||
Rfc3339 = Types::CustomTypeCoercer.build(::Date, ::Date.method(:rfc3339)) | ||
|
||
# Parses dates using +Date.rfc822+ | ||
Rfc822 = Types::CustomTypeCoercer.build(::Date, ::Date.method(:rfc822)) | ||
|
||
# Parses dates using +Date.xmlschema+ | ||
XmlSchema = Types::CustomTypeCoercer.build(::Date, ::Date.method(:xmlschema)) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.