added apps
This commit is contained in:
19
node_modules/@hookform/resolvers/ajv/package.json
generated
vendored
Normal file
19
node_modules/@hookform/resolvers/ajv/package.json
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@hookform/resolvers/ajv",
|
||||
"amdName": "hookformResolversAjv",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "React Hook Form validation resolver: ajv",
|
||||
"main": "dist/ajv.js",
|
||||
"module": "dist/ajv.module.js",
|
||||
"umd:main": "dist/ajv.umd.js",
|
||||
"source": "src/index.ts",
|
||||
"types": "dist/index.d.ts",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"react-hook-form": "^7.55.0",
|
||||
"@hookform/resolvers": "^2.0.0",
|
||||
"ajv": "^8.12.0",
|
||||
"ajv-errors": "^3.0.0"
|
||||
}
|
||||
}
|
||||
94
node_modules/@hookform/resolvers/ajv/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
94
node_modules/@hookform/resolvers/ajv/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import { JSONSchemaType } from 'ajv';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { ajvResolver } from '..';
|
||||
|
||||
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
|
||||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
|
||||
|
||||
type FormData = { username: string; password: string };
|
||||
|
||||
const schema: JSONSchemaType<FormData> = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
username: {
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
errorMessage: { minLength: USERNAME_REQUIRED_MESSAGE },
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
errorMessage: { minLength: PASSWORD_REQUIRED_MESSAGE },
|
||||
},
|
||||
},
|
||||
required: ['username', 'password'],
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const { register, handleSubmit } = useForm<FormData>({
|
||||
resolver: ajvResolver(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 Ajv", 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(/username/i), 'joe');
|
||||
await user.type(screen.getByPlaceholderText(/password/i), 'password');
|
||||
|
||||
// username
|
||||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement;
|
||||
expect(usernameField.validity.valid).toBe(true);
|
||||
expect(usernameField.validationMessage).toBe('');
|
||||
|
||||
// password
|
||||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
|
||||
expect(passwordField.validity.valid).toBe(true);
|
||||
expect(passwordField.validationMessage).toBe('');
|
||||
});
|
||||
65
node_modules/@hookform/resolvers/ajv/src/__tests__/Form.tsx
generated
vendored
Normal file
65
node_modules/@hookform/resolvers/ajv/src/__tests__/Form.tsx
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import { JSONSchemaType } from 'ajv';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { ajvResolver } from '..';
|
||||
|
||||
type FormData = { username: string; password: string };
|
||||
|
||||
const schema: JSONSchemaType<FormData> = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
username: {
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
errorMessage: { minLength: 'username field is required' },
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
errorMessage: { minLength: 'password field is required' },
|
||||
},
|
||||
},
|
||||
required: ['username', 'password'],
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const {
|
||||
register,
|
||||
formState: { errors },
|
||||
handleSubmit,
|
||||
} = useForm<FormData>({
|
||||
resolver: ajvResolver(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 Ajv 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();
|
||||
});
|
||||
216
node_modules/@hookform/resolvers/ajv/src/__tests__/__fixtures__/data-errors.ts
generated
vendored
Normal file
216
node_modules/@hookform/resolvers/ajv/src/__tests__/__fixtures__/data-errors.ts
generated
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
import { JSONSchemaType } from 'ajv';
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
|
||||
interface DataA {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export const schemaA: JSONSchemaType<DataA> = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
username: {
|
||||
type: 'string',
|
||||
minLength: 3,
|
||||
errorMessage: {
|
||||
minLength: 'username should be at least three characters long',
|
||||
},
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
pattern: '.*[A-Z].*',
|
||||
minLength: 8,
|
||||
errorMessage: {
|
||||
pattern: 'One uppercase character',
|
||||
minLength: 'passwords should be at least eight characters long',
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['username', 'password'],
|
||||
additionalProperties: false,
|
||||
errorMessage: {
|
||||
required: {
|
||||
username: 'username field is required',
|
||||
password: 'password field is required',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const validDataA: DataA = {
|
||||
username: 'kt666',
|
||||
password: 'validPassword',
|
||||
};
|
||||
|
||||
export const invalidDataA = {
|
||||
username: 'kt',
|
||||
password: 'invalid',
|
||||
};
|
||||
|
||||
export const undefinedDataA = {
|
||||
username: undefined,
|
||||
password: undefined,
|
||||
};
|
||||
|
||||
export const fieldsA: 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',
|
||||
},
|
||||
};
|
||||
|
||||
// examples from [ajv-errors](https://github.com/ajv-validator/ajv-errors)
|
||||
|
||||
interface DataB {
|
||||
foo: number;
|
||||
}
|
||||
|
||||
export const schemaB: JSONSchemaType<DataB> = {
|
||||
type: 'object',
|
||||
required: ['foo'],
|
||||
properties: {
|
||||
foo: { type: 'integer' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
errorMessage: 'should be an object with an integer property foo only',
|
||||
};
|
||||
|
||||
export const validDataB: DataB = { foo: 666 };
|
||||
export const invalidDataB = { foo: 'kt', bar: 6 };
|
||||
export const undefinedDataB = { foo: undefined };
|
||||
|
||||
interface DataC {
|
||||
foo: number;
|
||||
}
|
||||
|
||||
export const schemaC: JSONSchemaType<DataC> = {
|
||||
type: 'object',
|
||||
required: ['foo'],
|
||||
properties: {
|
||||
foo: { type: 'integer' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
errorMessage: {
|
||||
type: 'should be an object',
|
||||
required: 'should have property foo',
|
||||
additionalProperties: 'should not have properties other than foo',
|
||||
},
|
||||
};
|
||||
|
||||
export const validDataC: DataC = { foo: 666 };
|
||||
export const invalidDataC = { foo: 'kt', bar: 6 };
|
||||
export const undefinedDataC = { foo: undefined };
|
||||
export const invalidTypeDataC = 'something';
|
||||
|
||||
interface DataD {
|
||||
foo: number;
|
||||
bar: string;
|
||||
}
|
||||
|
||||
export const schemaD: JSONSchemaType<DataD> = {
|
||||
type: 'object',
|
||||
required: ['foo', 'bar'],
|
||||
properties: {
|
||||
foo: { type: 'integer' },
|
||||
bar: { type: 'string' },
|
||||
},
|
||||
errorMessage: {
|
||||
type: 'should be an object', // will not replace internal "type" error for the property "foo"
|
||||
required: {
|
||||
foo: 'should have an integer property "foo"',
|
||||
bar: 'should have a string property "bar"',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const validDataD: DataD = { foo: 666, bar: 'kt' };
|
||||
export const invalidDataD = { foo: 'kt', bar: 6 };
|
||||
export const undefinedDataD = { foo: undefined, bar: undefined };
|
||||
export const invalidTypeDataD = 'something';
|
||||
|
||||
interface DataE {
|
||||
foo: number;
|
||||
bar: string;
|
||||
}
|
||||
|
||||
export const schemaE: JSONSchemaType<DataE> = {
|
||||
type: 'object',
|
||||
required: ['foo', 'bar'],
|
||||
allOf: [
|
||||
{
|
||||
properties: {
|
||||
foo: { type: 'integer', minimum: 2 },
|
||||
bar: { type: 'string', minLength: 2 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
],
|
||||
errorMessage: {
|
||||
properties: {
|
||||
foo: 'data.foo should be integer >= 2',
|
||||
bar: 'data.bar should be string with length >= 2',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const validDataE: DataE = { foo: 666, bar: 'kt' };
|
||||
export const invalidDataE = { foo: 1, bar: 'k' };
|
||||
export const undefinedDataE = { foo: undefined, bar: undefined };
|
||||
|
||||
interface DataF {
|
||||
foo: number;
|
||||
bar: string;
|
||||
}
|
||||
|
||||
export const schemaF: JSONSchemaType<DataF> = {
|
||||
type: 'object',
|
||||
required: ['foo', 'bar'],
|
||||
allOf: [
|
||||
{
|
||||
properties: {
|
||||
foo: { type: 'integer', minimum: 2 },
|
||||
bar: { type: 'string', minLength: 2 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
],
|
||||
errorMessage: {
|
||||
type: 'data should be an object',
|
||||
properties: {
|
||||
foo: 'data.foo should be integer >= 2',
|
||||
bar: 'data.bar should be string with length >= 2',
|
||||
},
|
||||
_: 'data should have properties "foo" and "bar" only',
|
||||
},
|
||||
};
|
||||
|
||||
export const validDataF: DataF = { foo: 666, bar: 'kt' };
|
||||
export const invalidDataF = {};
|
||||
export const undefinedDataF = { foo: 1, bar: undefined };
|
||||
export const invalidTypeDataF = 'something';
|
||||
|
||||
export const fieldsRest: Record<InternalFieldName, Field['_f']> = {
|
||||
foo: {
|
||||
ref: { name: 'foo' },
|
||||
name: 'foo',
|
||||
},
|
||||
bar: {
|
||||
ref: { name: 'bar' },
|
||||
name: 'bar',
|
||||
},
|
||||
lorem: {
|
||||
ref: { name: 'lorem' },
|
||||
name: 'lorem',
|
||||
},
|
||||
};
|
||||
90
node_modules/@hookform/resolvers/ajv/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
90
node_modules/@hookform/resolvers/ajv/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
import { JSONSchemaType } from 'ajv';
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
|
||||
interface Data {
|
||||
username: string;
|
||||
password: string;
|
||||
deepObject: { data: string; twoLayersDeep: { name: string } };
|
||||
}
|
||||
|
||||
export const schema: JSONSchemaType<Data> = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
username: {
|
||||
type: 'string',
|
||||
minLength: 3,
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
pattern: '.*[A-Z].*',
|
||||
errorMessage: {
|
||||
pattern: 'One uppercase character',
|
||||
},
|
||||
},
|
||||
deepObject: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
data: { type: 'string' },
|
||||
twoLayersDeep: {
|
||||
type: 'object',
|
||||
properties: { name: { type: 'string' } },
|
||||
additionalProperties: false,
|
||||
required: ['name'],
|
||||
},
|
||||
},
|
||||
required: ['data', 'twoLayersDeep'],
|
||||
},
|
||||
},
|
||||
required: ['username', 'password', 'deepObject'],
|
||||
additionalProperties: false,
|
||||
};
|
||||
|
||||
export const validData: Data = {
|
||||
username: 'jsun969',
|
||||
password: 'validPassword',
|
||||
deepObject: {
|
||||
twoLayersDeep: {
|
||||
name: 'deeper',
|
||||
},
|
||||
data: 'data',
|
||||
},
|
||||
};
|
||||
|
||||
export const invalidData = {
|
||||
username: '__',
|
||||
password: 'invalid-password',
|
||||
deepObject: {
|
||||
data: 233,
|
||||
twoLayersDeep: { name: 123 },
|
||||
},
|
||||
};
|
||||
|
||||
export const invalidDataWithUndefined = {
|
||||
username: 'jsun969',
|
||||
password: undefined,
|
||||
deepObject: {
|
||||
twoLayersDeep: {
|
||||
name: 'deeper',
|
||||
},
|
||||
data: undefined,
|
||||
},
|
||||
};
|
||||
|
||||
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',
|
||||
},
|
||||
};
|
||||
462
node_modules/@hookform/resolvers/ajv/src/__tests__/__snapshots__/ajv-errors.ts.snap
generated
vendored
Normal file
462
node_modules/@hookform/resolvers/ajv/src/__tests__/__snapshots__/ajv-errors.ts.snap
generated
vendored
Normal file
@@ -0,0 +1,462 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return a default message if there is no specific message for the error when requirement fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "data should have properties "foo" and "bar" only",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "data should have properties "foo" and "bar" only",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "data should have properties "foo" and "bar" only",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "data should have properties "foo" and "bar" only",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return a default message if there is no specific message for the error when some properties are undefined 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "data should have properties "foo" and "bar" only",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "data should have properties "foo" and "bar" only",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "data.foo should be integer >= 2",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "minimum",
|
||||
"types": {
|
||||
"minimum": "data.foo should be integer >= 2",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return a default message if there is no specific message for the error when walidation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "data should have properties "foo" and "bar" only",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "data should have properties "foo" and "bar" only",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "data should have properties "foo" and "bar" only",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "data should have properties "foo" and "bar" only",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized error messages for certain keywords when requirement fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"foo": {
|
||||
"message": "should have property foo",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should have property foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized error messages for certain keywords when some properties are undefined 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"foo": {
|
||||
"message": "should have property foo",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should have property foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized error messages for certain keywords when walidation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"": {
|
||||
"message": "should not have properties other than foo",
|
||||
"ref": undefined,
|
||||
"type": "additionalProperties",
|
||||
"types": {
|
||||
"additionalProperties": "should not have properties other than foo",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "must be integer",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be integer",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized error messages when requirement fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"password": {
|
||||
"message": "password field is required",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "password field is required",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "username field is required",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "username field is required",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized error messages when some properties are undefined 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"password": {
|
||||
"message": "password field is required",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "password field is required",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "username field is required",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "username field is required",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized error messages when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "pattern",
|
||||
"types": {
|
||||
"minLength": "passwords should be at least eight characters long",
|
||||
"pattern": "One uppercase character",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "username should be at least three characters long",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "minLength",
|
||||
"types": {
|
||||
"minLength": "username should be at least three characters long",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized errors for properties/items when requirement fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "must have required property 'bar'",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "must have required property 'bar'",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "must have required property 'foo'",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "must have required property 'foo'",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized errors for properties/items when some properties are undefined 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "must have required property 'bar'",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "must have required property 'bar'",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "must have required property 'foo'",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "must have required property 'foo'",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return customized errors for properties/items when walidation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "data.bar should be string with length >= 2",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "minLength",
|
||||
"types": {
|
||||
"minLength": "data.bar should be string with length >= 2",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "data.foo should be integer >= 2",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "minimum",
|
||||
"types": {
|
||||
"minimum": "data.foo should be integer >= 2",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return different messages for different properties when requirement fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "should have a string property "bar"",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should have a string property "bar"",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "should have an integer property "foo"",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should have an integer property "foo"",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return different messages for different properties when some properties are undefined 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "should have a string property "bar"",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should have a string property "bar"",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "should have an integer property "foo"",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should have an integer property "foo"",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return different messages for different properties when walidation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"bar": {
|
||||
"message": "must be string",
|
||||
"ref": {
|
||||
"name": "bar",
|
||||
},
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be string",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "must be integer",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be integer",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return the same customized error message when requirement fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"foo": {
|
||||
"message": "should be an object with an integer property foo only",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should be an object with an integer property foo only",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return the same customized message for all validation failures 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"": {
|
||||
"message": "should be an object with an integer property foo only",
|
||||
"ref": undefined,
|
||||
"type": "additionalProperties",
|
||||
"types": {
|
||||
"additionalProperties": "should be an object with an integer property foo only",
|
||||
},
|
||||
},
|
||||
"foo": {
|
||||
"message": "should be an object with an integer property foo only",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "should be an object with an integer property foo only",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver with errorMessage > should return the same customized message when some properties are undefined 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"foo": {
|
||||
"message": "should be an object with an integer property foo only",
|
||||
"ref": {
|
||||
"name": "foo",
|
||||
},
|
||||
"type": "required",
|
||||
"types": {
|
||||
"required": "should be an object with an integer property foo only",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
245
node_modules/@hookform/resolvers/ajv/src/__tests__/__snapshots__/ajv.ts.snap
generated
vendored
Normal file
245
node_modules/@hookform/resolvers/ajv/src/__tests__/__snapshots__/ajv.ts.snap
generated
vendored
Normal file
@@ -0,0 +1,245 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`ajvResolver > should return all the error messages from ajvResolver when requirement fails and validateAllFieldCriteria set to true 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"message": "must have required property 'deepObject'",
|
||||
"ref": undefined,
|
||||
"type": "required",
|
||||
},
|
||||
"password": {
|
||||
"message": "must have required property 'password'",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "required",
|
||||
},
|
||||
"username": {
|
||||
"message": "must have required property 'username'",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "required",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver > should return all the error messages from ajvResolver when requirement fails and validateAllFieldCriteria set to true and \`mode: sync\` 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"message": "must have required property 'deepObject'",
|
||||
"ref": undefined,
|
||||
"type": "required",
|
||||
},
|
||||
"password": {
|
||||
"message": "must have required property 'password'",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "required",
|
||||
},
|
||||
"username": {
|
||||
"message": "must have required property 'username'",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "required",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver > should return all the error messages from ajvResolver when some property is undefined and result will keep the input data structure 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"data": {
|
||||
"message": "must have required property 'data'",
|
||||
"ref": undefined,
|
||||
"type": "required",
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "must have required property 'password'",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "required",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver > should return all the error messages from ajvResolver when validation fails and validateAllFieldCriteria set to true 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"data": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be string",
|
||||
},
|
||||
},
|
||||
"twoLayersDeep": {
|
||||
"name": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be string",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "pattern",
|
||||
"types": {
|
||||
"pattern": "One uppercase character",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "must NOT have fewer than 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "minLength",
|
||||
"types": {
|
||||
"minLength": "must NOT have fewer than 3 characters",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver > should return all the error messages from ajvResolver when validation fails and validateAllFieldCriteria set to true and \`mode: sync\` 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"data": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be string",
|
||||
},
|
||||
},
|
||||
"twoLayersDeep": {
|
||||
"name": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
"types": {
|
||||
"type": "must be string",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "pattern",
|
||||
"types": {
|
||||
"pattern": "One uppercase character",
|
||||
},
|
||||
},
|
||||
"username": {
|
||||
"message": "must NOT have fewer than 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "minLength",
|
||||
"types": {
|
||||
"minLength": "must NOT have fewer than 3 characters",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver > should return single error message from ajvResolver when validation fails and validateAllFieldCriteria set to false 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"data": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
},
|
||||
"twoLayersDeep": {
|
||||
"name": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
},
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "pattern",
|
||||
},
|
||||
"username": {
|
||||
"message": "must NOT have fewer than 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "minLength",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`ajvResolver > should return single error message from ajvResolver when validation fails and validateAllFieldCriteria set to false and \`mode: sync\` 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"deepObject": {
|
||||
"data": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
},
|
||||
"twoLayersDeep": {
|
||||
"name": {
|
||||
"message": "must be string",
|
||||
"ref": undefined,
|
||||
"type": "type",
|
||||
},
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
"type": "pattern",
|
||||
},
|
||||
"username": {
|
||||
"message": "must NOT have fewer than 3 characters",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
"type": "minLength",
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
227
node_modules/@hookform/resolvers/ajv/src/__tests__/ajv-errors.ts
generated
vendored
Normal file
227
node_modules/@hookform/resolvers/ajv/src/__tests__/ajv-errors.ts
generated
vendored
Normal file
@@ -0,0 +1,227 @@
|
||||
import { ajvResolver } from '..';
|
||||
import * as fixture from './__fixtures__/data-errors';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
|
||||
describe('ajvResolver with errorMessage', () => {
|
||||
it('should return values when validation pass', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaA)(fixture.validDataA, undefined, {
|
||||
fields: fixture.fieldsA,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toEqual({
|
||||
values: fixture.validDataA,
|
||||
errors: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('should return customized error messages when validation fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaA)(
|
||||
fixture.invalidDataA,
|
||||
{},
|
||||
{
|
||||
fields: fixture.fieldsA,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized error messages when requirement fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaA)({}, undefined, {
|
||||
fields: fixture.fieldsA,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized error messages when some properties are undefined', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaA, undefined, { mode: 'sync' })(
|
||||
fixture.undefinedDataA,
|
||||
undefined,
|
||||
{
|
||||
fields: fixture.fieldsA,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return the same customized message for all validation failures', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaB)(
|
||||
fixture.invalidDataB,
|
||||
{},
|
||||
{
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return the same customized error message when requirement fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaB)({}, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return the same customized message when some properties are undefined', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaB)(fixture.undefinedDataB, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized error messages for certain keywords when walidation fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaC)(
|
||||
fixture.invalidDataC,
|
||||
{},
|
||||
{
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized error messages for certain keywords when requirement fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaC)({}, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized error messages for certain keywords when some properties are undefined', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaC)(fixture.undefinedDataC, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return different messages for different properties when walidation fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaD)(
|
||||
fixture.invalidDataD,
|
||||
{},
|
||||
{
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return different messages for different properties when requirement fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaD)({}, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return different messages for different properties when some properties are undefined', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaD)(fixture.undefinedDataD, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized errors for properties/items when walidation fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaE)(
|
||||
fixture.invalidDataE,
|
||||
{},
|
||||
{
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized errors for properties/items when requirement fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaE)({}, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return customized errors for properties/items when some properties are undefined', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaE)(fixture.undefinedDataE, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return a default message if there is no specific message for the error when walidation fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaF)(
|
||||
fixture.invalidDataF,
|
||||
{},
|
||||
{
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return a default message if there is no specific message for the error when requirement fails', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaF)({}, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return a default message if there is no specific message for the error when some properties are undefined', async () => {
|
||||
expect(
|
||||
await ajvResolver(fixture.schemaF)(fixture.undefinedDataF, undefined, {
|
||||
fields: fixture.fieldsRest,
|
||||
criteriaMode: 'all',
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
103
node_modules/@hookform/resolvers/ajv/src/__tests__/ajv.ts
generated
vendored
Normal file
103
node_modules/@hookform/resolvers/ajv/src/__tests__/ajv.ts
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
import { ajvResolver } from '..';
|
||||
import {
|
||||
fields,
|
||||
invalidData,
|
||||
invalidDataWithUndefined,
|
||||
schema,
|
||||
validData,
|
||||
} from './__fixtures__/data';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
|
||||
describe('ajvResolver', () => {
|
||||
it('should return values from ajvResolver when validation pass', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema)(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toEqual({
|
||||
values: validData,
|
||||
errors: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('should return values from ajvResolver with `mode: sync` when validation pass', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(validData, undefined, { fields, shouldUseNativeValidation }),
|
||||
).toEqual({
|
||||
values: validData,
|
||||
errors: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('should return single error message from ajvResolver when validation fails and validateAllFieldCriteria set to false', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema)(invalidData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return single error message from ajvResolver when validation fails and validateAllFieldCriteria set to false and `mode: sync`', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema, undefined, {
|
||||
mode: 'sync',
|
||||
})(invalidData, undefined, { fields, shouldUseNativeValidation }),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the error messages from ajvResolver when validation fails and validateAllFieldCriteria set to true', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema)(
|
||||
invalidData,
|
||||
{},
|
||||
{ fields, criteriaMode: 'all', shouldUseNativeValidation },
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the error messages from ajvResolver when validation fails and validateAllFieldCriteria set to true and `mode: sync`', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema, undefined, { mode: 'sync' })(
|
||||
invalidData,
|
||||
{},
|
||||
{ fields, criteriaMode: 'all', shouldUseNativeValidation },
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the error messages from ajvResolver when requirement fails and validateAllFieldCriteria set to true', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema)({}, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the error messages from ajvResolver when requirement fails and validateAllFieldCriteria set to true and `mode: sync`', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema, undefined, { mode: 'sync' })({}, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should return all the error messages from ajvResolver when some property is undefined and result will keep the input data structure', async () => {
|
||||
expect(
|
||||
await ajvResolver(schema, undefined, { mode: 'sync' })(
|
||||
invalidDataWithUndefined,
|
||||
undefined,
|
||||
{
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
},
|
||||
),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
121
node_modules/@hookform/resolvers/ajv/src/ajv.ts
generated
vendored
Normal file
121
node_modules/@hookform/resolvers/ajv/src/ajv.ts
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
||||
import Ajv, { DefinedError } from 'ajv';
|
||||
import ajvErrors from 'ajv-errors';
|
||||
import { FieldError, appendErrors } from 'react-hook-form';
|
||||
import { AjvError, Resolver } from './types';
|
||||
|
||||
const parseErrorSchema = (
|
||||
ajvErrors: AjvError[],
|
||||
validateAllFieldCriteria: boolean,
|
||||
) => {
|
||||
const parsedErrors: Record<string, FieldError> = {};
|
||||
|
||||
const reduceError = (error: AjvError) => {
|
||||
// Ajv will return empty instancePath when require error
|
||||
if (error.keyword === 'required') {
|
||||
error.instancePath += `/${error.params.missingProperty}`;
|
||||
}
|
||||
|
||||
// `/deepObject/data` -> `deepObject.data`
|
||||
const path = error.instancePath.substring(1).replace(/\//g, '.');
|
||||
|
||||
if (!parsedErrors[path]) {
|
||||
parsedErrors[path] = {
|
||||
message: error.message,
|
||||
type: error.keyword,
|
||||
};
|
||||
}
|
||||
|
||||
if (validateAllFieldCriteria) {
|
||||
const types = parsedErrors[path].types;
|
||||
const messages = types && types[error.keyword];
|
||||
|
||||
parsedErrors[path] = appendErrors(
|
||||
path,
|
||||
validateAllFieldCriteria,
|
||||
parsedErrors,
|
||||
error.keyword,
|
||||
messages
|
||||
? ([] as string[]).concat(messages as string[], error.message || '')
|
||||
: error.message,
|
||||
) as FieldError;
|
||||
}
|
||||
};
|
||||
|
||||
for (let index = 0; index < ajvErrors.length; index += 1) {
|
||||
const error = ajvErrors[index];
|
||||
|
||||
if (error.keyword === 'errorMessage') {
|
||||
error.params.errors.forEach((originalError) => {
|
||||
originalError.message = error.message;
|
||||
reduceError(originalError);
|
||||
});
|
||||
} else {
|
||||
reduceError(error);
|
||||
}
|
||||
}
|
||||
|
||||
return parsedErrors;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a resolver for react-hook-form using Ajv schema validation
|
||||
* @param {Schema} schema - The Ajv schema to validate against
|
||||
* @param {Object} schemaOptions - Additional schema validation options
|
||||
* @param {Object} resolverOptions - Additional resolver configuration
|
||||
* @param {string} [resolverOptions.mode='async'] - Validation mode
|
||||
* @returns {Resolver<Schema>} A resolver function compatible with react-hook-form
|
||||
* @example
|
||||
* const schema = ajv.compile({
|
||||
* type: 'object',
|
||||
* properties: {
|
||||
* name: { type: 'string' },
|
||||
* age: { type: 'number' }
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* useForm({
|
||||
* resolver: ajvResolver(schema)
|
||||
* });
|
||||
*/
|
||||
export const ajvResolver: Resolver =
|
||||
(schema, schemaOptions, resolverOptions = {}) =>
|
||||
async (values, _, options) => {
|
||||
const ajv = new Ajv(
|
||||
Object.assign(
|
||||
{},
|
||||
{
|
||||
allErrors: true,
|
||||
validateSchema: true,
|
||||
},
|
||||
schemaOptions,
|
||||
),
|
||||
);
|
||||
|
||||
ajvErrors(ajv);
|
||||
|
||||
const validate = ajv.compile(
|
||||
Object.assign(
|
||||
{ $async: resolverOptions && resolverOptions.mode === 'async' },
|
||||
schema,
|
||||
),
|
||||
);
|
||||
|
||||
const valid = validate(values);
|
||||
|
||||
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
||||
|
||||
return valid
|
||||
? { values, errors: {} }
|
||||
: {
|
||||
values: {},
|
||||
errors: toNestErrors(
|
||||
parseErrorSchema(
|
||||
validate.errors as DefinedError[],
|
||||
!options.shouldUseNativeValidation &&
|
||||
options.criteriaMode === 'all',
|
||||
),
|
||||
options,
|
||||
),
|
||||
};
|
||||
};
|
||||
2
node_modules/@hookform/resolvers/ajv/src/index.ts
generated
vendored
Normal file
2
node_modules/@hookform/resolvers/ajv/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './ajv';
|
||||
export * from './types';
|
||||
20
node_modules/@hookform/resolvers/ajv/src/types.ts
generated
vendored
Normal file
20
node_modules/@hookform/resolvers/ajv/src/types.ts
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
import * as Ajv from 'ajv';
|
||||
import type { DefinedError, ErrorObject } from 'ajv';
|
||||
import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
|
||||
|
||||
export type Resolver = <T>(
|
||||
schema: Ajv.JSONSchemaType<T>,
|
||||
schemaOptions?: Ajv.Options,
|
||||
factoryOptions?: { mode?: 'async' | 'sync' },
|
||||
) => <TFieldValues extends FieldValues, TContext>(
|
||||
values: TFieldValues,
|
||||
context: TContext | undefined,
|
||||
options: ResolverOptions<TFieldValues>,
|
||||
) => Promise<ResolverResult<TFieldValues>>;
|
||||
|
||||
// ajv doesn't export any types for errors with `keyword='errorMessage'`
|
||||
type ErrorMessage = ErrorObject<
|
||||
'errorMessage',
|
||||
{ errors: (DefinedError & { emUsed: boolean })[] }
|
||||
>;
|
||||
export type AjvError = ErrorMessage | DefinedError;
|
||||
Reference in New Issue
Block a user