-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChoice.js
More file actions
126 lines (99 loc) · 3.29 KB
/
Choice.js
File metadata and controls
126 lines (99 loc) · 3.29 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use strict";
const isObject = require("../utils/isObject");
const isArray = require("../utils/isArray");
const Constraint = require("../prototypes/Constraint");
const def = {
message: "The value you selected is not a valid choice.",
multipleMessage: "One or more of the given values is invalid.",
minMessage: "You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.",
maxMessage: "You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.",
multiple: false,
// choices : []
// max,
// min;
};
const Choice = function (opt, extra) {
Constraint.apply(this, arguments); // call super constructor.
this.setExtra(extra);
if (isArray(opt)) {
opt = {
choices: opt,
};
}
opt = Object.assign({}, def, opt);
if (!isArray(opt.choices) || opt.choices.length === 0) {
throw new Error(`Choice: choices have to be non empty list`);
}
this.setOptions(opt);
};
Choice.prototype = Object.create(Constraint.prototype);
Choice.prototype.constructor = Choice;
Choice.prototype.NO_SUCH_CHOICE_ERROR = "NO_SUCH_CHOICE_ERROR";
Choice.prototype.TOO_FEW_ERROR = "TOO_FEW_ERROR";
Choice.prototype.TOO_MANY_ERROR = "TOO_MANY_ERROR";
const promise = (extra, f) => {
if (extra && extra.stop) {
return Promise.reject("stop Choice" + f);
}
return Promise.resolve("resolve Choice" + f);
};
Choice.prototype.validate = function (value, context, path, extra) {
const opt = this.getOptions();
if (value === null) {
return Promise.resolve("Choice");
}
if (opt.multiple) {
if (isArray(value)) {
const count = value.length;
for (let i = 0, l = count; i < l; i += 1) {
if (opt.choices.indexOf(value[i]) === -1) {
context
.buildViolation(opt.multipleMessage)
.atPath(path + "." + i)
.setParameter("{{ value }}", value[i])
.setInvalidValue(value)
.setCode(Choice.prototype.NO_SUCH_CHOICE_ERROR)
.setExtra(extra)
.addViolation();
return promise(extra, 2);
}
}
if (typeof opt.min !== "undefined" && count < opt.min) {
context
.buildViolation(opt.minMessage)
.atPath(path)
.setParameter("{{ limit }}", opt.min)
.setPlural(opt.min === 1 ? 0 : 1)
.setInvalidValue(value)
.setCode(Choice.prototype.TOO_FEW_ERROR)
.setExtra(extra)
.addViolation();
return promise(extra, 2);
}
if (typeof opt.max !== "undefined" && count > opt.max) {
context
.buildViolation(opt.maxMessage)
.atPath(path)
.setParameter("{{ limit }}", opt.max)
.setPlural(opt.max === 1 ? 0 : 1)
.setInvalidValue(value)
.setCode(Choice.prototype.TOO_MANY_ERROR)
.setExtra(extra)
.addViolation();
return promise(extra, 3);
}
}
} else if (opt.choices.indexOf(value) === -1) {
context
.buildViolation(opt.message)
.atPath(path)
.setParameter("{{ value }}", value)
.setInvalidValue(value)
.setCode(Choice.prototype.NO_SUCH_CHOICE_ERROR)
.setExtra(extra)
.addViolation();
return promise(extra, 4);
}
return Promise.resolve("Choice1");
};
module.exports = Choice;