Skip to content

Commit

Permalink
docs(effect-ts): add quickstart guide to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Trent Cox committed May 8, 2024
1 parent f8d4fd3 commit b6ee1f8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- [TypeBox](#typebox)
- [ArkType](#arktype)
- [Valibot](#valibot)
- [effect-ts](#effect-ts)
- [Backers](#backers)
- [Sponsors](#sponsors)
- [Contributors](#contributors)
Expand Down Expand Up @@ -576,6 +577,57 @@ const App = () => {
};
```

### [effect-ts](https://github.com/Effect-TS/effect)

A powerful TypeScript framework that provides a fully-fledged functional effect system with a rich standard library.

[![npm](https://img.shields.io/bundlephobia/minzip/effect?style=for-the-badge)]

```typescript jsx
import React from 'react';
import { useForm } from 'react-hook-form';
import { effectTsResolver } from '@hookform/resolvers/effect-ts';
import { Schema } from '@effect/schema';

const schema = Schema.Struct({
username: Schema.String.pipe(
Schema.nonEmpty({ message: () => 'username required' }),
),
password: Schema.String.pipe(
Schema.nonEmpty({ message: () => 'password required' }),
),
});

type FormData = Schema.Schema.Type<typeof schema>;

interface Props {
onSubmit: (data: FormData) => void;
}

function TestComponent({ onSubmit }: Props) {
const {
register,
handleSubmit,
formState: { errors },
// provide generic if TS has issues inferring types
} = useForm<FormData>({
resolver: effectTsResolver(schema),
});

return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register('username')} />
{errors.username && <span role="alert">{errors.username.message}</span>}

<input {...register('password')} />
{errors.password && <span role="alert">{errors.password.message}</span>}

<button type="submit">submit</button>
</form>
);
}
```

## Backers

Thanks goes to all our backers! [[Become a backer](https://opencollective.com/react-hook-form#backer)].
Expand Down
4 changes: 2 additions & 2 deletions effect-ts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Schema } from '@effect/schema';
import { ParseOptions } from '@effect/schema/AST';
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';

export type Resolver = <TFieldValues extends FieldValues, TContext = any>(
export type Resolver = <TFieldValues extends FieldValues, TContext>(
schema: Schema.Schema<TFieldValues>,
config?: ParseOptions,
) => (
values: FieldValues,
context: TContext | undefined,
_context: TContext | undefined,
options: ResolverOptions<TFieldValues>,
) => Promise<ResolverResult<TFieldValues>>;

0 comments on commit b6ee1f8

Please sign in to comment.