Skip to content

Instantly share code, notes, and snippets.

@colinhacks
Last active October 30, 2025 20:02
Show Gist options
  • Save colinhacks/d35825e505e635df27cc950776c5500b to your computer and use it in GitHub Desktop.
Save colinhacks/d35825e505e635df27cc950776c5500b to your computer and use it in GitHub Desktop.

Adapted from this recommendation by @jandockx

Cyclical objects

Despite supporting recursive schemas, passing cyclical data into Zod will cause an infinite loop in some cases.

You can protect against cyclical objects starting an infinite loop (at a performance cost) with the following approach (using the above jsonSchema as an example):

function isCircular(v: unknown, visited?: Set<unknown>): boolean {
  if (v === null || typeof v !== 'object') {
    return false;
  }

  if (visited?.has(v)) {
    return true;
  }

  const actualVisited = visited ?? new Set<unknown>();
  actualVisited.add(v);

  if (Array.isArray(v)) {
    return v.some(av => isCircular(av, actualVisited));
  }

  return Object.values(v).some(ov => isCircular(ov, actualVisited));
}

const NotCircular = z.unknown().superRefine((val, ctx) => {
  if (isCircular(val)) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      message: 'values cannot be circular data structures',
      fatal: true
    });

    return z.NEVER;
  }
})

const acircularJSONSchema = NotCircular.pipe(jsonSchema);

acircularJSONSchema.parse(data);

When NotCircular fails, pipe will not pass the value into the next schema for evaluation, preventing the infinite loop.

@hbb67441-stack
Copy link

Genuine Documents Centre Online은 시험 없이 최대 5일 만에 완료됩니다. 저희는 신뢰할 수 있는 전문가 및 공식 채널과 협력하여 진품이고 등록되었으며 완벽하게 검증된 문서를 제공합니다. Genuine Documents Centre를 이용하시면 집에서 편안하게 주문하실 수 있습니다. 줄을 설 필요도, 스트레스도 없습니다. 고객님의 집 앞이나 원하시는 장소로 직접 배송해 드립니다. 저희 배송 서비스는 100% 신중하고 빠르며 신뢰할 수 있어 모든 단계에서 고객님의 프라이버시와 안심을 보장합니다. Genuine Documents Centre Online ( 정품 문서 센터 온라인 홈페이지 https://Genuinedocumentscentre.com/ko/ )가 도와드리겠습니다. 시험 없이 운전면허증, 여권 재발급, 해외여행 비자, 거주 허가증 등 어떤 서류든 간편하고 스트레스 없이 발급받으실 수 있습니다. 집에서 편안하게 모든 것을 주문하실 수 있으며, 나머지는 저희가 처리해 드립니다. 지금 바로 Genuine Documents Centre Online에서 주문하세요.
정품 문서 센터 – 전 세계 정품 문서에 대한 신뢰할 수 있는 소스
운전면허증, 여권, 신분증, 거주 허가증, 비자, 선박 면허증 등 진본 서류를 받는 것은 특히 긴 대기 시간, 시험, 또는 복잡한 절차를 거치고 싶지 않다면 부담스러울 수 있습니다. Genuine Documents Centre에서는 전 세계 개인에게 빠르고 안전하며 합법적인 솔루션을 제공합니다. 2003년부터 수천 명의 고객이 시험 없이 진본 서류를 발급받을 수 있도록 지원해 왔으며, 단 5일 만에 서류를 받아볼 수 있었습니다.
운전면허증을 버몬트주로 이전하려면 어떻게 해야 하나요? https://genuinedocumentscentre.com/how-do-i-transfer-my-drivers-license-to-vermont/

@hbb67441-stack
Copy link

Genuine Documents Centre Online senza esami e test in soli 5 giorni al massimo. Collaboriamo con professionisti fidati e canali ufficiali per fornire documenti autentici, registrati e completamente verificabili. Con Genuine Documents Centre, puoi effettuare il tuo ordine comodamente da casa, senza code e senza stress. Consegniamo direttamente a casa tua o in qualsiasi luogo tu scelga. Il nostro servizio di consegna è discreto, veloce e affidabile al 100%, garantendo la tua privacy e tranquillità in ogni fase del processo. Genuine Documents Centre Online ( sede di Genuine Documents Centre Online https://Genuinedocumentscentre.com/ ) è qui per te, che tu abbia bisogno di una patente di guida senza esami, di una sostituzione del passaporto, di un visto per viaggiare all’estero o di un permesso di soggiorno, rendiamo il processo semplice e senza stress. Puoi ordinare tutto comodamente da casa tua e noi ci occupiamo del resto. Ordina oggi stesso da Genuine Documents Centre Online.
Genuine Documents Centre: la tua fonte affidabile per documenti autentici in tutto il mondo
Ottenere documenti autentici come patente di guida, passaporto, carta d’identità, permesso di soggiorno, visto o patente nautica può essere complicato, soprattutto se non si vogliono affrontare lunghe attese, esami o procedure complicate. Presso Genuine Documents Centre, offriamo soluzioni rapide, sicure e legali per privati ​​in tutto il mondo. Dal 2003, abbiamo aiutato migliaia di clienti a ottenere documenti autentici e autentici senza dover sostenere esami, con consegna in soli 5 giorni.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment