adopted-ember-addons / adopted-ember-addons/ember-cp-validations

Validate array of objects

未关闭
#560 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
439
派生
172
PR 合并指标
30 天内没有已合并 PR

描述

### Environment
- Ember CP Validations Version: 3.5.1

How would I go about validating an array of objects from the model? Validating profile.address nested attrs works fine. Not sure how to go about validating name and email for each friend?

I have the following example:

Data:
```
profile: {
friends: [{
name: 'bob',
email: 'bob@email.com'
}, {
name: 'sue',
email: 'sue@email.com'
}],
address: {
street: '10 Bash St',
town: 'Dundee'
}
}
```

```
import Model from 'ember-data/model';
import attr from 'ember-data/attr';

import { computed } from '@ember/object';

import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
name: [
validator('presence', true),
validator('length', {
min: 4
})
],
'profile.address.street': [
validator('presence', true),
validator('length', {
min: 4
})
],
'profile.address.town': [
validator('presence', true),
validator('length', {
min: 4
})
]
});

export default Model.extend(Validations, {

// Attributes

name: attr('string'),
// custom transform
profile: attr('object', {
defaultValue() {
return {
friends: [],
address: {}
}
}
}),

// Associations

// Computed Properties

isFormValid: computed.alias('validations.isValid')

});
```

贡献指南

打开贡献指南

调研方向

Start with the model example in the issue, especially the existing nested profile.address validations and the profile.friends array. Check the validation API and related tests for nested object or array-item validation, then establish whether the expected outcome is documented configuration or a library change that validates each friend's name and email.

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript
领域
frontend
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。