法拉第Json验证之前.post



目前正在使用法拉第对某些 API 执行一些 http 请求。 我想在.post的步骤验证正文+标题。 但是,在调用.post时,将设置并发送正文。

所以!我想知道是否可以在.post的步骤设置架构验证(我确实从 API 接收器端收到了 Json 架构验证(

我能够使用def call来拦截Faraday_Middleware,但是是否可以将请求转换为 Json 形式并对其进行验证?

宝石信息: 宝石faraday~> 0.9, 宝石faraday_middleware, 宝石jbuilder, 宝石json-schema

最初问这个问题时,我不清楚法拉第是如何工作的,在发送请求和获得响应方面。但现在更清楚的是,将请求转换为 JSON 形式并验证所有信息可能并不容易。

原因是,当法拉第发送信息时,信息包以对象(或连接(的形式捆绑在一起。您可以验证连接中信息的各个部分。目前没有简单的方法来验证整个连接。(您可以设置一些标头或正文合并 json 方法,然后一次验证两者。但是,如果是这种情况,为什么不单独验证以更准确(

底线是,如果我们想验证响应或整个请求,我们需要访问请求和响应各个字段(标头或正文(,并在中间件中单独验证它们。

为了设置中间件

Faraday.new(_proxy, _headers, _ssl) do |conn|
conn.request :json # <---- what you send
conn.response :xml,  :content_type => /bxml$/ .  # <---- what you received
conn.use _Validator_class, _parameter1, _parmeter2  
#Validator_class should is where actual middleware been setup
conn.adapter Faraday.default_adapter
#^ should aways set this. Even it is default_adapter. 
#  if you work in big project, other faraday gem might mess with this. 
#  so it is best to manually set the default.
end
class _Validator < Faraday::Middleware
method initialize(app = nil, _parameter1, _parameter2)
#blah #blah  <---- your code setup
super(app)
end 
method call(env)
#do your magic
after_call(env) <<---- required
end 
method after_call
# if you want to mess with out going information 
# you can do so right over here 
# but most of time just leave as 
@app.call(env).on_complete do |_env|
end
end
end
env.header <----- your header, in the format you packaged
env.body   <----- your body, same as header above
setup in the call method section for close up validation.

最新更新