/
index.js
548 lines (518 loc) · 17.8 KB
/
index.js
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* Map CQL types to Joi types
*
* @typedef Joi
* @see {@link https://github.com/hapijs/joi/tree/v8.0.4}
*
* @typedef JoiMetaDefinition
* @see {@link https://github.com/hapijs/joi/blob/v8.0.4/lib/any.js#L670}
*
* @callback ConversionMapper
* @param {Object} data - the value that is being converted by the handler
*
* @typedef DefaultSpecifierOptions
* @property {String} default - values vary based on which type of object is being defaulted
*
* @callback ObjectConversionHandler
* @param {Object} value - a property of the object being converted
*
* @callback ArrayConversionHandler
* @param {Array} value - a property of the list being converted
*/
var joi = require('@hapi/joi');
var uuid = require('uuid');
var undef;
//
// These ugly beasts are the side effect of not having direct
// access the base `joi` objects (i.e. Any). There is really
// no good way to extend `joi` with additional operations or tests.
//
// These are used for optionally marking a schema object with additional meta data
// so that we don't need as many properties that are disjointed from the
// validation schema.
//
var proto = joi.object().constructor.prototype;
var slice = Array.prototype.slice.call.bind(Array.prototype.slice);
/**
* Defining or retrieving lookup key names
*
* @param {String[]} [names] - defines the names of the lookup keys for the Cassandra table
* @returns {(String[]|Joi)} - depending on whether `names` is specified or not
*/
proto.lookupKeys = function (names) {
if (arguments.length === 0) return findMetaValue(this, 'cqlLookupKeys') || [];
return this.meta({ cqlLookupKeys: Array.isArray(names) ? names : slice(arguments) });
};
/**
* Defining or retrieving the name of the partition key(s)
*
* @param {(String|String[])} [name] - defines the name of the partition keys for the Cassandra table
* @returns {(String[]|Joi)} - depending on whether `name` is specified or not
*/
proto.partitionKey = function (name) {
if (arguments.length === 0) return findMetaValue(this, 'cqlPartitionKey') || [];
return this.meta({ cqlPartitionKey: name });
};
/**
* Defining or retrieving the name of the clustering key(s)
*
* @param {(String|String[])} [name] - defines the name of the clustering keys for the Cassandra table
* @returns {(String[]|Joi)} - depending on whether `name` is specified or not
*/
proto.clusteringKey = function (name) {
if (arguments.length === 0) return findMetaValue(this, 'cqlClusteringKey') || [];
return this.meta({ cqlClusteringKey: name });
};
/**
* Retrieve or specify rename aliases for properties of the schema
*
* @see {@link https://github.com/hapijs/joi/blob/v8.0.4/API.md#objectrenamefrom-to-options}
* @param {String} a - see the documentation for joi.object().rename()
* @returns {(Joi|Object.<String, String>)} - depends on whether you want to retrieve the aliases or set them
*/
proto.aliases = function (a) {
if (a) return this.rename.apply(this, arguments);
return this._inner.renames.reduce(function (memo, rename) {
memo[rename.from] = rename.to;
return memo;
}, {});
};
/**
* Return an object that specifies <FieldName<String>, JoiMetaDefinition> that can be used for configuring a Cassandra table.
*
* @returns {Object.<String, JoiMetaDefinition>} - the meta definitions of the objects children
*/
proto.toCql = function () {
// maps can be objects without children defined
if (!this._inner.children) {
return findMeta(this);
}
var children = this._inner.children;
return children.reduce(function (memo, child) {
memo[child.key] = findMeta(child.schema);
return memo;
}, {});
};
/**
* Return an object that can be used for configuring a Cassandra table.
*
* @returns {JoiMetaDefinition} - the meta definition of the object
*/
joi.constructor.prototype.toCql = function () {
return findMeta(this);
};
var types = module.exports = joi;
/**
* Used for creating Int64 with string and number alternative formats
*
* @param {String} name - the type of int64 value
* @returns {Joi} - the validator
*/
function int64(name) {
return joi.alternatives().meta({ cql: true, type: name }).try(
// a string that represents a number that is larger than JavaScript can handle
joi.string().regex(/^-?\d{1,19}$/m),
// any integer that can be represented in JavaScript
joi.number().integer()
);
}
/**
* Used for creating decimal with string and number alternative formats
*
* @param {String} name - the type of decimal value
* @returns {Joi} - the validator
*/
function decimal(name) {
return joi.alternatives().meta({ cql: true, type: name }).try(
// a string that represents a number that is larger than JavaScript can handle
joi.string().regex(/^-?\d+(\.\d+)?$/m),
// any number that can be represented in JavaScript
joi.number()
);
}
/**
* Each of the member functions returns a joi object that will validate the value given for the expected data type based on Cassandra and JavaScript specifications.
*
* Cassandra Data Types
*
* type constants description
* ----------------------------------
* ascii strings ASCII character string
* bigint integers 64-bit signed long
* blob blobs Arbitrary bytes (no validation)
* boolean booleans true or false
* counter integers Counter column (64-bit signed value). See [111]Counters for
* details
* decimal integers, Variable-precision decimal
* floats
* double integers 64-bit IEEE-754 floating point
* float integers, 32-bit IEEE-754 floating point
* floats
* An IP address. It can be either 4 bytes long (IPv4) or 16
* inet strings bytes long (IPv6). There is no inet constant, IP address
* should be inputed as strings
* int integers 32-bit signed int
* text strings UTF8 encoded string
* integers, A timestamp. Strings constant are allow to input timestamps
* timestamp strings as dates, see [112]Working with dates below for more
* information.
* timeuuid uuids Type 1 UUID. This is generally used as a “conflict-free”
* timestamp. Also see the [113]functions on Timeuuid
* uuid uuids Type 1 or type 4 UUID
* varchar strings UTF8 encoded string
* varint integers Arbitrary-precision integer
*
* @see {@link https://cassandra.apache.org/doc/cql3/CQL.html}
*
* @mixin
*/
types.cql = {
ascii: function () {
return joi.string().allow('').strict(true).meta({ cql: true, type: 'ascii' });
},
bigint: function () {
return int64('bigint');
},
blob: function () {
return joi.alternatives().meta({ cql: true, type: 'blob' }).try(
joi.binary(),
joi.string().hex()
);
},
boolean: function () {
return joi.boolean().meta({ cql: true, type: 'boolean' });
},
counter: function () {
return int64('counter');
},
decimal: function () {
return decimal('decimal');
},
double: function () {
return decimal('double');
},
float: function () {
return decimal('float');
},
inet: function () {
return joi.string()
.meta({ cql: true, type: 'inet' })
.ip({
version: ['ipv4', 'ipv6'],
cidr: 'optional'
});
},
int: function () {
return joi.number()
.integer()
.meta({ cql: true, type: 'int' })
.min(-0x80000000)
.max(0x7fffffff);
},
/**
* @param {Object} [object] - the definition object for joi.object(object)
* @see {@link https://github.com/hapijs/joi/blob/v8.0.4/API.md#object}
* @returns {Joi} - the validator
*/
json: function (object) {
if (object) {
return convertToJsonOnValidate(joi.object(object).meta({
cql: true,
type: 'text',
serialize: JSON.stringify,
deserialize: JSON.parse
}));
}
return convertToJsonOnValidate(
joi.any().meta({
cql: true,
type: 'text',
serialize: JSON.stringify,
deserialize: JSON.parse
})
);
},
text: function () {
return joi.string().allow('').strict(true).meta({ cql: true, type: 'text' });
},
/**
* @param {DefaultSpecifierOptions} [options] - optional way to define a default value
* @returns {Joi} - the validator
*/
timestamp: function (options) {
var validator = joi.alternatives().meta({ cql: true, type: 'timestamp', default: options && options.default }).try(
joi.number().integer(),
joi.date().iso()
);
return defaultify('date', validator, options);
},
/**
* @param {DefaultSpecifierOptions} [options] - optional way to define a default value
* @returns {Joi} - the validator
*/
timeuuid: function (options) {
var validator = joi.string().meta({ cql: true, type: 'timeuuid', default: options && options.default }).guid();
return defaultify('uuid', validator, { default: defaultUuid(options, 'v1') });
},
/**
* @param {DefaultSpecifierOptions} [options] - optional way to define a default value
* @returns {Joi} - the validator
*/
uuid: function (options) {
var validator = joi.string().meta({ cql: true, type: 'uuid', default: options && options.default }).guid();
return defaultify('uuid', validator, { default: defaultUuid(options, 'v4') });
},
varchar: function () {
return joi.string().allow('').meta({ cql: true, type: 'varchar' });
},
varint: function () {
return joi.alternatives().meta({ cql: true, type: 'varint' }).try(
// a string that represents a number that is larger than JavaScript can handle
joi.string().regex(/^-?\d+$/m),
// any integer that can be represented in JavaScript
joi.number().integer()
);
},
/**
* Create a joi object that can validate a `map` for Cassandra.
*
* @param {(String|Joi)} keyType - used for validating the fields of the object, ignored currently
* @param {Joi} valueType - used for validating the values of the fields in the object
* @returns {Joi} - the validator
*/
map: function (keyType, valueType) {
var meta = findMeta(valueType);
return joi.object().meta({
cql: true,
type: 'map',
mapType: ['text', meta.type],
serialize: convertMap(meta.serialize),
deserialize: convertMap(meta.deserialize)
}).pattern(/[-\w]+/, valueType);
},
/**
* Create a joi object that can validate a `set` for Cassandra.
*
* @param {Joi} type - used for validating the values of the set
* @returns {Joi} - the validator
*/
set: function (type) {
var meta = findMeta(type);
var set = joi.array().sparse(false).unique().items(type);
return joi.alternatives().meta({
cql: true,
type: 'set',
setType: meta.type,
serialize: convertArray(meta.serialize),
deserialize: convertArray(meta.deserialize)
}).try(set, joi.object().keys({
add: set,
remove: set
}).or('add', 'remove').unknown(false));
},
/**
* Create a joi object that can validate a `list` for Cassandra.
*
* @param {Joi} type - used for validating the values in the list
* @returns {Joi} - the validator
*/
list: function (type) {
var meta = findMeta(type);
var list = joi.array().sparse(false).items(type);
return joi.alternatives().meta({
cql: true,
type: 'list',
listType: meta.type,
serialize: convertArray(meta.serialize),
deserialize: convertArray(meta.deserialize)
}).try(list, joi.object().keys({
prepend: list,
append: list,
remove: list,
index: joi.object().pattern(/^\d+$/, type)
}).or('prepend', 'append', 'remove', 'index').unknown(false));
}
};
/**
*
* @param {String} type - One of the properties of `types.cql` that is a function that
* accepts the options specified in the `obj` argument.
* @param {Object} obj - specifies the options for the given types.cql `type`
* @param {String[]} [mapType] - when specifying a CQL type of 'map', this contains two arguments for `types.cql.map`
* @returns {Joi} - the validator
*/
types.cql.create = function (type, obj) {
var cqlType;
if (type === 'map') {
cqlType = types.cql.map(types.cql[obj.mapType[0]](), types.cql[obj.mapType[1]]());
} else if (['set', 'list'].indexOf(type) > -1) {
cqlType = types.cql[type](types.cql[obj[type + 'Type']]());
} else {
cqlType = types.cql[type](obj);
}
// obj can be undefined, look at map line #124
if (obj && obj.nullable) {
cqlType = cqlType.allow(null);
}
return cqlType;
};
/*
* Set a default value on a joi object based on specified options.
*/
var defaults = {
/**
* Set a default date based on the contextual operation in which the joi object is being validated.
*
* If 'update' is the specified default, default the value on both 'update' && 'create' operations
* otherwise only default if the operation matches the specified default.
*
* @param {Joi} validator - the object that is having a default set
* @param {DefaultSpecifierOptions} options - the options for setting the default
* @param {String} options.default - [create, update]
* @returns {Joi} - the new validator
*/
date: function (validator, options) {
var when = options.default;
var fn = function (context, config) {
if ((when === config.context.operation) ||
(when === 'update' && ['create', 'update'].indexOf(config.context.operation) > -1)
) {
return new Date().toISOString();
}
return undef;
};
fn.isJoi = true;
return validator.default(fn);
},
/**
* Set a default uuid when the the contextual operation is 'create'.
*
* @param {Joi} validator - the object that is having a default set
* @param {DefaultSpecifierOptions} options - the options for setting the default
* @param {String} options.default - [empty, v1, v4]
* @returns {Joi} - the new validator
*/
uuid: function (validator, options) {
var fn = function (context, config) {
if (config.context.operation === 'create')
return options.default === 'empty' ? '00000000-0000-0000-0000-000000000000' : uuid[options.default]();
return undef;
};
fn.isJoi = true;
return validator.default(fn);
}
};
/**
* Create a default based on a string identifier if one is provided.
*
* @param {String} type - types of defaults [date, uuid]
* @param {Joi} validator - the object being defaulted
* @param {DefaultSpecifierOptions} options - the options for creating the default
* @returns {Joi} - the new validator, possibly with a default
*/
function defaultify(type, validator, options) {
return options && options.default ? defaults[type](validator, options) : validator;
}
/**
* Find the meta object that has the key given.
*
* @param {Joi} any - the validator to be inspected
* @param {String} key - the name of the field in the meta object that we are looking for
* @returns {JoiMetaDefinition} - the result
*/
function findMeta(any, key) {
var searchKey = key || 'cql';
var isCql = searchKey === 'cql';
var meta = {};
var allMetas = (any.describe().meta || []);
var found = false;
for (var i = 0; i < allMetas.length; i++) {
var hasKey = (searchKey in allMetas[i]);
if (found || hasKey) {
if (isCql) {
// If 'cql', merge all subsequent metadata in
Object.assign(meta, allMetas[i]);
} else if (hasKey) {
// Otherwise, find last instance of that key
meta = allMetas[i];
}
found = true;
}
}
return found ? meta : void 0;
}
/**
* Find the value of the meta object that has the key given.
*
* @param {Joi} any - the validator to be inspected
* @param {String} key - the name of the field in the meta object that we are looking for
* @returns {Object} - the value of the field in the meta object
*/
function findMetaValue(any, key) {
return (findMeta(any, key) || {})[key];
}
/**
* Convert the resulting object back to JSON after validation.
*
* * Warning: This relies on the internal structure of a joi object
*
* @param {Joi} any - the object that will be extended to convert the given value
* @returns {Joi} - the extended validator
*/
function convertToJsonOnValidate(any) {
var validate = any._validate;
any._validate = function () {
var result = validate.apply(this, arguments);
if (!result.error && result.value !== undefined) {
result.value = JSON.stringify(result.value);
}
return result;
};
return any;
}
/**
* Ensure that `uuid` type joi objects have a default specified.
*
* @param {DefaultSpecifierOptions} [options] - possible default options
* @property {String} options.default - [empty, v1, v4]
* @param {String} version - the uuid version that should be used as a default if no other default is specified
* @returns {(Undefined|String)} - the value that will identify the appropriate default to the default handler on the validator
*/
function defaultUuid(options, version) {
if (options && options.default) {
if ([version, 'empty'].indexOf(options.default) > -1) {
return options.default;
}
return version;
}
return undef;
}
/**
* Create a serialization handler to run if specified.
*
* @param {ObjectConversionHandler} handler - handles the conversion of the values of an object
* @returns {ConversionMapper} - a wrapper that will iterate across the object given and pass the values to the handler
*/
function convertMap(handler) {
return function (data) {
if (!handler) return data || {};
return Object.keys(data || {}).reduce(function (memo, key) {
memo[key] = handler(data[key]);
return memo;
}, {});
};
}
/**
* Create a serialization handler to run if specified.
*
* @param {ArrayConversionHandler} handler - handles the conversion of the values of an array
* @returns {ConversionMapper} - a wrapper that will iterate across the object given and pass the values to the handler
*/
function convertArray(handler) {
return function (data) {
if (!handler) return data || [];
return (data || []).map(function (value) {
return handler(value);
});
};
}