Converting Function to Use Effect

Hey! It's my first time trying to learn how to use Effect and as a first challenge, I'm trying to convert this function to using Effect, the thing is that I already read all the docs but now I'm like, not sure where to start lol, seems like my mind got saturated with all that new info, also, I'm new to FP programming so...

Any idea how to do it or where to start?

export const getFormMetadata = (form: z.SomeZodObject) => {
  const fields: FieldDefinition[] = [];

  for (const name of Object.keys(form.shape)) {
    const data = form.shape[name]._def as SupportedZodDef;

    const commonProperties = {
      name,
      description: data.description,
      required: data?.typeName !== ZodFirstPartyTypeKind.ZodOptional,
    };

    const type =
      (data as ZodOptionalDef)?.innerType?._def?.typeName ?? data.typeName;

    switch (type) {
      case ZodFirstPartyTypeKind.ZodString:
        fields.push({ ...commonProperties, type: "String" });
        break;

      case ZodFirstPartyTypeKind.ZodEnum:
        fields.push({
          ...commonProperties,
          type: "Enum",
          values: (data as ZodEnumDef).values,
        });
        break;

      case ZodFirstPartyTypeKind.ZodBoolean:
        fields.push({ ...commonProperties, type: "Boolean" });
        break;

      default:
        throw new Error(`Unsupported zod type: ${data.typeName}`);
    }
  }

  return fields;
};
Was this page helpful?