alternatives.js
4.0 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Load modules
var Hoek = require('hoek');
var Any = require('./any');
var Cast = require('./cast');
var Ref = require('./ref');
var Errors = require('./errors');
// Declare internals
var internals = {};
internals.Alternatives = function () {
Any.call(this);
this._type = 'alternatives';
this._invalids.remove(null);
this._inner.matches = [];
};
Hoek.inherits(internals.Alternatives, Any);
internals.Alternatives.prototype._base = function (value, state, options) {
var errors = [];
for (var i = 0, il = this._inner.matches.length; i < il; ++i) {
var item = this._inner.matches[i];
var schema = item.schema;
if (!schema) {
var failed = item.is._validate(item.ref(state.parent, options), null, options, state.parent).errors;
schema = failed ? item.otherwise : item.then;
if (!schema) {
continue;
}
}
var result = schema._validate(value, state, options);
if (!result.errors) { // Found a valid match
return result;
}
errors = errors.concat(result.errors);
}
return { errors: errors.length ? errors : Errors.create('alternatives.base', null, state, options) };
};
internals.Alternatives.prototype.try = function (/* schemas */) {
var schemas = Hoek.flatten(Array.prototype.slice.call(arguments));
Hoek.assert(schemas.length, 'Cannot add other alternatives without at least one schema');
var obj = this.clone();
for (var i = 0, il = schemas.length; i < il; ++i) {
var cast = Cast.schema(schemas[i]);
if (cast._refs.length) {
obj._refs = obj._refs.concat(cast._refs);
}
obj._inner.matches.push({ schema: cast });
}
return obj;
};
internals.Alternatives.prototype.when = function (ref, options) {
Hoek.assert(Ref.isRef(ref) || typeof ref === 'string', 'Invalid reference:', ref);
Hoek.assert(options, 'Missing options');
Hoek.assert(typeof options === 'object', 'Invalid options');
Hoek.assert(options.hasOwnProperty('is'), 'Missing "is" directive');
Hoek.assert(options.then !== undefined || options.otherwise !== undefined, 'options must have at least one of "then" or "otherwise"');
var obj = this.clone();
var is = Cast.schema(options.is);
if (options.is === null || !options.is.isJoi) {
// Only apply required if this wasn't already a schema, we'll suppose people know what they're doing
is = is.required();
}
var item = {
ref: Cast.ref(ref),
is: is,
then: options.then !== undefined ? Cast.schema(options.then) : undefined,
otherwise: options.otherwise !== undefined ? Cast.schema(options.otherwise) : undefined
};
Ref.push(obj._refs, item.ref);
obj._refs = obj._refs.concat(item.is._refs);
if (item.then && item.then._refs) {
obj._refs = obj._refs.concat(item.then._refs);
}
if (item.otherwise && item.otherwise._refs) {
obj._refs = obj._refs.concat(item.otherwise._refs);
}
obj._inner.matches.push(item);
return obj;
};
internals.Alternatives.prototype.describe = function () {
var description = Any.prototype.describe.call(this);
var alternatives = [];
for (var i = 0, il = this._inner.matches.length; i < il; ++i) {
var item = this._inner.matches[i];
if (item.schema) {
// try()
alternatives.push(item.schema.describe());
}
else {
// when()
var when = {
ref: item.ref.toString(),
is: item.is.describe()
};
if (item.then) {
when.then = item.then.describe();
}
if (item.otherwise) {
when.otherwise = item.otherwise.describe();
}
alternatives.push(when);
}
}
description.alternatives = alternatives;
return description;
};
module.exports = new internals.Alternatives();