Skip to content
Merged
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
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,10 @@ import { useForm } from 'react-hook-form';
import { vineResolver } from '@hookform/resolvers/vine';
import vine from '@vinejs/vine';

const schema = vine.compile(
vine.object({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
}),
);
const schema = vine.create({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
});

const App = () => {
const { register, handleSubmit } = useForm({
Expand Down
28 changes: 13 additions & 15 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,13 @@
"@typeschema/core": "^0.14.0",
"@typeschema/main": "^0.14.1",
"@typeschema/zod": "^0.14.0",
"@vinejs/vine": "^3.0.1",
"@vinejs/vine": "^4.4.0",
"@vitejs/plugin-react": "^4.7.0",
"ajv": "^8.20.0",
"ata-validator": "^0.7.3",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
"arktype": "2.0.4",
"ata-validator": "^0.7.3",
"check-export-map": "^1.3.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.4",
Expand Down Expand Up @@ -332,7 +332,7 @@
"@sinclair/typebox": ">=0.25.24",
"@standard-schema/spec": "^1.0.0",
"@typeschema/main": ">=0.13.7",
"@vinejs/vine": "^2.0.0 || ^3.0.0",
"@vinejs/vine": "^2.0.0 || ^3.0.0 || ^4.0.0",
"ajv": "^8.12.0",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion vine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"peerDependencies": {
"react-hook-form": "^7.55.0",
"@hookform/resolvers": "^2.0.0",
"@vinejs/vine": "^2.0.0"
"@vinejs/vine": "^2.0.0 || ^3.0.0 || ^4.0.0"
}
}
10 changes: 4 additions & 6 deletions vine/src/__tests__/Form-native-validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import React from 'react';
import { useForm } from 'react-hook-form';
import { vineResolver } from '..';

const schema = vine.compile(
vine.object({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
}),
);
const schema = vine.create({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
});

interface Props {
onSubmit: (data: Infer<typeof schema>) => void;
Expand Down
10 changes: 4 additions & 6 deletions vine/src/__tests__/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import React from 'react';
import { useForm } from 'react-hook-form';
import { vineResolver } from '..';

const schema = vine.compile(
vine.object({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
}),
);
const schema = vine.create({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
});

function TestComponent({
onSubmit,
Expand Down
54 changes: 26 additions & 28 deletions vine/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ import vine from '@vinejs/vine';
import { InferInput } from '@vinejs/vine/build/src/types';
import { Field, InternalFieldName } from 'react-hook-form';

export const schema = vine.compile(
vine.object({
username: vine.string().regex(/^\w+$/).minLength(3).maxLength(30),
password: vine
.string()
.regex(new RegExp('.*[A-Z].*'))
.regex(new RegExp('.*[a-z].*'))
.regex(new RegExp('.*\\d.*'))
.regex(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'))
.minLength(8)
.confirmed({ confirmationField: 'repeatPassword' }),
repeatPassword: vine.string().sameAs('password'),
accessToken: vine.unionOfTypes([vine.string(), vine.number()]),
birthYear: vine.number().min(1900).max(2013),
email: vine.string().email().optional(),
tags: vine.array(vine.string()),
enabled: vine.boolean(),
like: vine.array(
vine.object({
id: vine.number(),
name: vine.string().fixedLength(4),
}),
),
dateStr: vine
.string()
.transform((value: string) => new Date(value).toISOString()),
}),
);
export const schema = vine.create({
username: vine.string().regex(/^\w+$/).minLength(3).maxLength(30),
password: vine
.string()
.regex(new RegExp('.*[A-Z].*'))
.regex(new RegExp('.*[a-z].*'))
.regex(new RegExp('.*\\d.*'))
.regex(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'))
.minLength(8)
.confirmed({ as: 'repeatPassword' }),
repeatPassword: vine.string().sameAs('password'),
accessToken: vine.unionOfTypes([vine.string(), vine.number()]),
birthYear: vine.number().min(1900).max(2013),
email: vine.string().email().optional(),
tags: vine.array(vine.string()),
enabled: vine.boolean(),
like: vine.array(
vine.object({
id: vine.number(),
name: vine.string().fixedLength(4),
}),
),
dateStr: vine
.string()
.transform((value: string) => new Date(value).toISOString()),
});

export const validData = {
username: 'Doe',
Expand Down
32 changes: 13 additions & 19 deletions vine/src/__tests__/vine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,30 @@ describe('vineResolver', () => {
* Type inference tests
*/
it('should correctly infer the output type from a vine schema', () => {
const resolver = vineResolver(
vine.compile(vine.object({ id: vine.number() })),
);
const schema = vine.create({ id: vine.number() });
const resolver = vineResolver(schema);

expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: number | string }, unknown, { id: number }>
>();
});

it('should correctly infer the output type from a vine schema using a transform', () => {
const resolver = vineResolver(
vine.compile(
vine.object({
id: vine
.number()
.decimal([2, 4])
.transform<string>((val: unknown) => String(val)),
}),
),
);
const schema = vine.create({
id: vine
.number()
.decimal([2, 4])
.transform<string>((val: unknown) => String(val)),
});
const resolver = vineResolver(schema);

expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: number | string }, unknown, { id: string }>
>();
});

it('should correctly infer the output type from a vine schema for the handleSubmit function in useForm', () => {
const schema = vine.compile(vine.object({ id: vine.number() }));
const schema = vine.create({ id: vine.number() });

const {
result: { current: form },
Expand All @@ -106,11 +102,9 @@ describe('vineResolver', () => {
});

it('should correctly infer the output type from a vine schema with a transform for the handleSubmit function in useForm', () => {
const schema = vine.compile(
vine.object({
id: vine.number().transform<string>((val: unknown) => String(val)),
}),
);
const schema = vine.create({
id: vine.number().transform<string>((val: unknown) => String(val)),
});

const {
result: { current: form },
Expand Down
10 changes: 4 additions & 6 deletions vine/src/vine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ export function vineResolver<Input extends FieldValues, Context, Output>(
* @param {boolean} [resolverOptions.raw=false] - If true, returns raw values instead of validated results
* @returns {Resolver<Infer<typeof schema>>} A resolver function compatible with react-hook-form
* @example
* const schema = vine.compile(
* vine.object({
* name: vine.string().minLength(2),
* age: vine.number().min(18)
* })
* );
* const schema = vine.create({
* name: vine.string().minLength(2),
* age: vine.number().min(18)
* });
*
* useForm({
* resolver: vineResolver(schema)
Expand Down
Loading