-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidate.js
More file actions
209 lines (209 loc) · 6.21 KB
/
validate.js
File metadata and controls
209 lines (209 loc) · 6.21 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert = require("./assertions");
const sym = require("./symbols");
const common_1 = require("./common");
const errors_1 = require("./errors");
const validators_1 = require("./validators");
function ValidateObject(schema, input, name) {
// Null Validation
if (schema === null) {
Null(schema, input, name);
return input;
}
// Array Validation
if (Array.isArray(schema)) {
try {
assert.isArray(input);
}
catch (ex) {
throw new errors_1.ValidatorError(Array.name, name, input, ex);
}
let memIdx = 0;
for (const member of input) {
let idx = 0;
for (const prop of schema) {
try {
input[memIdx] = ValidateRecursive(prop, member, `${name}[${idx}]`);
break;
}
catch (ex) {
if ((idx + 1) === schema.length) {
throw new errors_1.ValidatorError(Array.name, `${name}[${idx}]`, input, new errors_1.NotMatchAnyError(input[idx]));
}
}
idx++;
}
memIdx++;
}
// Object Validation
}
else {
try {
assert.isObject(input);
assert.isNull(input, true);
assert.isArray(input, true);
}
catch (ex) {
throw new errors_1.ValidatorError(Object.name, name, input, ex);
}
for (const prop of Object.getOwnPropertyNames(schema)) {
input[prop] = ValidateRecursive(schema[prop], input[prop], `${name}.${prop}`);
}
}
return input;
}
exports.ValidateObject = ValidateObject;
;
function ValidateFunction(schema, input, name) {
// Type Validation
if (schema[sym.Validator] === sym.TypeValidator) {
let typeName = schema[sym.TypeValidator];
try {
assert.isSameTypeName(typeName, common_1.objectType(input));
}
catch (ex) {
throw new errors_1.ValidatorError(typeName, name, input, ex);
}
}
else if (schema[sym.Validator] === sym.OptionsValidator) {
let schemas = schema[sym.OptionsValidator].schemas;
let option = schema[sym.OptionsValidator].option;
let optionName = schema[sym.Metadata].name;
let idx = 0;
for (const subSchema of schemas) {
try {
idx++;
input = ValidateRecursive(subSchema, input, name);
if (option === validators_1.ValidationOptions.any)
break;
}
catch (ex) {
if ((option === validators_1.ValidationOptions.all) || (idx === schemas.length)) {
ex = (option === validators_1.ValidationOptions.all) ? ex : new errors_1.NotMatchAnyError(input);
throw new errors_1.ValidatorError(optionName, name, input, ex);
}
}
}
}
else if (schema[sym.Validator] === sym.CustomValidator) {
let validatorName = schema[sym.Metadata].name;
try {
input = schema[sym.CustomValidator](input, name);
}
catch (ex) {
throw new errors_1.ValidatorError(validatorName, name, input, ex);
}
}
return input;
}
exports.ValidateFunction = ValidateFunction;
;
function LiteralString(schema, input, name) {
try {
assert.isString(input);
assert.isEqual(schema, input);
}
catch (ex) {
throw new errors_1.ValidatorError(LiteralString.name, name, input, ex);
}
return input;
}
exports.LiteralString = LiteralString;
;
function LiteralNumber(schema, input, name) {
try {
assert.isNumber(input);
assert.isEqual(schema, input);
}
catch (ex) {
throw new errors_1.ValidatorError(LiteralNumber.name, name, input, ex);
}
return input;
}
exports.LiteralNumber = LiteralNumber;
;
function LiteralBoolean(schema, input, name) {
try {
assert.isBoolean(input);
assert.isEqual(schema, input);
}
catch (ex) {
throw new errors_1.ValidatorError(LiteralBoolean.name, name, input, ex);
}
return input;
}
exports.LiteralBoolean = LiteralBoolean;
;
function LiteralSymbol(schema, input, name) {
try {
assert.isSymbol(input);
assert.isEqual(schema, input);
}
catch (ex) {
throw new errors_1.ValidatorError(LiteralSymbol.name, name, input, ex);
}
return input;
}
exports.LiteralSymbol = LiteralSymbol;
;
function Undefined(schema, input, name) {
try {
assert.isUndefined(input);
}
catch (ex) {
throw new errors_1.ValidatorError(Undefined.name, name, input, ex);
}
return input;
}
exports.Undefined = Undefined;
;
function Null(schema, input, name) {
try {
assert.isNull(input);
}
catch (ex) {
throw new errors_1.ValidatorError(Null.name, name, input, ex);
}
return input;
}
exports.Null = Null;
;
function Unknown(schema, input, name) {
throw new errors_1.ValidatorError(Undefined.name, name, input, new Error('unknown validation error.'));
}
exports.Unknown = Unknown;
;
function ValidateRecursive(schema, input, name) {
if (typeof schema === 'object') {
input = ValidateObject(schema, input, name);
}
else if (typeof schema === 'function') {
input = ValidateFunction(schema, input, name);
}
else if (typeof schema === 'string') {
input = LiteralString(schema, input, name);
}
else if (typeof schema === 'number') {
input = LiteralNumber(schema, input, name);
}
else if (typeof schema === 'boolean') {
input = LiteralBoolean(schema, input, name);
}
else if (typeof schema === 'symbol') {
input = LiteralSymbol(schema, input, name);
}
else if (typeof schema === 'undefined') {
input = Undefined(schema, input, name);
}
else {
input = Unknown(schema, input, name);
}
return input;
}
exports.ValidateRecursive = ValidateRecursive;
function validate(schema, input, name = 'input') {
return ValidateRecursive(schema, input, name);
}
exports.validate = validate;
//# sourceMappingURL=validate.js.map