@constructive-io/coerce 0.2.0 → 0.3.0
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/coerce.d.ts +8 -0
- package/coerce.js +27 -1
- package/esm/coerce.js +24 -0
- package/package.json +2 -2
package/coerce.d.ts
CHANGED
|
@@ -48,6 +48,14 @@ export declare const asBoolean: (value: unknown) => boolean | null;
|
|
|
48
48
|
* shrinks.
|
|
49
49
|
*/
|
|
50
50
|
export declare const asStringArray: (value: unknown) => string[] | null;
|
|
51
|
+
/**
|
|
52
|
+
* An array whose every entry coerces through `element`, else `null`. One bad
|
|
53
|
+
* entry rejects the whole array — a partially-valid list is a partially-wrong
|
|
54
|
+
* answer.
|
|
55
|
+
*/
|
|
56
|
+
export declare const asArrayOf: <T>(value: unknown, element: (entry: unknown) => T | null) => T[] | null;
|
|
57
|
+
/** {@link asArrayOf}, throwing {@link CoerceError} when absent. */
|
|
58
|
+
export declare const requireArrayOf: <T>(value: unknown, element: (entry: unknown) => T | null, label: string, expected?: string) => T[];
|
|
51
59
|
/**
|
|
52
60
|
* A list of non-empty strings from either an array (see
|
|
53
61
|
* {@link asStringArray}) or a delimited string, else `null`.
|
package/coerce.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* which keeps this package usable off the request path.
|
|
23
23
|
*/
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.requireOneOf = exports.asOneOf = exports.requireIntegerIn = exports.requireNumberIn = exports.requireJson = exports.requireDuration = exports.requireUuid = exports.requireEmail = exports.requireHostname = exports.requireUrl = exports.requireBigInt = exports.requirePort = exports.requireNumericInteger = exports.requireNumeric = exports.requireIsoString = exports.requireDate = exports.requireRecord = exports.requireStringList = exports.requireStringArray = exports.requireBoolean = exports.requireInteger = exports.requireNumber = exports.requireString = exports.asJson = exports.asDuration = exports.asUuid = exports.asEmail = exports.asHostname = exports.asUrl = exports.asBigInt = exports.asPort = exports.asIntegerIn = exports.asNumberIn = exports.asNumericInteger = exports.asNumeric = exports.asIsoString = exports.asDate = exports.asRecord = exports.asStringList = exports.asStringArray = exports.asBoolean = exports.asInteger = exports.asNumber = exports.asString = exports.CoerceError = void 0;
|
|
25
|
+
exports.requireOneOf = exports.asOneOf = exports.requireIntegerIn = exports.requireNumberIn = exports.requireJson = exports.requireDuration = exports.requireUuid = exports.requireEmail = exports.requireHostname = exports.requireUrl = exports.requireBigInt = exports.requirePort = exports.requireNumericInteger = exports.requireNumeric = exports.requireIsoString = exports.requireDate = exports.requireRecord = exports.requireStringList = exports.requireStringArray = exports.requireBoolean = exports.requireInteger = exports.requireNumber = exports.requireString = exports.asJson = exports.asDuration = exports.asUuid = exports.asEmail = exports.asHostname = exports.asUrl = exports.asBigInt = exports.asPort = exports.asIntegerIn = exports.asNumberIn = exports.asNumericInteger = exports.asNumeric = exports.asIsoString = exports.asDate = exports.asRecord = exports.asStringList = exports.requireArrayOf = exports.asArrayOf = exports.asStringArray = exports.asBoolean = exports.asInteger = exports.asNumber = exports.asString = exports.CoerceError = void 0;
|
|
26
26
|
/** A value that was required but absent or of the wrong shape. */
|
|
27
27
|
class CoerceError extends Error {
|
|
28
28
|
/** What the caller was asking for, e.g. `'message_id'`. */
|
|
@@ -85,6 +85,32 @@ const asStringArray = (value) => {
|
|
|
85
85
|
return strings;
|
|
86
86
|
};
|
|
87
87
|
exports.asStringArray = asStringArray;
|
|
88
|
+
/**
|
|
89
|
+
* An array whose every entry coerces through `element`, else `null`. One bad
|
|
90
|
+
* entry rejects the whole array — a partially-valid list is a partially-wrong
|
|
91
|
+
* answer.
|
|
92
|
+
*/
|
|
93
|
+
const asArrayOf = (value, element) => {
|
|
94
|
+
if (!Array.isArray(value))
|
|
95
|
+
return null;
|
|
96
|
+
const entries = [];
|
|
97
|
+
for (const item of value) {
|
|
98
|
+
const coerced = element(item);
|
|
99
|
+
if (coerced === null)
|
|
100
|
+
return null;
|
|
101
|
+
entries.push(coerced);
|
|
102
|
+
}
|
|
103
|
+
return entries;
|
|
104
|
+
};
|
|
105
|
+
exports.asArrayOf = asArrayOf;
|
|
106
|
+
/** {@link asArrayOf}, throwing {@link CoerceError} when absent. */
|
|
107
|
+
const requireArrayOf = (value, element, label, expected = 'an array of valid entries') => {
|
|
108
|
+
const entries = (0, exports.asArrayOf)(value, element);
|
|
109
|
+
if (entries === null)
|
|
110
|
+
throw new CoerceError(label, expected);
|
|
111
|
+
return entries;
|
|
112
|
+
};
|
|
113
|
+
exports.requireArrayOf = requireArrayOf;
|
|
88
114
|
/**
|
|
89
115
|
* A list of non-empty strings from either an array (see
|
|
90
116
|
* {@link asStringArray}) or a delimited string, else `null`.
|
package/esm/coerce.js
CHANGED
|
@@ -76,6 +76,30 @@ export const asStringArray = (value) => {
|
|
|
76
76
|
}
|
|
77
77
|
return strings;
|
|
78
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* An array whose every entry coerces through `element`, else `null`. One bad
|
|
81
|
+
* entry rejects the whole array — a partially-valid list is a partially-wrong
|
|
82
|
+
* answer.
|
|
83
|
+
*/
|
|
84
|
+
export const asArrayOf = (value, element) => {
|
|
85
|
+
if (!Array.isArray(value))
|
|
86
|
+
return null;
|
|
87
|
+
const entries = [];
|
|
88
|
+
for (const item of value) {
|
|
89
|
+
const coerced = element(item);
|
|
90
|
+
if (coerced === null)
|
|
91
|
+
return null;
|
|
92
|
+
entries.push(coerced);
|
|
93
|
+
}
|
|
94
|
+
return entries;
|
|
95
|
+
};
|
|
96
|
+
/** {@link asArrayOf}, throwing {@link CoerceError} when absent. */
|
|
97
|
+
export const requireArrayOf = (value, element, label, expected = 'an array of valid entries') => {
|
|
98
|
+
const entries = asArrayOf(value, element);
|
|
99
|
+
if (entries === null)
|
|
100
|
+
throw new CoerceError(label, expected);
|
|
101
|
+
return entries;
|
|
102
|
+
};
|
|
79
103
|
/**
|
|
80
104
|
* A list of non-empty strings from either an array (see
|
|
81
105
|
* {@link asStringArray}) or a delimited string, else `null`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/coerce",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Coerce untrusted unknown values into narrow TypeScript types",
|
|
6
6
|
"main": "index.js",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"validation",
|
|
37
37
|
"typescript"
|
|
38
38
|
],
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "06bf3edad0d1d1dba9b44f17b8c791de6768576d"
|
|
40
40
|
}
|