{ "name"=>"Name", "duratio-6ren">
gpt4 book ai didi

ruby - 如何在 Grape 中定义哈希数组?

转载 作者:数据小太阳 更新时间:2023-10-29 07:13:22 24 4
gpt4 key购买 nike

我使用 Ember 作为我的前端和 Grape API 来为我的 API 提供服务。

前端发送类似:

{
"service"=>{
"name"=>"Name",
"duration"=>"30",
"user"=>nil,
"organization"=>"org",
"category"=>nil,
"description"=>"description",
"disabled"=>true,
"color"=>nil,
"availabilities"=>[
{
"day"=>"Saturday",
"enabled"=>false,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Sunday",
"enabled"=>false,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Monday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
},
{
"startAt"=>"05:00 AM",
"endAt"=>"09:00 PM"
},
{
"startAt"=>"05:00 AM",
"endAt"=>"09:00 PM"
}
]
},
{
"day"=>"Tuesday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Wednesday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Thursday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Friday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
}
]
}
}

我正在努力实现 Grape api 接口(interface)来接收有效载荷,我最终得到了类似的东西:

  resource :services do
params do
requires :service, type: Hash do
requires :name, type: String
requires :organization, type: String
requires :duration, type: Integer
requires :category, type: String
optional :color, type: String
optional :description, type: String
optional :disabled, type: Boolean
requires :availabilities, type: Array do
requires :day, type: String
optional :enabled, type: Boolean
optional :timeSlots, type: Array do
requires :startAt, type: Time
requires :endtAt, type: Time
end
end
end
end
post do
puts params
end
end

但运气不好,因为我收到以下错误:

ERROR -- service[availabilities][0][timeSlots][0][endtAt] is missing

最佳答案

您可以尝试使用“组”来指示您的数组元素的类型。

示例(更新):

params do
group :services, type: Array, desc: "An array of services" do
requires :name, type: String
requires :organization, type: String
requires :duration, type: Integer
...
end
end
put '/test' do
s = []
params[:services].each do |service|
s << service
end
return s
end

关于ruby - 如何在 Grape 中定义哈希数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37712028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com