binhend 2.3.3 → 2.3.4
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/package.json
CHANGED
|
@@ -5,9 +5,9 @@ const types = require('@binhend/types');
|
|
|
5
5
|
* @typedef {Object} Options
|
|
6
6
|
* @property {string=} message - Any custom error message
|
|
7
7
|
* @property {string=} type - Expected type name of value, e.g. string, number, array, etc.
|
|
8
|
-
* @property {string=} name - The variable/property
|
|
9
|
-
* @property {boolean=} required - The
|
|
10
|
-
* @property {*=} default - The
|
|
8
|
+
* @property {string=} name - The name of variable/property holding value
|
|
9
|
+
* @property {boolean=} required - The value must not be null or undefined
|
|
10
|
+
* @property {*=} default - The default value being used if null or undefined
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -109,15 +109,46 @@ function Array(input, options = {}) {
|
|
|
109
109
|
/**
|
|
110
110
|
* Validate an input being an object (non-array)
|
|
111
111
|
*
|
|
112
|
-
* @
|
|
113
|
-
* @
|
|
114
|
-
*
|
|
112
|
+
* @template {Record<string, any>} [T={}]
|
|
113
|
+
* @template {Record<string, any>} [O={}]
|
|
114
|
+
*
|
|
115
|
+
* @param {T=} input - Input value needs to be validated.
|
|
116
|
+
* @param {Options & { optional?: O }} options - Options for validation with extra option { optional } for listing optional properties.
|
|
117
|
+
* @returns {T & O & Record<string, any>} - The validated input
|
|
115
118
|
* @throws {HttpError} - If validation fails
|
|
116
119
|
*/
|
|
117
120
|
function Object(input, options = {}) {
|
|
118
121
|
return Validator(input, types.isObject, { ...options, type: 'object' });
|
|
119
122
|
}
|
|
120
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Return an object fragment with a specific key
|
|
126
|
+
* only if the key exists AND validation is passed.
|
|
127
|
+
*
|
|
128
|
+
* @template {string} K
|
|
129
|
+
* @template V
|
|
130
|
+
* @param {K} key - A property name
|
|
131
|
+
* @param {*} object - Any target that may have properties (objects or primitives)
|
|
132
|
+
* @param {((value: any, options: Options) => V)} [validator] - An optional validation function
|
|
133
|
+
* @param {Options=} options - Options for validation.
|
|
134
|
+
* @returns {{ [P in K]?: V }}
|
|
135
|
+
*/
|
|
136
|
+
function Prop(key, object, validator, options) {
|
|
137
|
+
var value = object != null ? object[key] : undefined;
|
|
138
|
+
|
|
139
|
+
if (validator instanceof Function) {
|
|
140
|
+
value = validator(value, { ...options, name: key });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (object == null || !Object.prototype.hasOwnProperty.call(object, key)) {
|
|
144
|
+
return /** @type {{ [P in K]?: V }} */ ({});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return /** @type {{ [P in K]?: V }} */ ({
|
|
148
|
+
[key]: value
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
121
152
|
/**
|
|
122
153
|
* Validate an input being a function
|
|
123
154
|
*
|
|
@@ -273,6 +304,7 @@ module.exports = {
|
|
|
273
304
|
Boolean,
|
|
274
305
|
Array,
|
|
275
306
|
Object,
|
|
307
|
+
Prop,
|
|
276
308
|
Function,
|
|
277
309
|
AsyncFunction,
|
|
278
310
|
Date,
|