zodContract
Adapter, initially created for Contracts that allows you to introduce data validation of the application.
WARNING
Async schemas has not supported yet - related with Contracts protocol returned types:
Async schemas validation return Promise that can't be used with current Contracts protocol
Usage as a Contract
@withease/zod is an adapter based on @withease/contract (used only Contract protocol type) for full compatibility with Effector's ecosystem without additional interop. Just wrap your zod schema into adapter and use as usual Contract.
Farfetched
Farfetched 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.
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 protocol it can be used with any library that supports it.
The full list of libraries that support Contract protocol can be found here.