/
presence.js
38 lines (32 loc) · 1.13 KB
/
presence.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
import buildMessage from 'ember-changeset-validations/utils/validation-errors';
import { validate } from 'ember-validators';
export default function validatePresence(options) {
let targets;
if (typeof options === 'boolean') {
options = { presence: options };
} else if (options && options.on !== undefined) {
if (typeof options.on === 'string') {
targets = [ options.on ];
} else if (Array.isArray(options.on)) {
targets = options.on;
}
delete options.on;
}
return (key, value, _oldValue, changes, content) => {
if (targets && !targets.some((target) => changes[target] || (changes[target] === undefined && content[target]))) {
return true;
}
let result = validate('presence', value, options, null, key);
if (typeof result === 'boolean' || typeof result === 'string') {
return result;
} else {
// We flipped the meaning behind `present` and `blank` so switch the two
if (result.type === 'present') {
result.type = 'blank';
} else if (result.type === 'blank') {
result.type = 'present';
}
return buildMessage(key, result);
}
};
}