forked from adiwg/mdEditor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray-required.js
75 lines (65 loc) · 1.78 KB
/
array-required.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { assert } from '@ember/debug';
import { get } from '@ember/object';
import { isArray } from '@ember/array';
import BaseValidator from 'ember-cp-validations/validators/base';
const ArrayRequired = BaseValidator.extend({
/**
* Validation that checks array length
*
* @module mdeditor
* @submodule validator
* @class array-required
* @extends BaseValidator
* @example
* validator('array-required', {
track: ['type']
})
*/
/**
* Validate the array
*
* @method validate
* @param {Array} value The array to test
* @param {Object} options
* @return {Mixed} True or error message
*
*/
validate(value, options) {
if(isArray(value)) {
if(value.length) {
return true;
}
}
options.item = this.options.description || this.options.attribute;
return this.createErrorMessage('arrayRequired', value, options);
}
});
ArrayRequired.reopenClass({
/**
* Define attribute specific dependent keys for your validator
*
* [
* `model.array.@each.${attribute}` --> Dependent is created on the model's context
* `${attribute}.isValid` --> Dependent is created on the `model.validations.attrs` context
* ]
*
* @property getDependentsFor
* @param {String} attribute The attribute being evaluated
* @param {Unknown} options Options passed into your validator
* @return {Array}
*/
getDependentsFor(attribute, options) {
//return[];
let track = [];
let opts = get(options, 'track');
assert(
`[validator:array-valid] [${attribute}] option 'track' must be an array`,
isArray(opts));
if(!isArray(opts)) return track;
opts.forEach((itm) => {
track.push(`model.${attribute}.@each.${itm}`);
});
return track;
}
});
export default ArrayRequired;