【发布时间】:2021-03-14 17:07:53
【问题描述】:
我很难掌握可能的基本概念。我正在传递一个location 作为道具。它有一个 json 列来存储additionalAttributes。它看起来像这样:
"additionalProperties": [
{
"integrations": [
{
"exampleVendor": {
"locationId": 123,
"positionId": 456
}
}
]
}
],
"createdAt": "",
"updatedAt": "",
...
以上是我硬编码到我的数据库 (Postgres) 中的内容,以尝试模拟数据返回时的样子。
我正在使用 vuelidate 文档的 validate collections 部分。
这是我用来尝试创建验证规则的内容:
validations: {
location: {
additionalProperties: {
$each: {
integrations: {
$each: {
exampleVendor: {
locationId: {required},
positionId: {required},
}
}
}
}
}
}
},
在我的模板中,我尝试像这样连接验证:
<select id="my-id"
name="my-id"
class="py-3 px-3 mt-1 block w-full pl-3 pr-10 py-2 text-base sm:text-sm rounded-md"
v-if="locations"
v-model.trim="$v.location.additionalProperties[0].integrations[0].exampleVendor.locationId.$model"
:class="[$v.location.additionalProperties[0].integrations[0].exampleVendor.locationId.$error ?
'focus:ring-red-500 focus:border-red-500 border-red-300' : 'focus:ring-gray-400 focus:border-gray-400 border-gray-300',]"
>
...
</select>
我已经使用这个组件有一段时间了,并且有一个很愚蠢的问题already asked。
我也担心设置这么死板的路径additionalProperties[0].integrations[0]真的很糟糕。
我担心这个问题不会落后太多,但现在是时候寻求一些建议了。感谢您的任何建议!
编辑
@tony19 很好地解释了为什么只使用第一个值的数组。也许有更好的方法来做我正在做的事情;这是我的数据库中的数据可能的外观的更广泛视图。它现在除了集成之外还有其他属性。不过,目前我只专注于此。
"additionalProperties": [
{
"integrations": [
{
"exampleVendor": {
"locationId": 123,
"positionId": 456
},
"anotherVendor": {
"foo": "abc",
"bar": "def"
},
"someOtherVendor": {
"thing": "value"
}
}
],
"anotherAttribute: {
"one": "two"
},
"possibleAttributes": [...]
}
],
【问题讨论】: