Comments (4)
I can't believe we don't support this today! I think it's a new feature request.
One other idea of how we could express this is by allowing duplicate keys, but it's probably too weird.
requires :value, type: Hash do
requires :fixed_price, type: Float
end
requires :value, type: Hash do
requires :time_unit, type: String
requires :rate, type: Float
end
Another could use :as
.
requires :value_with_fixed_price, as: :value, type: Hash do
requires :fixed_price, type: Float
end
requires :value_with_time_and_rate, as: :value, type: Hash do
requires :time_unit, type: String
requires :rate, type: Float
end
I think extending types
as you suggest is the way to go.
from grape.
Or do we want to avoid having custom types and you want to be able to define
Hash
with specific properties directly when defining params? I think it overcomplicates the solution and it could be difficult to read.
This is the feature request: one shouldn't have to declare an entire custom type, just use a Hash
from grape.
@dblock Thanks for the fast response!
Using :as
I don't think will work as the whole point is that it is expected as :value
on the request contract regardless. I ran a quick test with:
optional :value_fixed, as: :value, type: Hash do
requires :fixed_price, type: Float
end
optional :value_labor_rate, as: :value, type: Hash do
requires :rate, type: Float
requires :time_unit, type: String
end
exactly_one_of :value_fixed, :value_labor_rate
and tried sending a request with
"value": {
"time_unit": "hour",
"rate": 10000
}
but it just gives "error": "value_fixed, value_labor_rate are missing, exactly one parameter must be provided
so that won't do.
For now I'll just deal with how it is but I'm happy this can be a feature request.
from grape.
Hey 👋
Is this similar to #2151 ?
I know they're not exactly the same, as #2151 is talking about different types using allowed values depending on the type. But I think having different Hashes containing different properties is like having different types too, right?
In #2151 , @dblock commented about the preference on allowing re-declaration that would be cumulative by default. He already did the same comment for this issue, so maybe is a solution for taking into account? For me is also weird to have multiple times the same property, because from my understanding the last one will win :) so I also prefer an approach for defining multiple types inside the array.
However, should this be solved by just passing custom value objects instead of Hash? Like explained here? https://github.com/ruby-grape/grape?tab=readme-ov-file#custom-types-and-coercions
require_relative 'fixed_price'
require_relative 'labor_rate'
params do
requires :value, desc: "TODO", types: [FixedPrice, LaborRate]
end
Where each value object is just a Ruby class that includes the parsed?
and parse
methods:
class FixedPrice
attr_accessor :fixed_price
def initialize(fixed_price)
@fixed_price = fixed_price
end
def to_h
{
fixed_price: @fixed_price
}
end
def self.parsed?(value)
keys = value.keys.sort
[:fixed_price].sort == keys
end
def self.parse(value)
if value["fixed_price"]
new(value["fixed_price"]).to_h
else
{}
end
end
end
class LaborRate
attr_accessor :rate, :time_unit
def initialize(rate, time_unit)
@rate = rate
@time_unit = time_unit
end
def to_h
{
rate: @rate,
time_unit: @time_unit
}
end
def self.parsed?(value)
keys = value.keys.sort
[:rate, :time_unit].sort == keys
end
def self.parse(value)
if value["rate"] && value["time_unit"]
new(value["rate"], value["time_unit"]).to_h
else
{}
end
end
end
Or do we want to avoid having custom types and you want to be able to define Hash
with specific properties directly when defining params? I think it overcomplicates the solution and it could be difficult to read.
from grape.
Related Issues (20)
- Route#options and Route#settings are confusing HOT 1
- Ruby 3.3 && cookiejar = ArgumentError HOT 1
- Use Rack's HeadersHash instead of {}
- endpoint method_missing vs override inspect HOT 2
- Values Validator => Proc's arity < 2 ? HOT 3
- Rails Edge requires Ruby >= 3.1
- Fix specs for Rack Edge
- Fix `params` warning method redefined
- Better tracking of gems deprecation
- Rack::Lint::Error => a header value must be a String or Array of Strings, but the value of 'content-type' is a NilClass HOT 2
- Recognize_path should account for HTTP method HOT 1
- Grape and Protecting Against Mass Assignment Abuse HOT 8
- Why do you convert PATH_INFO? Is there some RFC? HOT 2
- Add support for exclusive parameter groups (e.g. exactly_one_of) HOT 2
- Drop support rack ~> 1 ? HOT 2
- Replace Appraisals by eval_gemfile ? HOT 3
- DeprecatedConstantProxy without a deprecator is deprecated HOT 1
- Investigation memory allocations HOT 1
- Add a memory usage check
- Dropping builder as dependency ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grape.