Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Apr 30, 2024
1 parent 48885b5 commit 569bf35
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 84 deletions.
43 changes: 1 addition & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1959,48 +1959,7 @@ Thanks to [ggoodman](https://github.com/ggoodman) for suggesting this.

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):

```ts
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.
> To detect cyclical objects before they cause problems, consider [this approach](https://gist.github.com/colinhacks/d35825e505e635df27cc950776c5500b).
## Promises

Expand Down
43 changes: 1 addition & 42 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1959,48 +1959,7 @@ Thanks to [ggoodman](https://github.com/ggoodman) for suggesting this.

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):

```ts
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.
> To detect cyclical objects before they cause problems, consider [this approach](https://gist.github.com/colinhacks/d35825e505e635df27cc950776c5500b).
## Promises

Expand Down

0 comments on commit 569bf35

Please sign in to comment.