Skip to content

Commit

Permalink
feat: finish implementation and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Jul 8, 2024
1 parent 28bfcfa commit 30bce58
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 4 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,80 @@ declare zod schemas that can be inverted to format from output to input
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![npm version](https://badge.fury.io/js/zod-invertible.svg)](https://badge.fury.io/js/zod-invertible)

# Example

```sh
npm i zod-invertible
# OR
pnpm i zod-invertible
```

```ts
import z from 'zod'
import { invertible, invert } from 'zod-intertible'

const StringToNumber = invertible(
z.string(),
(s, ctx) => {
const result = parseFloat(s)
if (isNaN(result)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'invalid float',
})
}
return result
},
z.number(),
(n) => String(n)
)

StringToNumber.parse('3.5') // 3.5

const NumberToString = invert(StringToNumber)

NumberToString.parse(3.5) // '3.5'

const obj = z.object({ foo: StringToNumber })
obj.parse({ foo: '3.5' }) // { foo: 3.5 }
// invert works deeply:
invert(obj).parse({ foo: 3.5 }) // { foo: '3.5' }
```

## `invertible(inputSchema, parse, outputSchema, format)`

Creates an invertible schema that transforms from an input type to a different output type.

### `inputSchema`

The `ZodType` for validating the input value

### `parse`

The function for transforming the input value into the output value. It is called with two arguments:

- `value`: the output of `inputSchema`
- `ctx`: the zod `RefinementCtx`

`parse` may be `async`.

### `outputSchema`

The `ZodType` for validating the output

### `format`

The function for converting from the output value back into the input value. It is called with two arguments:

- `value`: the input of `outputSchema`
- `ctx`: the zod `RefinementCtx`

`format` may be `async`.

## `invert(schema)`

Deeply inverts a zod schema, inverting any `ZodInvertible` schemas inside it, and otherwise preserving the structure of
objects, arrays, etc.

If the zod schema parses input type `I` into output type `O`, the inverted schema will parse input type `O` into output type `I`.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
"@jcoreio/toolchain-mocha": "^4.6.0",
"@jcoreio/toolchain-semantic-release": "^4.6.0",
"@jcoreio/toolchain-typescript": "^4.6.0",
"@types/chai": "^4.0.0",
"@types/mocha": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"chai": "^4.0.0",
"eslint": "^8.56.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"mocha": "^10.2.0",
"mocha": "^10.4.0",
"typescript": "^5.1.0"
},
"version": "0.0.0-development",
Expand All @@ -46,6 +49,7 @@
},
"packageManager": "pnpm@8.11.0",
"dependencies": {
"@babel/runtime": "^7.18.6"
"@babel/runtime": "^7.18.6",
"zod": "^3.23.8"
}
}
75 changes: 73 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 30bce58

Please sign in to comment.