假设我已经定义了这样的干验证:
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
- 如果 my_json 喜欢: { "title": "", "items": [ { "name": "bbb" } ] }
然后我得到了回应:
{
"todo": {
"title": [
"phai duoc dien"
]
}
}
你们可以看到我对字段标题的验证适用于语言环境 vi
- 现在,如果我的 json 喜欢: { "title": "aa", "items": [ { "name": "" } ] }
那么响应是:
{
"todo": {
"items": {
"0": {
"name": [
"translation missing: vi.dry_validation.errors.filled?"
]
}
}
}
}
验证仍然有效,但无法获取我的语言环境消息。它显示警告“缺少翻译:vi.dry_validation.errors.filled?” 反而。我该如何解决这个问题?