Vanilla JS is a fast, lightweight, cross-platform framework for building incredible, powerful JavaScript applications.
 
You're boring enough.
如果只要求简单的校验清洗,我觉得就没有必要搞那么复杂。
因为我目前也是按照 JSON API 规范去构建 API,我 Explore 了一下其它的参数校验清洗 gem,包括 strong_parameters, rails_param 感觉都不是很满意,hanami-validation 和 dry-validation 的逻辑表达能力实在太好了,于是就用了。
其实我也不知道这种方式好不好,不过就目前来看也还 OK
ApplicationController
class Api::BaseController < ActionController::API
  before_action :sanitize
  helper_method :sanitized_params
  private
  def sanitized_params
    @sanitized_params ||= begin
      sanitizer ? sanitization.output : request.parameters
    end
  end
  def sanitize
    render_errors(sanitization.messages, :bad_request) && return if sanitization && sanitization.failure?
  end
  def sanitization
    @sanitization ||= sanitizer ? sanitizer.call(request.parameters.to_h) : nil
  end
  def sanitizer
    @sanitizer ||= begin
      controller_without_suffix = request.controller_class.to_s.match(/^(.*)Controller$/)[1]
      action                    = params[:action].titleize
      "#{controller_without_suffix}::#{action}Validation".safe_constantize
    end
  end
end
A simple sanitizer: Posts::IndexValidation
class Api::V1::Posts::IndexValidation < Api::V1::Validation
  validations do
    optional(:sort).filled
    optional(:filter).filled
    optional(:page).schema do
      optional(:number).filled
      optional(:size).filled
    end
  end
end
PostsController
class Api::V1::PostsController < Api::BaseController
  def index
    # Use sanitized_params here
  end
end
很多人批评 hanami 将一个 action 独立成一个类的方式,但这其实赋予了 action 更大的灵活性。使用 rails_param,你的 action 很快就会充满各种参数过滤清洗逻辑,当然,你可以对其进行抽取分离,但是效果也并不是很好。反观 hanami-validation,一方面参数过滤清晰表达能力更强,另一方面能够跟 action 的逻辑进行有效分离。
推荐 hanami-validation 或者 dry-validation (其实他们是一伙的),至于怎样整合进入 Rails,我目前是在 application_controller 里面加入一个 before_action, 根据对应的 controller+action 按照某种约定查找对应的 validation,暴露一个 sanitized_params helper 方法给后续的 action 逻辑
好厉害
strong_params 还是弱了一点,hanami-validation 或者 dry-validation 对于参数的 sanitize 和验证更强。
params.require(:product).permit(:name, tags: [])
开一个 repo,不是 pro
可以问问希拉里
I think it's clear enough. Just follow the error messages and you'll find your way.

 
Client: 1.21
Engine: 1.18
你的 Docker Client 跟 Docker Engine 的版本不一致
 
You're boring enough.
#35 楼 @vincent Sounds like simple_command
Sounds like tag, not node / category
  
支持雨哥
#5 楼 @greatghoul You get it. Sentry is awesome.
Test Driven Design. 当你发现你很难给一个功能添加测试的时候,很可能你没有好的 OO Design。另外可靠的测试也是很重要,所以"Write a test for your test"  
#3 楼 @guyanbiao *nix pipe
params.dig(:data, :attributes, :name)