Skip to content

Commit

Permalink
feat: add typeschema resolver (#699)
Browse files Browse the repository at this point in the history
* feat(typeschema): add typeschema resolver

* fix(arktype): exclude arktype from resolver bundle (#693)

* perf(valibot): reduce bundle size

* perf(vine): reduce bundle size

* chore(deps): update dependencies

* chore: migrate to Bun & Biome



---------

Co-authored-by: Trent Cox <admin@havenworldtours.com>

* Update node version (#680)

* Update main.yml (#681)

* perf(typeschemaResolver): save some bytes

* perf(arktype): reduce bundle size

* perf: reduce bundle size

* chore: update deps

* chore: update all deps

---------

Co-authored-by: André Costa <andrefonsecacosta@gmail.com>
Co-authored-by: Trent Cox <admin@havenworldtours.com>
Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
  • Loading branch information
4 people authored Jul 4, 2024
1 parent 4b9acb7 commit 18e423f
Show file tree
Hide file tree
Showing 128 changed files with 1,430 additions and 8,044 deletions.
33 changes: 0 additions & 33 deletions .eslintrc.js

This file was deleted.

13 changes: 8 additions & 5 deletions .github/workflows/compressedSize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.0
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup bun
uses: oven-sh/setup-bun@v1

- uses: preactjs/compressed-size-action@v2
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
install-script: 'bun i'
build-script: 'bun run build'
45 changes: 14 additions & 31 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,20 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.0
- name: Setup bun
uses: oven-sh/setup-bun@v1

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: Install dependencies
run: pnpm install
- name: Install Dependencies
run: bun install

- name: Lint
run: |
pnpm lint
pnpm lint:types
- name: Test
run: pnpm test
run: bun lint

- name: TS
run: bun lint:types

- name: Build
run: pnpm build
run: bun run build

publish-module:
name: 'Publish Module to NPM on Node ${{ matrix.node }} and ${{ matrix.os }}'
Expand All @@ -53,22 +43,15 @@ jobs:
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.0

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'
- name: Setup bun
uses: oven-sh/setup-bun@v1

- name: Install dependencies
run: pnpm install
- name: Install Dependencies
run: bun install

- name: Build
run: pnpm build
run: bun run build

- name: Publish
run: npx semantic-release
Expand Down
1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

60 changes: 57 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm lint:types
pnpm lint-staged
if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
set -x
fi

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

call_lefthook()
{
if test -n "$LEFTHOOK_BIN"
then
"$LEFTHOOK_BIN" "$@"
elif lefthook -h >/dev/null 2>&1
then
lefthook "$@"
else
dir="$(git rev-parse --show-toplevel)"
if test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"
else
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')
if test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"

elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1
then
swift package --disable-sandbox plugin lefthook "$@"
elif command -v mint >/dev/null 2>&1
then
mint run csjones/lefthook-plugin "$@"
elif command -v npx >/dev/null 2>&1
then
npx lefthook "$@"
else
echo "Can't find lefthook in PATH"
fi
fi
fi
}

call_lefthook run "pre-commit" "$@"
59 changes: 59 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/sh

if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
set -x
fi

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

call_lefthook()
{
if test -n "$LEFTHOOK_BIN"
then
"$LEFTHOOK_BIN" "$@"
elif lefthook -h >/dev/null 2>&1
then
lefthook "$@"
else
dir="$(git rev-parse --show-toplevel)"
if test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"
else
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')
if test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"

elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1
then
swift package --disable-sandbox plugin lefthook "$@"
elif command -v mint >/dev/null 2>&1
then
mint run csjones/lefthook-plugin "$@"
elif command -v npx >/dev/null 2>&1
then
npx lefthook "$@"
else
echo "Can't find lefthook in PATH"
fi
fi
fi
}

call_lefthook run "prepare-commit-msg" "$@"
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ Here is a quick guide to doing code contributions to the library.

3. Install packages by running:

> pnpm install
> bun install
4. If you've added a code that should be tested, ensure the test suite still passes.

> pnpm test
> bun test
5. Try to write some unit tests to cover as much of your code as possible.

6. Ensure your code lints without errors.

> pnpm lint
> bun lint
7. Ensure build passes.

> pnpm build
> bun run build
8. Push your branch: `git push -u origin your-meaningful-branch-name`

Expand All @@ -36,7 +36,7 @@ Here is a quick guide to doing code contributions to the library.

## Coding style

Please follow the coding style of the project. React Hook Form uses eslint and prettier. If possible, enable their respective plugins in your editor to get real-time feedback. The linting can be run manually with the following command: `pnpm lint`
Please follow the coding style of the project. React Hook Form uses eslint and prettier. If possible, enable their respective plugins in your editor to get real-time feedback. The linting can be run manually with the following command: `bun lint`

## License

Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [With `TypeCompiler`](#with-typecompiler)
- [ArkType](#arktype)
- [Valibot](#valibot)
- [TypeSchema](#typeschema)
- [effect-ts](#effect-ts)
- [VineJS](#vinejs)
- [Backers](#backers)
Expand Down Expand Up @@ -615,6 +616,38 @@ const App = () => {
};
```

### [TypeSchema](https://typeschema.com)

Universal adapter for schema validation, compatible with [any validation library](https://typeschema.com/#coverage)

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

```typescript jsx
import { useForm } from 'react-hook-form';
import { typeschemaResolver } from '@hookform/resolvers/typeschema';
import * as z from 'zod';

// Use your favorite validation library
const schema = z.object({
username: z.string().min(1, { message: 'Required' }),
password: z.number().min(1, { message: 'Required' }),
});

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

return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```

### [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.
Expand Down
8 changes: 7 additions & 1 deletion ajv/src/__tests__/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ajvResolver } from '..';
import { fields, invalidData, invalidDataWithUndefined, schema, validData } from './__fixtures__/data';
import {
fields,
invalidData,
invalidDataWithUndefined,
schema,
validData,
} from './__fixtures__/data';

const shouldUseNativeValidation = false;

Expand Down
2 changes: 1 addition & 1 deletion ajv/src/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import Ajv, { DefinedError } from 'ajv';
import ajvErrors from 'ajv-errors';
import { appendErrors, FieldError } from 'react-hook-form';
import { FieldError, appendErrors } from 'react-hook-form';
import { Resolver } from './types';

const parseErrorSchema = (
Expand Down
6 changes: 1 addition & 5 deletions ajv/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
FieldValues,
ResolverOptions,
ResolverResult,
} from 'react-hook-form';
import * as Ajv from 'ajv';
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';

export type Resolver = <T>(
schema: Ajv.JSONSchemaType<T>,
Expand Down
3 changes: 2 additions & 1 deletion arktype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "MIT",
"peerDependencies": {
"react-hook-form": "^7.0.0",
"@hookform/resolvers": "^2.0.0"
"@hookform/resolvers": "^2.0.0",
"arktype": "2.0.0-dev.14"
}
}
Loading

0 comments on commit 18e423f

Please sign in to comment.