@ninetailed/experience.js-utils
Advanced tools
Comparing version 2.2.9-beta.0 to 2.2.10-beta.0
@@ -34,2 +34,33 @@ import { logger } from '@ninetailed/experience.js'; | ||
/** | ||
* Zod helper for parsing arrays and ignore items not specified in the schema | ||
* | ||
* @param zodUnion - union of known types | ||
* | ||
* @example | ||
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')])) | ||
* type BinaryArray = z.TypeOf<typeof binaryArraySchema> | ||
* | ||
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0']) | ||
* console.log(binaryArray) // ['0', '1', '0'] | ||
*/ | ||
function zodArrayIgnoreUnknown(zodType) { | ||
const isKnownItem = item => zodType.safeParse(item).success; | ||
return z.preprocess(val => toSafeArray(val).filter(isKnownItem), z.array(zodType)); | ||
} | ||
function toSafeArray(item) { | ||
if (isArray(item)) { | ||
return item; | ||
} | ||
return [item]; | ||
} | ||
function isArray(item) { | ||
return Array.isArray(item); | ||
} | ||
const Experience = z.object({ | ||
@@ -61,3 +92,3 @@ id: z.string(), | ||
*/ | ||
variants: z.array(Variant).default([]) | ||
variants: zodArrayIgnoreUnknown(Variant).default([]) | ||
}); | ||
@@ -64,0 +95,0 @@ |
@@ -63,2 +63,37 @@ (function (global, factory) { | ||
/** | ||
* Zod helper for parsing arrays and ignore items not specified in the schema | ||
* | ||
* @param zodUnion - union of known types | ||
* | ||
* @example | ||
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')])) | ||
* type BinaryArray = z.TypeOf<typeof binaryArraySchema> | ||
* | ||
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0']) | ||
* console.log(binaryArray) // ['0', '1', '0'] | ||
*/ | ||
function zodArrayIgnoreUnknown(zodType) { | ||
var isKnownItem = function (item) { | ||
return zodType.safeParse(item).success; | ||
}; | ||
return zod.z.preprocess(function (val) { | ||
return toSafeArray(val).filter(isKnownItem); | ||
}, zod.z.array(zodType)); | ||
} | ||
function toSafeArray(item) { | ||
if (isArray(item)) { | ||
return item; | ||
} | ||
return [item]; | ||
} | ||
function isArray(item) { | ||
return Array.isArray(item); | ||
} | ||
var Experience = zod.z.object({ | ||
@@ -90,3 +125,3 @@ id: zod.z.string(), | ||
*/ | ||
variants: zod.z.array(Variant).default([]) | ||
variants: zodArrayIgnoreUnknown(Variant).default([]) | ||
}); | ||
@@ -93,0 +128,0 @@ |
{ | ||
"name": "@ninetailed/experience.js-utils", | ||
"version": "2.2.9-beta.0", | ||
"version": "2.2.10-beta.0", | ||
"main": "./index.umd.js", | ||
@@ -8,5 +8,5 @@ "module": "./index.esm.js", | ||
"dependencies": { | ||
"@ninetailed/experience.js": "2.2.9-beta.0", | ||
"@ninetailed/experience.js": "2.2.10-beta.0", | ||
"analytics": "^0.8.0", | ||
"@ninetailed/experience.js-shared": "2.2.9-beta.0", | ||
"@ninetailed/experience.js-shared": "2.2.10-beta.0", | ||
"uuid": "^8.3.2", | ||
@@ -13,0 +13,0 @@ "ts-toolbelt": "^9.6.0", |
@@ -25,5 +25,3 @@ import { z } from 'zod'; | ||
id?: string | undefined; | ||
}>; /** | ||
* The type if the experience (nt_experiment | nt_personalization) | ||
*/ | ||
}>; | ||
variants: z.ZodArray<z.ZodObject<{ | ||
@@ -38,5 +36,3 @@ id: z.ZodDefault<z.ZodString>; | ||
hidden?: boolean | undefined; | ||
}>, "many">; /** | ||
* The config of the experience (JSON) | ||
*/ | ||
}>, "many">; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -97,3 +93,3 @@ baseline: { | ||
*/ | ||
variants: z.ZodDefault<z.ZodArray<z.ZodObject<{ | ||
variants: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{ | ||
id: z.ZodString; | ||
@@ -104,3 +100,7 @@ }, "passthrough", z.ZodTypeAny, { | ||
id: string; | ||
}>, "many">>; | ||
}>, "many">, { | ||
id: string; | ||
}[], { | ||
id: string; | ||
}[]>>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -107,0 +107,0 @@ audience?: { |
@@ -76,3 +76,3 @@ import { z } from 'zod'; | ||
}>>>; | ||
variants: z.ZodDefault<z.ZodArray<z.ZodObject<{ | ||
variants: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{ | ||
id: z.ZodString; | ||
@@ -83,3 +83,7 @@ }, "passthrough", z.ZodTypeAny, { | ||
id: string; | ||
}>, "many">>; | ||
}>, "many">, { | ||
id: string; | ||
}[], { | ||
id: string; | ||
}[]>>; | ||
}, { | ||
@@ -86,0 +90,0 @@ type: z.ZodLiteral<"nt_experiment">; |
24136
14
719
+ Added@ninetailed/experience.js@2.2.10-beta.0(transitive)
+ Added@ninetailed/experience.js-shared@2.2.10-beta.0(transitive)
- Removed@ninetailed/experience.js@2.2.9-beta.0(transitive)
- Removed@ninetailed/experience.js-shared@2.2.9-beta.0(transitive)