Skip to content

Commit

Permalink
Fixing and standardizing Zod example (#90)
Browse files Browse the repository at this point in the history
* Fixed Zod example

* tsConfig -> tsconfig

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
  • Loading branch information
colinhacks and Colin McDonnell authored Dec 10, 2020
1 parent 9e6135e commit 634dab7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,36 @@ TypeScript-first schema validation with static type inference

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

```typescript jsx
> ⚠️ Example below uses the `valueAsNumber`, which requires `react-hook-form` v6.12.0 (released Nov 28, 2020) or later.
```tsx
import React from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';

const schema = z.object({
username: z.string(),
name: z.string().nonempty({ message: 'Required' }),
age: z.number().min(10),
});

const App = () => {
const { register, handleSubmit } = useForm({
const { register, handleSubmit, errors } = useForm({
resolver: zodResolver(schema),
});

return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input name="name" ref={register} />
<input name="age" type="number" ref={register} />
{errors.name?.message && <p>{errors.name?.message}</p>}
<input name="age" type="number" ref={register({ valueAsNumber: true })} />
{errors.age?.message && <p>{errors.age?.message}</p>}
<input type="submit" />
</form>
);
};

export default App;
```

### [Superstruct](https://github.com/ianstormtaylor/superstruct)
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
},
globals: {
'ts-jest': {
tsConfig: 'tsconfig.jest.json',
tsconfig: 'tsconfig.jest.json',
},
},
testMatch: ['**/?(*.)+(spec|test).ts?(x)'],
Expand Down

0 comments on commit 634dab7

Please sign in to comment.