@ninetailed/experience.js-utils 2.2.8 → 2.2.10-beta.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/index.esm.js +32 -1
- package/index.umd.js +36 -1
- package/package.json +3 -3
- package/types/Experience.d.ts +8 -8
- package/types/Experiment.d.ts +6 -2
- package/types/zodArrayIgnoreUnknown.d.ts +14 -0
package/index.esm.js
CHANGED
|
@@ -31,6 +31,37 @@ const Variant = z.object({
|
|
|
31
31
|
id: z.string()
|
|
32
32
|
}).passthrough();
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Zod helper for parsing arrays and ignore items not specified in the schema
|
|
36
|
+
*
|
|
37
|
+
* @param zodUnion - union of known types
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')]))
|
|
41
|
+
* type BinaryArray = z.TypeOf<typeof binaryArraySchema>
|
|
42
|
+
*
|
|
43
|
+
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
|
|
44
|
+
* console.log(binaryArray) // ['0', '1', '0']
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
function zodArrayIgnoreUnknown(zodType) {
|
|
48
|
+
const isKnownItem = item => zodType.safeParse(item).success;
|
|
49
|
+
|
|
50
|
+
return z.preprocess(val => toSafeArray(val).filter(isKnownItem), z.array(zodType));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function toSafeArray(item) {
|
|
54
|
+
if (isArray(item)) {
|
|
55
|
+
return item;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return [item];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isArray(item) {
|
|
62
|
+
return Array.isArray(item);
|
|
63
|
+
}
|
|
64
|
+
|
|
34
65
|
const Experience = z.object({
|
|
35
66
|
id: z.string(),
|
|
36
67
|
|
|
@@ -57,7 +88,7 @@ const Experience = z.object({
|
|
|
57
88
|
/**
|
|
58
89
|
* All used variants of the experience (References to other Content Types)
|
|
59
90
|
*/
|
|
60
|
-
variants:
|
|
91
|
+
variants: zodArrayIgnoreUnknown(Variant).default([])
|
|
61
92
|
});
|
|
62
93
|
|
|
63
94
|
const Experiment = Experience.extend({
|
package/index.umd.js
CHANGED
|
@@ -60,6 +60,41 @@
|
|
|
60
60
|
id: zod.z.string()
|
|
61
61
|
}).passthrough();
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Zod helper for parsing arrays and ignore items not specified in the schema
|
|
65
|
+
*
|
|
66
|
+
* @param zodUnion - union of known types
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')]))
|
|
70
|
+
* type BinaryArray = z.TypeOf<typeof binaryArraySchema>
|
|
71
|
+
*
|
|
72
|
+
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
|
|
73
|
+
* console.log(binaryArray) // ['0', '1', '0']
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
function zodArrayIgnoreUnknown(zodType) {
|
|
77
|
+
var isKnownItem = function (item) {
|
|
78
|
+
return zodType.safeParse(item).success;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return zod.z.preprocess(function (val) {
|
|
82
|
+
return toSafeArray(val).filter(isKnownItem);
|
|
83
|
+
}, zod.z.array(zodType));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function toSafeArray(item) {
|
|
87
|
+
if (isArray(item)) {
|
|
88
|
+
return item;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return [item];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isArray(item) {
|
|
95
|
+
return Array.isArray(item);
|
|
96
|
+
}
|
|
97
|
+
|
|
63
98
|
var Experience = zod.z.object({
|
|
64
99
|
id: zod.z.string(),
|
|
65
100
|
|
|
@@ -86,7 +121,7 @@
|
|
|
86
121
|
/**
|
|
87
122
|
* All used variants of the experience (References to other Content Types)
|
|
88
123
|
*/
|
|
89
|
-
variants:
|
|
124
|
+
variants: zodArrayIgnoreUnknown(Variant).default([])
|
|
90
125
|
});
|
|
91
126
|
|
|
92
127
|
var Experiment = Experience.extend({
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ninetailed/experience.js-utils",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.10-beta.0",
|
|
4
4
|
"main": "./index.umd.js",
|
|
5
5
|
"module": "./index.esm.js",
|
|
6
6
|
"typings": "./index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@ninetailed/experience.js": "2.2.
|
|
8
|
+
"@ninetailed/experience.js": "2.2.10-beta.0",
|
|
9
9
|
"analytics": "^0.8.0",
|
|
10
|
-
"@ninetailed/experience.js-shared": "2.2.
|
|
10
|
+
"@ninetailed/experience.js-shared": "2.2.10-beta.0",
|
|
11
11
|
"uuid": "^8.3.2",
|
|
12
12
|
"ts-toolbelt": "^9.6.0",
|
|
13
13
|
"locale-enum": "^1.1.1",
|
package/types/Experience.d.ts
CHANGED
|
@@ -22,9 +22,7 @@ export declare const Experience: z.ZodObject<{
|
|
|
22
22
|
id: string;
|
|
23
23
|
}, {
|
|
24
24
|
id?: string | undefined;
|
|
25
|
-
}>;
|
|
26
|
-
* The type if the experience (nt_experiment | nt_personalization)
|
|
27
|
-
*/
|
|
25
|
+
}>;
|
|
28
26
|
variants: z.ZodArray<z.ZodObject<{
|
|
29
27
|
id: z.ZodDefault<z.ZodString>;
|
|
30
28
|
hidden: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -34,9 +32,7 @@ export declare const Experience: z.ZodObject<{
|
|
|
34
32
|
}, {
|
|
35
33
|
id?: string | undefined;
|
|
36
34
|
hidden?: boolean | undefined;
|
|
37
|
-
}>, "many">;
|
|
38
|
-
* The config of the experience (JSON)
|
|
39
|
-
*/
|
|
35
|
+
}>, "many">;
|
|
40
36
|
}, "strip", z.ZodTypeAny, {
|
|
41
37
|
baseline: {
|
|
42
38
|
id: string;
|
|
@@ -92,13 +88,17 @@ export declare const Experience: z.ZodObject<{
|
|
|
92
88
|
/**
|
|
93
89
|
* All used variants of the experience (References to other Content Types)
|
|
94
90
|
*/
|
|
95
|
-
variants: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
91
|
+
variants: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{
|
|
96
92
|
id: z.ZodString;
|
|
97
93
|
}, "passthrough", z.ZodTypeAny, {
|
|
98
94
|
id: string;
|
|
99
95
|
}, {
|
|
100
96
|
id: string;
|
|
101
|
-
}>, "many"
|
|
97
|
+
}>, "many">, {
|
|
98
|
+
id: string;
|
|
99
|
+
}[], {
|
|
100
|
+
id: string;
|
|
101
|
+
}[]>>;
|
|
102
102
|
}, "strip", z.ZodTypeAny, {
|
|
103
103
|
audience?: {
|
|
104
104
|
id: string;
|
package/types/Experiment.d.ts
CHANGED
|
@@ -73,13 +73,17 @@ export declare const Experiment: z.ZodObject<z.extendShape<{
|
|
|
73
73
|
}, {
|
|
74
74
|
id: string;
|
|
75
75
|
}>>>;
|
|
76
|
-
variants: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
76
|
+
variants: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{
|
|
77
77
|
id: z.ZodString;
|
|
78
78
|
}, "passthrough", z.ZodTypeAny, {
|
|
79
79
|
id: string;
|
|
80
80
|
}, {
|
|
81
81
|
id: string;
|
|
82
|
-
}>, "many"
|
|
82
|
+
}>, "many">, {
|
|
83
|
+
id: string;
|
|
84
|
+
}[], {
|
|
85
|
+
id: string;
|
|
86
|
+
}[]>>;
|
|
83
87
|
}, {
|
|
84
88
|
type: z.ZodLiteral<"nt_experiment">;
|
|
85
89
|
}>, "strip", z.ZodTypeAny, {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z, ZodTypeAny } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Zod helper for parsing arrays and ignore items not specified in the schema
|
|
4
|
+
*
|
|
5
|
+
* @param zodUnion - union of known types
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')]))
|
|
9
|
+
* type BinaryArray = z.TypeOf<typeof binaryArraySchema>
|
|
10
|
+
*
|
|
11
|
+
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
|
|
12
|
+
* console.log(binaryArray) // ['0', '1', '0']
|
|
13
|
+
*/
|
|
14
|
+
export declare function zodArrayIgnoreUnknown<T extends ZodTypeAny>(zodType: T): z.ZodEffects<z.ZodArray<T, "many">, T["_output"][], T["_input"][]>;
|