* Add handlePaste function and integrate it with options fields * Prevents function running if user does not paste a list, allows list items to be pasted without deleting following items * Subs useState for rhf useWatch to fix issues with multiple form fields * Sets options array as optional property in zod schema * Assigns default value to options array to fix possibly undefined type errors * Sets placedholder property as optional * maps selectText to options on mount * Removes comma & semicolon delimiting for pasted lists (new line only) & defines regex as a constant * Sets default value of placeholder to empty string, provides default fallback for component text field if placeholder is omitted. * Fix type error on placeholder * Extracts handlePaste to service function & tests thoroughly * Some improvements and tests arent needed * More fixes * Fix reporting as well * TS fixes * Do transforms to and fro from label * Add unit tests * Unit test reporting * Test Improvements * Self review feedback addressed --------- Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: Anik Dhabal Babu <[email protected]>
100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import { z } from "zod";
|
|
|
|
export const zodNonRouterField = z.object({
|
|
id: z.string(),
|
|
label: z.string(),
|
|
identifier: z.string().optional(),
|
|
placeholder: z.string().optional(),
|
|
type: z.string(),
|
|
/**
|
|
* @deprecated in favour of `options`
|
|
*/
|
|
selectText: z.string().optional(),
|
|
required: z.boolean().optional(),
|
|
deleted: z.boolean().optional(),
|
|
options: z
|
|
.array(
|
|
z.object({
|
|
label: z.string(),
|
|
// To keep backwards compatibility with the options generated from legacy selectText, we allow saving null as id
|
|
// It helps in differentiating whether the routing logic should consider the option.label as value or option.id as value.
|
|
// This is important for legacy routes which has option.label saved in conditions and it must keep matching with the value of the option
|
|
id: z.string().or(z.null()),
|
|
})
|
|
)
|
|
.optional(),
|
|
});
|
|
|
|
export const zodRouterField = zodNonRouterField.extend({
|
|
routerId: z.string(),
|
|
});
|
|
|
|
// This ordering is important - If routerId is present then it should be in the parsed object. Moving zodNonRouterField to first position doesn't do that
|
|
export const zodField = z.union([zodRouterField, zodNonRouterField]);
|
|
export const zodFields = z.array(zodField).optional();
|
|
|
|
export const zodNonRouterFieldView = zodNonRouterField;
|
|
export const zodRouterFieldView = zodRouterField.extend({
|
|
routerField: zodNonRouterFieldView,
|
|
router: z.object({
|
|
name: z.string(),
|
|
description: z.string(),
|
|
id: z.string(),
|
|
}),
|
|
});
|
|
/**
|
|
* Has some additional fields that are not supposed to be saved to DB but are required for the UI
|
|
*/
|
|
export const zodFieldView = z.union([zodNonRouterFieldView, zodRouterFieldView]);
|
|
|
|
export const zodFieldsView = z.array(zodFieldView).optional();
|
|
|
|
export const zodNonRouterRoute = z.object({
|
|
id: z.string(),
|
|
queryValue: z.object({
|
|
id: z.string().optional(),
|
|
type: z.union([z.literal("group"), z.literal("switch_group")]),
|
|
children1: z.any(),
|
|
properties: z.any(),
|
|
}),
|
|
isFallback: z.boolean().optional(),
|
|
action: z.object({
|
|
// TODO: Make it a union type of "customPageMessage" and ..
|
|
type: z.union([
|
|
z.literal("customPageMessage"),
|
|
z.literal("externalRedirectUrl"),
|
|
z.literal("eventTypeRedirectUrl"),
|
|
]),
|
|
value: z.string(),
|
|
}),
|
|
});
|
|
|
|
export const zodNonRouterRouteView = zodNonRouterRoute;
|
|
|
|
export const zodRouterRoute = z.object({
|
|
// This is the id of the Form being used as router
|
|
id: z.string(),
|
|
isRouter: z.literal(true),
|
|
});
|
|
|
|
export const zodRoute = z.union([zodNonRouterRoute, zodRouterRoute]);
|
|
|
|
export const zodRouterRouteView = zodRouterRoute.extend({
|
|
//TODO: Extend it from form
|
|
name: z.string(),
|
|
description: z.string().nullable(),
|
|
routes: z.array(z.union([zodRoute, z.null()])),
|
|
});
|
|
|
|
export const zodRoutes = z.union([z.array(zodRoute), z.null()]).optional();
|
|
|
|
export const zodRouteView = z.union([zodNonRouterRouteView, zodRouterRouteView]);
|
|
|
|
export const zodRoutesView = z.union([z.array(zodRouteView), z.null()]).optional();
|
|
|
|
// TODO: This is a requirement right now that zod.ts file (if it exists) must have appDataSchema export(which is only required by apps having EventTypeAppCard interface)
|
|
// This is a temporary solution and will be removed in future
|
|
export const appDataSchema = z.any();
|
|
|
|
export const appKeysSchema = z.object({});
|