-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvalidator.js
More file actions
352 lines (308 loc) · 12.4 KB
/
validator.js
File metadata and controls
352 lines (308 loc) · 12.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* Validate and apply default values to resource's configuration options.
*
* @function validateOptions
* @private
* @param {object} options Configuration options.
* @returns {object} valid configuration options.
*/
function validateOptions(options) {
options = {
id: 'id',
blacklist: [],
whitelist: [],
links: {},
relationships: {},
topLevelLinks: {},
topLevelMeta: {},
meta: {},
blacklistOnDeserialize: [],
whitelistOnDeserialize: [],
jsonapiObject: true,
...options,
};
if (!Array.isArray(options.blacklist)) throw new Error("option 'blacklist' must be an array");
if (!Array.isArray(options.whitelist)) throw new Error("option 'whitelist' must be an array");
if (typeof options.links !== 'object' && typeof options.links !== 'function')
throw new Error("option 'links' must be an object or a function");
if (!Array.isArray(options.blacklistOnDeserialize))
throw new Error("option 'blacklistOnDeserialize' must be an array");
if (!Array.isArray(options.whitelistOnDeserialize))
throw new Error("option 'whitelistOnDeserialize' must be an array");
if (
options.topLevelLinks &&
typeof options.topLevelLinks !== 'object' &&
typeof options.topLevelLinks !== 'function'
)
throw new Error("option 'topLevelLinks' must be an object or a function");
if (
options.topLevelMeta &&
typeof options.topLevelMeta !== 'object' &&
typeof options.topLevelMeta !== 'function'
)
throw new Error("option 'topLevelMeta' must be an object or a function");
if (options.meta && typeof options.meta !== 'object' && typeof options.meta !== 'function')
throw new Error("option 'meta' must be an object or a function");
if (typeof options.jsonapiObject !== 'boolean')
throw new Error("option 'jsonapiObject' must a boolean");
if (
options.convertCase &&
!['kebab-case', 'snake_case', 'camelCase'].includes(options.convertCase)
)
throw new Error("option 'convertCase' must be one of 'kebab-case', 'snake_case', 'camelCase'");
if (
options.unconvertCase &&
!['kebab-case', 'snake_case', 'camelCase'].includes(options.unconvertCase)
)
throw new Error(
"option 'unconvertCase' must be one of 'kebab-case', 'snake_case', 'camelCase'"
);
if (options.beforeSerialize && typeof options.beforeSerialize !== 'function')
throw new Error("option 'beforeSerialize' must be function");
if (options.afterDeserialize && typeof options.afterDeserialize !== 'function')
throw new Error("option 'afterDeserialize' must be function");
const { relationships } = options;
Object.keys(relationships).forEach((key) => {
relationships[key] = {
schema: 'default',
links: {},
meta: {},
...relationships[key],
};
if (!relationships[key].type)
throw new Error(`option 'type' for relationship '${key}' is required`);
if (
typeof relationships[key].type !== 'string' &&
typeof relationships[key].type !== 'function'
)
throw new Error(`option 'type' for relationship '${key}' must be a string or a function`);
if (relationships[key].alternativeKey && typeof relationships[key].alternativeKey !== 'string')
throw new Error(`option 'alternativeKey' for relationship '${key}' must be a string`);
if (relationships[key].schema && typeof relationships[key].schema !== 'string')
throw new Error(`option 'schema' for relationship '${key}' must be a string`);
if (
typeof relationships[key].links !== 'object' &&
typeof relationships[key].links !== 'function'
)
throw new Error(`option 'links' for relationship '${key}' must be an object or a function`);
if (
typeof relationships[key].meta !== 'object' &&
typeof relationships[key].meta !== 'function'
)
throw new Error(`option 'meta' for relationship '${key}' must be an object or a function`);
if (relationships[key].deserialize && typeof relationships[key].deserialize !== 'function')
throw new Error(`option 'deserialize' for relationship '${key}' must be a function`);
if (relationships[key].data && typeof relationships[key].data !== 'function')
throw new Error(`option 'data' for relationship '${key}' must be a function`);
});
return options;
}
/**
* Validate and apply default values to the dynamic type object option.
*
* @function validateDynamicTypeOptions
* @private
* @param {object} options dynamic type object option.
* @returns {object} valid dynamic type options.
*/
function validateDynamicTypeOptions(options) {
options = { topLevelLinks: {}, topLevelMeta: {}, jsonapiObject: true, ...options };
if (!options.type) throw new Error("option 'type' is required");
if (typeof options.type !== 'string' && typeof options.type !== 'function') {
throw new Error("option 'type' must be a string or a function");
}
if (
options.topLevelLinks &&
typeof options.topLevelLinks !== 'object' &&
typeof options.topLevelLinks !== 'function'
)
throw new Error("option 'topLevelLinks' must be an object or a function");
if (
options.topLevelMeta &&
typeof options.topLevelMeta !== 'object' &&
typeof options.topLevelMeta !== 'function'
)
throw new Error("option 'topLevelMeta' must be an object or a function");
if (options.meta && typeof options.meta !== 'object' && typeof options.meta !== 'function')
throw new Error("option 'meta' must be an object or a function");
if (typeof options.jsonapiObject !== 'boolean')
throw new Error("option 'jsonapiObject' must a boolean");
return options;
}
/**
* Validate a JSON:API error object
*
* @function validateError
* @private
* @throws Will throw an error if the argument is not an object
* @param {object} err a JSONAPI error object
* @returns {object} JSONAPI valid error object
*/
function validateError(err) {
if (typeof err !== 'object') {
throw new Error('error must be an object');
}
const { id, links, status, statusCode, code, title, detail, source, meta } = err;
/**
* Validates the `links` property, ensuring that it is an object and,
* if present, contains valid members. From the JSON:API spec:
*
* links: a links object containing the following members:
* about: a link that leads to further details about this particular
* occurrence of the problem.
*
* @function isValidLink
* @private
* @see https://jsonapi.org/format/#error-objects
* @throws Will throw an error if the argument is not an object
* @throws Will throw an error if the argument contains unpermitted members
* @throws Will throw an error if `links.about` is present but not a string
* @param {object} linksObj links object
* @returns {object} validated links
*/
const isValidLink = function isValidLink(linksObj) {
const permittedMembers = ['about'];
if (typeof linksObj !== 'object') {
throw new Error("error 'link' property must be an object");
}
Object.keys(linksObj).forEach((key) => {
if (!permittedMembers.includes(key)) {
throw new Error(`error 'links.${key}' is not permitted`);
}
});
if (linksObj.about && typeof linksObj.about !== 'string') {
throw new Error("'links.about' property must be a string");
}
return links;
};
/**
* Validates the `source` property, ensuring that it is an object and,
* if present, contains valid members. From the JSON:API spec:
*
* source: an object containing references to the source of the error,
* optionally including any of the following members:
* pointer: a JSON Pointer [RFC6901] to the associated entity in the
* request document [e.g. "/data" for a primary data object,
* or "/data/attributes/title" for a specific attribute].
* parameter: a string indicating which URI query parameter caused the
* error.
*
* @function isValidSource
* @private
* @see https://jsonapi.org/format/#error-objects
* @param {object} sourceObj source object
* @throws Will throw an error if the argument is not an object
* @throws Will throw an error if the argument contains unpermitted members
* @throws Will throw an error if `sourceObj.pointer` is present but not a string
* @throws Will throw an error if `sourceObj.parameter` is present but not a string
* @returns {object} validated source
*/
const isValidSource = function isValidSource(sourceObj) {
const permittedMembers = ['pointer', 'parameter'];
if (typeof sourceObj !== 'object') {
throw new Error("error 'source' property must be an object");
}
Object.keys(sourceObj).forEach((key) => {
if (!permittedMembers.includes(key)) {
throw new Error(`error 'source.${key}' is not permitted`);
}
});
if (sourceObj.pointer && typeof sourceObj.pointer !== 'string') {
throw new Error("error 'source.pointer' property must be a string");
}
if (sourceObj.parameter && typeof sourceObj.parameter !== 'string') {
throw new Error("error 'source.parameter' property must be a string");
}
return source;
};
/**
* Validates the `meta` property, ensuring that it is an object. From
* the JSON:API spec:
*
* meta: a meta object containing non-standard meta-information about
* the error.
*
* @function isValidMeta
* @private
* @see https://jsonapi.org/format/#error-objects
* @param {object} metaObj meta object
* @throws Will throw an error if the argument is not an object
* @returns {object} validated meta
*/
const isValidMeta = function isValidMeta(metaObj) {
if (typeof metaObj !== 'object') {
throw new Error("error 'meta' property must be an object");
}
return meta;
};
/**
* Determines if the provided number is a valid HTTP status code.
*
* @function isValidHttpStatusCode
* @private
* @see https://jsonapi.org/format/#error-objects
* @param {number} theStatusCode The status code to validate
* @returns {boolean} is valid
*/
const isValidHttpStatusCode = function isValidHttpStatusCode(theStatusCode) {
const validHttpStatusCodes = [
// 1XX: Informational
100,101, 102, // eslint-disable-line
// 2XX: Success
200, 201, 202, 203, 204, 205, 206, 207, 208, 226, // eslint-disable-line
// 3XX: Redirection
300, 301, 302, 303, 304, 305, 307, 308, // eslint-disable-line
// 4XX: Client Error
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, // eslint-disable-line
413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 426, 428, 429, // eslint-disable-line
431, 444, 451, 499, // eslint-disable-line
// 5XX: Server Error
500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, 599 // eslint-disable-line
];
return validHttpStatusCodes.includes(theStatusCode);
};
/**
* Validates a status code, ensuring that it is both a number and a valid
* HTTP status code. From the JSON:API spec:
*
* status: the HTTP status code applicable to this problem, expressed
* as a string value.
*
* @function isValidStatus
* @private
* @see https://jsonapi.org/format/#error-objects
* @param {string|number} theStatusCode status code
* @throws Will throw an error if the argument is not number-like
* @throws Will throw an error if the argument is not a valid HTTP status code
* @returns {string} validated status
*/
const isValidStatus = function isValidStatus(theStatusCode) {
const statusAsNumber = Number(theStatusCode);
if (Number.isNaN(statusAsNumber)) {
throw new Error("error 'status' must be a number");
}
if (!isValidHttpStatusCode(statusAsNumber)) {
throw new Error("error 'status' must be a valid HTTP status code");
}
return statusAsNumber.toString();
};
const error = {};
if (id) error.id = id;
if (links && Object.keys(links).length) error.links = isValidLink(links);
if (status || statusCode) error.status = isValidStatus(status || statusCode);
if (code) error.code = code.toString();
if (title) {
error.title = title.toString();
} else if (err.constructor.name !== 'Object') {
error.title = err.constructor.name;
}
error.detail = detail ? detail.toString() : err.message;
if (source && Object.keys(source).length) error.source = isValidSource(source);
if (meta && Object.keys(meta).length) error.meta = isValidMeta(meta);
return error;
}
module.exports = {
validateOptions,
validateDynamicTypeOptions,
validateError,
};