Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

749 B
Raw Blame
import { dequal } from 'dequal/lite';
import { z } from 'zod/v3';

import type { Serializable } from '../types';

export const parseLiteralValues = (values: ReadonlyArray<Serializable>): z.ZodTypeAny => {
  if (values.length === 0) return z.never();

  if (values.every(value => value === null || typeof value !== 'object')) {
    if (values.length === 1) return z.literal(values[0] as z.Primitive);
    if (values.every(value => typeof value === 'string')) return z.enum(values as [string]);
    return z.union(
      values.map(value => z.literal(value as z.Primitive)) as unknown as [
        z.ZodTypeAny,
        z.ZodTypeAny,
      ]
    );
  }

  return z.custom<Serializable>(value => values.some(candidate => dequal(value, candidate)));
};