Skip to content

Commit

Permalink
feat: add ignoreEffect helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Oct 24, 2024
1 parent 7ccbfdd commit 6358273
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ declare zod schemas that can be inverted to format from output to input
- `z.preprocess(...)`
- `ZodType.transform(...)` (outside of the transform created by `invertible`)

However, you may work around this by attaching a symbol:
However, you may work around this by marking the schema with `ignoreEffect`:

```ts
import { schema, IgnoreEffect } from 'zod-intervible'
import { schema, ignoreEffect } from 'zod-intervible'

const innerSchema = ...
const schema = z.preprocess((value) => ..., innerSchema)
;(schema as any)[IgnoreEffect] = true
const schema = ignoreEffect(z.preprocess((value) => ..., innerSchema))
const inverse = invert(schema) // equivalent to invert(innerSchema)
```

Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export function invertible<T extends z.ZodTypeAny, U extends z.ZodTypeAny>(

export const IgnoreEffect = Symbol('IgnoreEffect')

export function ignoreEffect<T extends z.ZodEffects<any, any, any>>(
schema: T
): T {
;(schema as any)[IgnoreEffect] = true
return schema
}

export function invert<T extends z.ZodTypeAny>(
schema: T
): z.ZodType<z.input<T>, any, z.output<T>> {
Expand Down

0 comments on commit 6358273

Please sign in to comment.