index.js
3.2 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 Any = require('./any');
var Cast = require('./cast');
var Ref = require('./ref');
// Declare internals
var internals = {
alternatives: require('./alternatives'),
array: require('./array'),
boolean: require('./boolean'),
binary: require('./binary'),
date: require('./date'),
number: require('./number'),
object: require('./object'),
string: require('./string')
};
internals.root = function () {
var any = new Any();
var root = any.clone();
root.any = function () {
return any;
};
root.alternatives = root.alt = function () {
return arguments.length ? internals.alternatives.try.apply(internals.alternatives, arguments) : internals.alternatives;
};
root.array = function () {
return internals.array;
};
root.boolean = root.bool = function () {
return internals.boolean;
};
root.binary = function () {
return internals.binary;
};
root.date = function () {
return internals.date;
};
root.func = function () {
return internals.object._func();
};
root.number = function () {
return internals.number;
};
root.object = function () {
return arguments.length ? internals.object.keys.apply(internals.object, arguments) : internals.object;
};
root.string = function () {
return internals.string;
};
root.ref = function () {
return Ref.create.apply(null, arguments);
};
root.isRef = function (ref) {
return Ref.isRef(ref);
};
root.validate = function (value /*, [schema], [options], callback */) {
var last = arguments[arguments.length - 1];
var callback = typeof last === 'function' ? last : null;
var count = arguments.length - (callback ? 1 : 0);
if (count === 1) {
return any.validate(value, callback);
}
var options = count === 3 ? arguments[2] : {};
var schema = root.compile(arguments[1]);
return schema._validateWithOptions(value, options, callback);
};
root.describe = function () {
var schema = arguments.length ? root.compile(arguments[0]) : any;
return schema.describe();
};
root.compile = function (schema) {
try {
return Cast.schema(schema);
}
catch (err) {
if (err.hasOwnProperty('path')) {
err.message += '(' + err.path + ')';
}
throw err;
}
};
root.assert = function (value, schema, message) {
root.attempt(value, schema, message);
};
root.attempt = function (value, schema, message) {
var result = root.validate(value, schema);
var error = result.error;
if (error) {
if (!message) {
error.message = error.annotate();
throw error;
}
if (!(message instanceof Error)) {
error.message = message + ' ' + error.annotate();
throw error;
}
throw message;
}
return result.value;
};
return root;
};
module.exports = internals.root();