v1.0 with SW PWA enabled
This commit is contained in:
81
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
81
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import * as v from 'valibot';
|
||||
import { valibotResolver } from '..';
|
||||
|
||||
const USERNAME_REQUIRED_MESSAGE = 'username field is v.required';
|
||||
const PASSWORD_REQUIRED_MESSAGE = 'password field is v.required';
|
||||
|
||||
const schema = v.object({
|
||||
username: v.pipe(
|
||||
v.string(USERNAME_REQUIRED_MESSAGE),
|
||||
v.minLength(2, USERNAME_REQUIRED_MESSAGE),
|
||||
),
|
||||
password: v.pipe(
|
||||
v.string(PASSWORD_REQUIRED_MESSAGE),
|
||||
v.minLength(2, PASSWORD_REQUIRED_MESSAGE),
|
||||
),
|
||||
});
|
||||
|
||||
type FormData = { username: string; password: string };
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const { register, handleSubmit } = useForm<FormData>({
|
||||
resolver: valibotResolver(schema),
|
||||
shouldUseNativeValidation: true,
|
||||
});
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<input {...register('username')} placeholder="username" />
|
||||
|
||||
<input {...register('password')} placeholder="password" />
|
||||
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
test("form's native validation with Valibot", async () => {
|
||||
const handleSubmit = vi.fn();
|
||||
render(<TestComponent onSubmit={handleSubmit} />);
|
||||
|
||||
// username
|
||||
let usernameField = screen.getByPlaceholderText(
|
||||
/username/i,
|
||||
) as HTMLInputElement;
|
||||
expect(usernameField.validity.valid).toBe(true);
|
||||
expect(usernameField.validationMessage).toBe('');
|
||||
|
||||
// password
|
||||
let passwordField = screen.getByPlaceholderText(
|
||||
/password/i,
|
||||
) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(true);
|
||||
expect(passwordField.validationMessage).toBe('');
|
||||
|
||||
await user.click(screen.getByText(/submit/i));
|
||||
|
||||
// username
|
||||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
||||
expect(usernameField.validity.valid).toBe(false);
|
||||
expect(usernameField.validationMessage).toBe(USERNAME_REQUIRED_MESSAGE);
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(false);
|
||||
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE);
|
||||
|
||||
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(true);
|
||||
expect(passwordField.validationMessage).toBe('');
|
||||
});
|
||||
61
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/Form.tsx
generated
vendored
Normal file
61
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/Form.tsx
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import * as v from 'valibot';
|
||||
import { valibotResolver } from '..';
|
||||
|
||||
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
|
||||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
|
||||
|
||||
const schema = v.object({
|
||||
username: v.pipe(
|
||||
v.string(USERNAME_REQUIRED_MESSAGE),
|
||||
v.minLength(2, USERNAME_REQUIRED_MESSAGE),
|
||||
),
|
||||
password: v.pipe(
|
||||
v.string(PASSWORD_REQUIRED_MESSAGE),
|
||||
v.minLength(2, PASSWORD_REQUIRED_MESSAGE),
|
||||
),
|
||||
});
|
||||
|
||||
type FormData = { username: string; password: string };
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
} = useForm<FormData>({
|
||||
resolver: valibotResolver(schema), // Useful to check TypeScript regressions
|
||||
});
|
||||
|
||||
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>
|
||||
);
|
||||
}
|
||||
|
||||
test("form's validation with Valibot and TypeScript's integration", async () => {
|
||||
const handleSubmit = vi.fn();
|
||||
render(<TestComponent onSubmit={handleSubmit} />);
|
||||
|
||||
expect(screen.queryAllByRole('alert')).toHaveLength(0);
|
||||
|
||||
await user.click(screen.getByText(/submit/i));
|
||||
|
||||
expect(screen.getByText(/username field is required/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
|
||||
expect(handleSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
95
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
95
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
import * as v from 'valibot';
|
||||
|
||||
export const schema = v.object({
|
||||
username: v.pipe(
|
||||
v.string(),
|
||||
v.minLength(2),
|
||||
v.maxLength(30),
|
||||
v.regex(/^\w+$/),
|
||||
),
|
||||
password: v.pipe(
|
||||
v.string('New Password is required'),
|
||||
v.regex(new RegExp('.*[A-Z].*'), 'One uppercase character'),
|
||||
v.regex(new RegExp('.*[a-z].*'), 'One lowercase character'),
|
||||
v.regex(new RegExp('.*\\d.*'), 'One number'),
|
||||
v.regex(
|
||||
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
|
||||
'One special character',
|
||||
),
|
||||
v.minLength(8, 'Must be at least 8 characters in length'),
|
||||
),
|
||||
repeatPassword: v.string('Repeat Password is required'),
|
||||
accessToken: v.union(
|
||||
[
|
||||
v.string('Access token should be a string'),
|
||||
v.number('Access token should be a number'),
|
||||
],
|
||||
'access token is required',
|
||||
),
|
||||
birthYear: v.pipe(
|
||||
v.number('Please enter your birth year'),
|
||||
v.minValue(1900),
|
||||
v.maxValue(2013),
|
||||
),
|
||||
email: v.pipe(v.string(), v.email('Invalid email address')),
|
||||
tags: v.array(v.string('Tags should be strings')),
|
||||
enabled: v.boolean(),
|
||||
like: v.object({
|
||||
id: v.number('Like id is required'),
|
||||
name: v.pipe(
|
||||
v.string('Like name is required'),
|
||||
v.minLength(4, 'Too short'),
|
||||
),
|
||||
}),
|
||||
});
|
||||
|
||||
export const schemaError = v.variant('type', [
|
||||
v.object({ type: v.literal('a') }),
|
||||
v.object({ type: v.literal('b') }),
|
||||
]);
|
||||
|
||||
export const validSchemaErrorData = { type: 'a' };
|
||||
export const invalidSchemaErrorData = { type: 'c' };
|
||||
|
||||
export const validData = {
|
||||
username: 'Doe',
|
||||
password: 'Password123_',
|
||||
repeatPassword: 'Password123_',
|
||||
birthYear: 2000,
|
||||
email: 'john@doe.com',
|
||||
tags: ['tag1', 'tag2'],
|
||||
enabled: true,
|
||||
accessToken: 'accessToken',
|
||||
like: {
|
||||
id: 1,
|
||||
name: 'name',
|
||||
},
|
||||
};
|
||||
|
||||
export const invalidData = {
|
||||
password: '___',
|
||||
email: '',
|
||||
birthYear: 'birthYear',
|
||||
like: { id: 'z' },
|
||||
tags: [1, 2, 3],
|
||||
};
|
||||
|
||||
export const fields: Record<InternalFieldName, Field['_f']> = {
|
||||
username: {
|
||||
ref: { name: 'username' },
|
||||
name: 'username',
|
||||
},
|
||||
password: {
|
||||
ref: { name: 'password' },
|
||||
name: 'password',
|
||||
},
|
||||
email: {
|
||||
ref: { name: 'email' },
|
||||
name: 'email',
|
||||
},
|
||||
birthday: {
|
||||
ref: { name: 'birthday' },
|
||||
name: 'birthday',
|
||||
},
|
||||
};
|
||||
418
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/__snapshots__/valibot.ts.snap
generated
vendored
Normal file
418
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/__snapshots__/valibot.ts.snap
generated
vendored
Normal file
@ -0,0 +1,418 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`valibotResolver > should return a single error from valibotResolver when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"accessToken": {
|
||||
"message": "access token is required",
|
||||
"ref": undefined,
|
||||
"type": "union",
|
||||
},
|
||||
"birthYear": {
|
||||
"message": "Please enter your birth year",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
},
|
||||
"email": {
|
||||
"message": "Invalid email address",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "email",
|
||||
},
|
||||
"enabled": {
|
||||
"message": "Invalid type: Expected boolean but received undefined",
|
||||
"ref": undefined,
|
||||
"type": "boolean",
|
||||
},
|
||||
"like": {
|
||||
"id": {
|
||||
"message": "Like id is required",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
},
|
||||
"name": {
|
||||
"message": "Like name is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "regex",
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "Repeat Password is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
],
|
||||
"username": {
|
||||
"message": "Invalid type: Expected string but received undefined",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`valibotResolver > should return a single error from valibotResolver with \`mode: sync\` when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"accessToken": {
|
||||
"message": "access token is required",
|
||||
"ref": undefined,
|
||||
"type": "union",
|
||||
},
|
||||
"birthYear": {
|
||||
"message": "Please enter your birth year",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
},
|
||||
"email": {
|
||||
"message": "Invalid email address",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "email",
|
||||
},
|
||||
"enabled": {
|
||||
"message": "Invalid type: Expected boolean but received undefined",
|
||||
"ref": undefined,
|
||||
"type": "boolean",
|
||||
},
|
||||
"like": {
|
||||
"id": {
|
||||
"message": "Like id is required",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
},
|
||||
"name": {
|
||||
"message": "Like name is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "regex",
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "Repeat Password is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
},
|
||||
],
|
||||
"username": {
|
||||
"message": "Invalid type: Expected string but received undefined",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`valibotResolver > should return all the errors from valibotResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"accessToken": {
|
||||
"message": "access token is required",
|
||||
"ref": undefined,
|
||||
"type": "union",
|
||||
"types": {
|
||||
"union": "access token is required",
|
||||
},
|
||||
},
|
||||
"birthYear": {
|
||||
"message": "Please enter your birth year",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
"types": {
|
||||
"number": "Please enter your birth year",
|
||||
},
|
||||
},
|
||||
"email": {
|
||||
"message": "Invalid email address",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "email",
|
||||
"types": {
|
||||
"email": "Invalid email address",
|
||||
},
|
||||
},
|
||||
"enabled": {
|
||||
"message": "Invalid type: Expected boolean but received undefined",
|
||||
"ref": undefined,
|
||||
"type": "boolean",
|
||||
"types": {
|
||||
"boolean": "Invalid type: Expected boolean but received undefined",
|
||||
},
|
||||
},
|
||||
"like": {
|
||||
"id": {
|
||||
"message": "Like id is required",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
"types": {
|
||||
"number": "Like id is required",
|
||||
},
|
||||
},
|
||||
"name": {
|
||||
"message": "Like name is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Like name is required",
|
||||
},
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "regex",
|
||||
"types": {
|
||||
"min_length": "Must be at least 8 characters in length",
|
||||
"regex": [
|
||||
"One uppercase character",
|
||||
"One lowercase character",
|
||||
"One number",
|
||||
],
|
||||
},
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "Repeat Password is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Repeat Password is required",
|
||||
},
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Tags should be strings",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Tags should be strings",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Tags should be strings",
|
||||
},
|
||||
},
|
||||
],
|
||||
"username": {
|
||||
"message": "Invalid type: Expected string but received undefined",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Invalid type: Expected string but received undefined",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`valibotResolver > should return all the errors from valibotResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"accessToken": {
|
||||
"message": "access token is required",
|
||||
"ref": undefined,
|
||||
"type": "union",
|
||||
"types": {
|
||||
"union": "access token is required",
|
||||
},
|
||||
},
|
||||
"birthYear": {
|
||||
"message": "Please enter your birth year",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
"types": {
|
||||
"number": "Please enter your birth year",
|
||||
},
|
||||
},
|
||||
"email": {
|
||||
"message": "Invalid email address",
|
||||
"ref": {
|
||||
"name": "email",
|
||||
},
|
||||
"type": "email",
|
||||
"types": {
|
||||
"email": "Invalid email address",
|
||||
},
|
||||
},
|
||||
"enabled": {
|
||||
"message": "Invalid type: Expected boolean but received undefined",
|
||||
"ref": undefined,
|
||||
"type": "boolean",
|
||||
"types": {
|
||||
"boolean": "Invalid type: Expected boolean but received undefined",
|
||||
},
|
||||
},
|
||||
"like": {
|
||||
"id": {
|
||||
"message": "Like id is required",
|
||||
"ref": undefined,
|
||||
"type": "number",
|
||||
"types": {
|
||||
"number": "Like id is required",
|
||||
},
|
||||
},
|
||||
"name": {
|
||||
"message": "Like name is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Like name is required",
|
||||
},
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "regex",
|
||||
"types": {
|
||||
"min_length": "Must be at least 8 characters in length",
|
||||
"regex": [
|
||||
"One uppercase character",
|
||||
"One lowercase character",
|
||||
"One number",
|
||||
],
|
||||
},
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "Repeat Password is required",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Repeat Password is required",
|
||||
},
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Tags should be strings",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Tags should be strings",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message": "Tags should be strings",
|
||||
"ref": undefined,
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Tags should be strings",
|
||||
},
|
||||
},
|
||||
],
|
||||
"username": {
|
||||
"message": "Invalid type: Expected string but received undefined",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "string",
|
||||
"types": {
|
||||
"string": "Invalid type: Expected string but received undefined",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`valibotResolver > should return parsed values from valibotResolver with \`mode: sync\` when validation pass 1`] = `
|
||||
{
|
||||
"errors": {},
|
||||
"values": {
|
||||
"accessToken": "accessToken",
|
||||
"birthYear": 2000,
|
||||
"email": "john@doe.com",
|
||||
"enabled": true,
|
||||
"like": {
|
||||
"id": 1,
|
||||
"name": "name",
|
||||
},
|
||||
"password": "Password123_",
|
||||
"repeatPassword": "Password123_",
|
||||
"tags": [
|
||||
"tag1",
|
||||
"tag2",
|
||||
],
|
||||
"username": "Doe",
|
||||
},
|
||||
}
|
||||
`;
|
||||
141
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/valibot.ts
generated
vendored
Normal file
141
frontend/node_modules/@hookform/resolvers/valibot/src/__tests__/valibot.ts
generated
vendored
Normal file
@ -0,0 +1,141 @@
|
||||
import * as valibot from 'valibot';
|
||||
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
||||
import { valibotResolver } from '..';
|
||||
import {
|
||||
fields,
|
||||
invalidData,
|
||||
invalidSchemaErrorData,
|
||||
schema,
|
||||
schemaError,
|
||||
validData,
|
||||
validSchemaErrorData,
|
||||
} from './__fixtures__/data';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
describe('valibotResolver', () => {
|
||||
it('should return parsed values from valibotResolver with `mode: sync` when validation pass', async () => {
|
||||
vi.mock('valibot', async () => {
|
||||
const a = await vi.importActual<any>('valibot');
|
||||
return {
|
||||
__esModule: true,
|
||||
...a,
|
||||
};
|
||||
});
|
||||
const funcSpy = vi.spyOn(valibot, 'safeParseAsync');
|
||||
|
||||
const result = await valibotResolver(schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(validData, undefined, { fields, shouldUseNativeValidation });
|
||||
|
||||
expect(funcSpy).toHaveBeenCalledTimes(1);
|
||||
expect(result.errors).toEqual({});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return a single error from valibotResolver with `mode: sync` when validation fails', async () => {
|
||||
vi.mock('valibot', async () => {
|
||||
const a = await vi.importActual<any>('valibot');
|
||||
return {
|
||||
__esModule: true,
|
||||
...a,
|
||||
};
|
||||
});
|
||||
const funcSpy = vi.spyOn(valibot, 'safeParseAsync');
|
||||
|
||||
const result = await valibotResolver(schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(invalidData, undefined, { fields, shouldUseNativeValidation });
|
||||
|
||||
expect(funcSpy).toHaveBeenCalledTimes(1);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return values from valibotResolver when validation pass', async () => {
|
||||
const result = await valibotResolver(schema)(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
});
|
||||
|
||||
it('should return a single error from valibotResolver when validation fails', async () => {
|
||||
const result = await valibotResolver(schema)(invalidData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return values from valibotResolver when validation pass & raw=true', async () => {
|
||||
const result = await valibotResolver(schema, undefined, {
|
||||
raw: true,
|
||||
})(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
});
|
||||
|
||||
it('should return all the errors from valibotResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
||||
const result = await valibotResolver(schema)(invalidData, undefined, {
|
||||
fields,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the errors from valibotResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
||||
const result = await valibotResolver(schema, undefined, { mode: 'sync' })(
|
||||
invalidData,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should be able to validate variants without errors', async () => {
|
||||
const result = await valibotResolver(schemaError, undefined, {
|
||||
mode: 'sync',
|
||||
})(validSchemaErrorData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
errors: {},
|
||||
values: {
|
||||
type: 'a',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to validate variants with errors', async () => {
|
||||
const result = await valibotResolver(schemaError, undefined, {
|
||||
mode: 'sync',
|
||||
})(invalidSchemaErrorData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
errors: {
|
||||
type: {
|
||||
message: 'Invalid type: Expected "a" | "b" but received "c"',
|
||||
ref: undefined,
|
||||
type: 'variant',
|
||||
},
|
||||
},
|
||||
values: {},
|
||||
});
|
||||
});
|
||||
});
|
||||
2
frontend/node_modules/@hookform/resolvers/valibot/src/index.ts
generated
vendored
Normal file
2
frontend/node_modules/@hookform/resolvers/valibot/src/index.ts
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './valibot';
|
||||
export * from './types';
|
||||
34
frontend/node_modules/@hookform/resolvers/valibot/src/types.ts
generated
vendored
Normal file
34
frontend/node_modules/@hookform/resolvers/valibot/src/types.ts
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
||||
import {
|
||||
BaseIssue,
|
||||
BaseSchema,
|
||||
BaseSchemaAsync,
|
||||
Config,
|
||||
InferIssue,
|
||||
} from 'valibot';
|
||||
|
||||
export type Resolver = <
|
||||
T extends
|
||||
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
|
||||
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
|
||||
>(
|
||||
schema: T,
|
||||
schemaOptions?: Partial<
|
||||
Omit<Config<InferIssue<T>>, 'abortPipeEarly' | 'skipPipe'>
|
||||
>,
|
||||
resolverOptions?: {
|
||||
/**
|
||||
* @default async
|
||||
*/
|
||||
mode?: 'sync' | 'async';
|
||||
/**
|
||||
* Return the raw input values rather than the parsed values.
|
||||
* @default false
|
||||
*/
|
||||
raw?: boolean;
|
||||
},
|
||||
) => <TFieldValues extends FieldValues, TContext>(
|
||||
values: TFieldValues,
|
||||
context: TContext | undefined,
|
||||
options: ResolverOptions<TFieldValues>,
|
||||
) => Promise<ResolverResult<TFieldValues>>;
|
||||
73
frontend/node_modules/@hookform/resolvers/valibot/src/valibot.ts
generated
vendored
Normal file
73
frontend/node_modules/@hookform/resolvers/valibot/src/valibot.ts
generated
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
import { toNestErrors } from '@hookform/resolvers';
|
||||
import { FieldError, FieldValues, appendErrors } from 'react-hook-form';
|
||||
import { getDotPath, safeParseAsync } from 'valibot';
|
||||
import type { Resolver } from './types';
|
||||
|
||||
export const valibotResolver: Resolver =
|
||||
(schema, schemaOptions, resolverOptions = {}) =>
|
||||
async (values, _, options) => {
|
||||
// Check if we should validate all field criteria
|
||||
const validateAllFieldCriteria =
|
||||
!options.shouldUseNativeValidation && options.criteriaMode === 'all';
|
||||
|
||||
// Parse values with Valibot schema
|
||||
const result = await safeParseAsync(
|
||||
schema,
|
||||
values,
|
||||
Object.assign({}, schemaOptions, {
|
||||
abortPipeEarly: !validateAllFieldCriteria,
|
||||
}),
|
||||
);
|
||||
|
||||
// If there are issues, return them as errors
|
||||
if (result.issues) {
|
||||
// Create errors object
|
||||
const errors: Record<string, FieldError> = {};
|
||||
|
||||
// Iterate over issues to add them to errors object
|
||||
for (; result.issues.length; ) {
|
||||
const issue = result.issues[0];
|
||||
// Create dot path from issue
|
||||
const path = getDotPath(issue);
|
||||
|
||||
if (path) {
|
||||
// Add first error of path to errors object
|
||||
if (!errors[path]) {
|
||||
errors[path] = { message: issue.message, type: issue.type };
|
||||
}
|
||||
|
||||
// If configured, add all errors of path to errors object
|
||||
if (validateAllFieldCriteria) {
|
||||
const types = errors[path].types;
|
||||
const messages = types && types[issue.type];
|
||||
errors[path] = appendErrors(
|
||||
path,
|
||||
validateAllFieldCriteria,
|
||||
errors,
|
||||
issue.type,
|
||||
messages
|
||||
? ([] as string[]).concat(
|
||||
messages as string | string[],
|
||||
issue.message,
|
||||
)
|
||||
: issue.message,
|
||||
) as FieldError;
|
||||
}
|
||||
}
|
||||
|
||||
result.issues.shift();
|
||||
}
|
||||
|
||||
// Return resolver result with errors
|
||||
return {
|
||||
values: {},
|
||||
errors: toNestErrors(errors, options),
|
||||
} as const;
|
||||
}
|
||||
|
||||
// Otherwise, return resolver result with values
|
||||
return {
|
||||
values: resolverOptions.raw ? values : (result.output as FieldValues),
|
||||
errors: {},
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user