Add support for more advanced forms of overriding

This commit is contained in:
Dermot Duffy
2024-04-13 18:23:11 -07:00
parent 0eca5d780c
commit 323925101d
21 changed files with 855 additions and 285 deletions
+20 -2
View File
@@ -243,8 +243,8 @@ export const getChildrenFromElement = (parent: HTMLElement): HTMLElement[] => {
return children.filter(isHTMLElement);
};
export const recursivelyMergeObjectsNotArrays = <T>(src1: T, src2: T): T => {
return mergeWith({}, src1, src2, (_a, b) => (Array.isArray(b) ? b : undefined));
export const recursivelyMergeObjectsNotArrays = <T>(target: T, src1: T, src2: T): T => {
return mergeWith(target, src1, src2, (_a, b) => (Array.isArray(b) ? b : undefined));
};
export const aspectRatioToString = (options?: {
@@ -268,3 +268,21 @@ export const aspectRatioToStyle = (options?: {
'aspect-ratio': aspectRatioToString(options),
};
};
/**
* Remove empty slots from nested arrays.
*/
export const desparsifyArrays = <T>(data: T): T => {
if (Array.isArray(data)) {
return <T>(
data.filter((item) => item !== undefined).map((item) => desparsifyArrays(item))
);
} else if (typeof data === 'object' && data !== null) {
const result: Record<string | number | symbol, unknown> = {};
for (const key in data) {
result[key] = desparsifyArrays(data[key]);
}
return <T>result;
}
return data;
};
+4 -1
View File
@@ -28,7 +28,10 @@ export function deepRemoveDefaults<T extends z.ZodTypeAny>(schema: T): any {
}
if (schema instanceof z.ZodArray) {
return z.ZodArray.create(deepRemoveDefaults(schema.element));
return z.ZodArray.create(deepRemoveDefaults(schema.element))
.min(schema._def.minLength?.value, schema._def.minLength?.message)
.max(schema._def.maxLength?.value, schema._def.maxLength?.message)
.length(schema._def.exactLength?.value, schema._def.exactLength?.message);
}
if (schema instanceof z.ZodOptional) {