阵列验证的干式验证i18n消息



假设我已经定义了这样的干式验证:

class ApplicationContract < Dry::Validation::Contract
config.messages.backend = :i18n
config.messages.load_paths << 'config/errors.yml'
params do
required(:todo).schema do
required(:title).filled(:string)
required(:items).array(:hash) do
required(:name).filled(:string)
end
end
end
end

这是我的配置/errors.yml:

vi:
dry_validation:
errors:
rules:
title:
filled?: 'phai duoc dien'
key?: 'ko dc trong'
items:
name:
key?: 'thieu name'
filled?: 'name rong'

在我的代码中,我使用它来验证我的数据:

my_json = create_my_json
v = ApplicationContract.new
result = v.call(my_json)
render json: result.errors(locale: :vi).to_h
  1. 如果my_json喜欢:{"标题":";,"项目":[{"name":"bbb";}]}

然后我得到了回应:

{
"todo": {
"title": [
"phai duoc dien"
]
}
}

你们可以看到我对字段标题的验证在locale vi 中运行良好

  1. 如果我的json喜欢:{"标题":"aa";,"项目":[{"name":"quot;}]}

则响应为:

{
"todo": {
"items": {
"0": {
"name": [
"translation missing: vi.dry_validation.errors.filled?"
]
}
}
}
}

验证仍然有效,但无法获取我的区域设置消息。它显示了警告";缺少翻译:vi.dry_validation.errors.filled"相反如何解决此问题?

终于拿到了。只需从config/errors.yml:中删除节点项

vi:
dry_validation:
errors:
rules:
title:
filled?: 'phai duoc dien'
key?: 'ko dc trong'
name:
key?: 'thieu name'
filled?: 'name rong'

最新更新