binhend 2.1.16 → 2.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/types/common.d.ts +10 -0
- package/src/types/index.d.ts +2 -0
- package/src/utils/typedefs.js +31 -0
- package/src/utils/types.js +7 -2
- package/src/utils/validation.js +40 -1
- package/demo.js +0 -45
package/index.js
CHANGED
|
@@ -24,6 +24,7 @@ const createCSD = require('./src/csd');
|
|
|
24
24
|
const security = require('./src/security');
|
|
25
25
|
const types = require('./src/utils/types');
|
|
26
26
|
const validation = require('./src/utils/validation');
|
|
27
|
+
const typedefs = require('./src/utils/typedefs');
|
|
27
28
|
const Bromise = require('./src/utils/Bromise.js');
|
|
28
29
|
|
|
29
30
|
/** CLIENT - Frontend */
|
|
@@ -46,6 +47,7 @@ module.exports = {
|
|
|
46
47
|
createCSD,
|
|
47
48
|
security,
|
|
48
49
|
types,
|
|
50
|
+
typedefs,
|
|
49
51
|
validation,
|
|
50
52
|
Bromise,
|
|
51
53
|
WebBuilder,
|
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {Object<string, any>} ObjectType
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type definition for object accepting any extra properties
|
|
8
|
+
*
|
|
9
|
+
* @returns {ObjectType}
|
|
10
|
+
*/
|
|
11
|
+
function ObjectType() { return this; }
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {(Date|string|number)} DateType
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Type definition for date accepting any valid formats (string, number, date)
|
|
21
|
+
*
|
|
22
|
+
* @returns {DateType}
|
|
23
|
+
*/
|
|
24
|
+
function DateType() { return this; }
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
ObjectType,
|
|
30
|
+
DateType
|
|
31
|
+
};
|
package/src/utils/types.js
CHANGED
|
@@ -7,6 +7,10 @@ function isObject(input) {
|
|
|
7
7
|
return typeOf(input) === 'Object';
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
function mustObject(input, defaultValue = {}) {
|
|
11
|
+
return isObject(input) ? input : isObject(defaultValue) ? defaultValue : {};
|
|
12
|
+
}
|
|
13
|
+
|
|
10
14
|
function isEmptyObject(input) {
|
|
11
15
|
return isObject(input) && Object.keys(input).length === 0;
|
|
12
16
|
}
|
|
@@ -17,8 +21,8 @@ function isEmptyArray(input) {
|
|
|
17
21
|
return isArray(input) && input.length === 0;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
function mustArray(input,
|
|
21
|
-
return isArray(input) ? input : isArray(
|
|
24
|
+
function mustArray(input, defaultValue = []) {
|
|
25
|
+
return isArray(input) ? input : isArray(defaultValue) ? defaultValue : [];
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
function isFunction(input) {
|
|
@@ -86,6 +90,7 @@ module.exports = {
|
|
|
86
90
|
typeOf,
|
|
87
91
|
isObject,
|
|
88
92
|
isEmptyObject,
|
|
93
|
+
mustObject,
|
|
89
94
|
isArray,
|
|
90
95
|
isEmptyArray,
|
|
91
96
|
mustArray,
|
package/src/utils/validation.js
CHANGED
|
@@ -71,6 +71,18 @@ function Number(input, options = {}) {
|
|
|
71
71
|
return Validator(input, types.isNumber, { ...options, type: 'number' });
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Validate an input being an integer
|
|
76
|
+
*
|
|
77
|
+
* @param {*} input - Input value needs to be validated.
|
|
78
|
+
* @param {Options} options - Additional options for validation.
|
|
79
|
+
* @returns {number} - The validated input
|
|
80
|
+
* @throws {HttpError} - If validation fails
|
|
81
|
+
*/
|
|
82
|
+
function Integer(input, options = {}) {
|
|
83
|
+
return Validator(input, global.Number.isInteger, { ...options, type: 'integer' });
|
|
84
|
+
}
|
|
85
|
+
|
|
74
86
|
/**
|
|
75
87
|
* Validate an input being boolean
|
|
76
88
|
*
|
|
@@ -136,13 +148,38 @@ function Date(input, options = {}) {
|
|
|
136
148
|
*
|
|
137
149
|
* @param {*} input - Input value needs to be validated.
|
|
138
150
|
* @param {Options} options - Additional options for validation.
|
|
139
|
-
* @returns {
|
|
151
|
+
* @returns {Date|string|number} - The validated input
|
|
140
152
|
* @throws {HttpError} - If validation fails
|
|
141
153
|
*/
|
|
142
154
|
function DateLike(input, options = {}) {
|
|
143
155
|
return Validator(input, types.isDateLike, { ...options, type: 'date-like' });
|
|
144
156
|
}
|
|
145
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Validate an input being one of the specified enum values
|
|
160
|
+
*
|
|
161
|
+
* @param {*} input - Input value needs to be validated.
|
|
162
|
+
* @param {Array|Object} enumValues - Array of allowed values or object whose keys are allowed values
|
|
163
|
+
* @param {Options} options - Additional options for validation.
|
|
164
|
+
* @returns {*} - The validated input
|
|
165
|
+
* @throws {HttpError} - If validation fails
|
|
166
|
+
*/
|
|
167
|
+
function Enum(input, enumValues, options = {}) {
|
|
168
|
+
var validArray = types.isArray(enumValues),
|
|
169
|
+
validObject = types.isObject(enumValues);
|
|
170
|
+
|
|
171
|
+
if (!(validArray || validObject)) {
|
|
172
|
+
throwError('Enum validator requires an array or object of allowed values');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
var method = validArray ? 'includes' : 'hasOwnProperty';
|
|
176
|
+
|
|
177
|
+
return Validator(
|
|
178
|
+
input, (input) => enumValues[method](input),
|
|
179
|
+
{ ...options, message: options.message || `${input} is not defined in enum` }
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
146
183
|
/**
|
|
147
184
|
* Validate an input being defined (not undefined)
|
|
148
185
|
*
|
|
@@ -219,12 +256,14 @@ function Validator(input, validate, options = {}) {
|
|
|
219
256
|
module.exports = {
|
|
220
257
|
String,
|
|
221
258
|
Number,
|
|
259
|
+
Integer,
|
|
222
260
|
Boolean,
|
|
223
261
|
Array,
|
|
224
262
|
Object,
|
|
225
263
|
Function,
|
|
226
264
|
Date,
|
|
227
265
|
DateLike,
|
|
266
|
+
Enum,
|
|
228
267
|
Defined,
|
|
229
268
|
NotNull,
|
|
230
269
|
NotNullish,
|
package/demo.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation } = require('./index');
|
|
3
|
-
|
|
4
|
-
// HttpCodes.ACCEPTED;
|
|
5
|
-
|
|
6
|
-
// new ConfigLoader({}).cli;
|
|
7
|
-
|
|
8
|
-
// const builder = new WebBuilder('src', { output: 'build/bundle' });
|
|
9
|
-
|
|
10
|
-
// builder.bundle();
|
|
11
|
-
|
|
12
|
-
// throw new HttpError(HttpCodes.BAD_REQUEST, '[BINHEND][VALIDATION] Must be a string.');
|
|
13
|
-
// throw new HttpError(HttpCodes.BAD_REQUEST, '[BINHEND][VALIDATION] MUST BE A STRING.');
|
|
14
|
-
// var typeName = `array`;
|
|
15
|
-
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: value not passed custom validator.`);
|
|
16
|
-
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: unknown reason.`);
|
|
17
|
-
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: value must be type of \`${typeName}\`.`);
|
|
18
|
-
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: Field "title" must be type of \`${typeName}\`.`);
|
|
19
|
-
|
|
20
|
-
// validation.String('a', {});
|
|
21
|
-
|
|
22
|
-
validation.String('a');
|
|
23
|
-
// validation.String(null);
|
|
24
|
-
// validation.String(1, { name: 'myNum', message: 'Require string for this field' });
|
|
25
|
-
|
|
26
|
-
// validation.Validator('this is me', (input) => {
|
|
27
|
-
// if (!input.startsWith('It')) validation.throwError(`"title" must start with 'it' or 'It'`)
|
|
28
|
-
// return input.length < 5;
|
|
29
|
-
// }, { message: '"title" must not exceed max length of 5' })
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
validation.NotNull(1);
|
|
33
|
-
// validation.NotNullish(null, { required: true, default: null });
|
|
34
|
-
// validation.String(null, { required: true, default: 1 });
|
|
35
|
-
|
|
36
|
-
validation.DateLike('2022-10-29a', { default: new Date().getTime() });
|
|
37
|
-
validation.Date(new Date());
|
|
38
|
-
|
|
39
|
-
function Abc(a) {
|
|
40
|
-
this.a = a;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
console.log(JSON.stringify(new Abc(123)));
|
|
44
|
-
|
|
45
|
-
validation.Object(new Abc(123));
|