Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-buckets-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@withease/zod': patch
---

Increased package size limit
5 changes: 5 additions & 0 deletions .changeset/sharp-trees-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@withease/zod': major
---

Initial release: Zod package adapter package
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dist
node_modules
.DS_Store
.idea
*.log

# Playwright
Expand Down
11 changes: 11 additions & 0 deletions apps/website/docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default defineConfig({
{ text: 'web-api', link: '/web-api/' },
{ text: 'factories', link: '/factories/' },
{ text: 'contracts', link: '/contracts/' },
{ text: 'zod', link: '/zod/' },
],
},
{ text: 'Magazine', link: '/magazine/' },
Expand Down Expand Up @@ -140,6 +141,16 @@ export default defineConfig({
},
{ text: 'APIs', link: '/contracts/api' },
]),
...createSidebar('zod', [
{ text: 'Get Started', link: '/zod/' },
{ text: 'Compatibility', link: '/zod/compatibility' },
{
text: "API",
items: [
{ text: "zodContract", link: '/zod/api/contract/' }
]
}
]),
'/magazine/': [
{
text: 'Architecture',
Expand Down
54 changes: 54 additions & 0 deletions apps/website/docs/zod/api/contract/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# zodContract

Adapter, initially created for [_Contracts_](/protocols/contract) that allows you to introduce data validation of the application.

::: warning
Async schemas has not supported yet - related with [_Contracts protocol_](/protocols/contract) returned types:\
Async schemas validation return Promise that can't be used with current [_Contracts protocol_](/protocols/contract)
:::

## Usage as a _Contract_

`@withease/zod` is an adapter based on `@withease/contract` (used only [_Contract_ protocol](/protocols/contract) type) for full compatibility with Effector's ecosystem without additional interop. Just wrap your zod schema into adapter and use as usual [_Contract_](/protocols/contract).

### Farfetched

[Farfetched](https://ff.effector.dev) is the advanced data fetching tool for web applications based of Effector. It suggests to ensure that data received from the server is conforms desired [_Contract_](/protocols/contract).

```ts
import { createQuery } from '@farfetched/core';
import { z } from 'zod';
import { zodContract } from '@withease/zod';

const CharacterSchema = z.object({
id: z.string(),
name: z.string(),
status: StatusSchema,
species: z.string(),
type: z.string(),
gender: GenderSchema,
origin: z.object({ name: z.string(), url: z.string() }),
location: z.object({ name: z.string(), url: z.string() }),
image: z.string(),
episode: z.array(z.string()),
});

const characterQuery = createQuery({
effect: createEffect(async (config: { id: number }) => {
const response = await fetch(
`https://rickandmortyapi.com/api/character/${config.id}`
);
return response.json();
}),
// after receiving data from the server
// check if it is conforms the Contract to ensure
// API does not return something unexpected
contract: zodContract(CharacterSchema),
});
```

### Integration with other libraries

Since _zodContract_ (`@withease/zod`) is compatible [_Contract_](/protocols/contract) protocol it can be used with any library that supports it.

The full list of libraries that support _Contract_ protocol can be found [here](/protocols/contract).
30 changes: 30 additions & 0 deletions apps/website/docs/zod/compatibility.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Compatibility

The best practice is to use a supported version of [Zod](https://zod.dev/). Preferably, opt for Zod@4. However, we recognize the need to support transitional versions to assist with migrations.

## With zod@^4

You can use standard zod import.

```ts
import { z } from 'zod';
import { zodContract } from '@withease/zod';
```

## With zod@^3.25.0

You still can use latest versions of zod@3 for migrations we support it.

```ts
import { z } from 'zod/v3';
import { zodContract } from '@withease/zod';
```

## Before [email protected]

You can use adapter from [Farfetched](https://ff.effector/)

```ts
import { z } from 'zod';
import { zodContract } from '@farfetched/zod';
```
39 changes: 39 additions & 0 deletions apps/website/docs/zod/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<script setup>
import pkg from '../../../../packages/zod/package.json';

const maxSize = pkg['size-limit'].at(0).limit;
</script>

# @withease/zod

Small adapters (less than **{{maxSize}}** summary controlled by CI) for [Zod](https://zod.dev/) package support.

## Installation

First, you need to install package:

::: code-group

```sh [pnpm]
pnpm install @withease/zod
```

```sh [yarn]
yarn add @withease/zod
```

```sh [npm]
npm install @withease/zod
```

:::

## Version compatibility

Best way of usage - use a supported zod version. zod@4 is preferred.\
But we understand the need to maintain a transitional versions to assist with migrations.\
[Read more](./compatibility)

## API

- [zodContract](./api/contract/)
3 changes: 3 additions & 0 deletions packages/zod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @withease/zod

Read documentation [here](https://withease.effector.dev/zod/).
50 changes: 50 additions & 0 deletions packages/zod/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@withease/zod",
"version": "0.1.0",
"license": "MIT",
"repository": "https://github.com/igorkamyshev/withease",
"scripts": {
"test:run": "vitest run --typecheck",
"test:watch": "vitest --typecheck",
"build": "vite build",
"size": "size-limit",
"publint": "node ../../tools/publint.mjs",
"typelint": "attw --pack"
},
"peerDependencies": {
"@withease/contracts": "workspace:*",
"zod": "^3.25.0 || ^4.0.0"
},
"devDependencies": {
"@withease/contracts": "workspace:*",
"zod": "^3.25.0 || ^4.0.0"
},
"type": "module",
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"main": "./dist/zod.cjs",
"module": "./dist/zod.js",
"types": "./dist/zod.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/zod.d.ts",
"default": "./dist/zod.js"
},
"require": {
"types": "./dist/zod.d.cts",
"default": "./dist/zod.cjs"
}
}
},
"size-limit": [
{
"path": "./dist/zod.js",
"limit": "315 B"
}
]
}
Loading