Save current BZZZ config-ui state before CHORUS branding update
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
18
install/config-ui/node_modules/@hookform/resolvers/nope/package.json
generated
vendored
Normal file
18
install/config-ui/node_modules/@hookform/resolvers/nope/package.json
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@hookform/resolvers/nope",
|
||||
"amdName": "hookformResolversNope",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "React Hook Form validation resolver: nope",
|
||||
"main": "dist/nope.js",
|
||||
"module": "dist/nope.module.js",
|
||||
"umd:main": "dist/nope.umd.js",
|
||||
"source": "src/index.ts",
|
||||
"types": "dist/index.d.ts",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"react-hook-form": "^7.0.0",
|
||||
"@hookform/resolvers": "^2.0.0",
|
||||
"nope-validator": "^0.12.0"
|
||||
}
|
||||
}
|
||||
85
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
85
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/Form-native-validation.tsx
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import Nope from 'nope-validator';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { nopeResolver } from '..';
|
||||
|
||||
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
|
||||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
|
||||
|
||||
const schema = Nope.object().shape({
|
||||
username: Nope.string().required(USERNAME_REQUIRED_MESSAGE),
|
||||
password: Nope.string().required(PASSWORD_REQUIRED_MESSAGE),
|
||||
});
|
||||
|
||||
interface FormData {
|
||||
unusedProperty: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const { register, handleSubmit } = useForm<FormData>({
|
||||
resolver: nopeResolver(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 Nope", 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('');
|
||||
});
|
||||
55
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/Form.tsx
generated
vendored
Normal file
55
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/Form.tsx
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import user from '@testing-library/user-event';
|
||||
import Nope from 'nope-validator';
|
||||
import React from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { nopeResolver } from '..';
|
||||
|
||||
const schema = Nope.object().shape({
|
||||
username: Nope.string().required(),
|
||||
password: Nope.string().required(),
|
||||
});
|
||||
|
||||
interface FormData {
|
||||
unusedProperty: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onSubmit: (data: FormData) => void;
|
||||
}
|
||||
|
||||
function TestComponent({ onSubmit }: Props) {
|
||||
const {
|
||||
register,
|
||||
formState: { errors },
|
||||
handleSubmit,
|
||||
} = useForm<FormData>({
|
||||
resolver: nopeResolver(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 Yup 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.getAllByText(/This field is required/i)).toHaveLength(2);
|
||||
expect(handleSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
70
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
70
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/__fixtures__/data.ts
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
import Nope from 'nope-validator';
|
||||
import { Field, InternalFieldName } from 'react-hook-form';
|
||||
|
||||
export const schema = Nope.object().shape({
|
||||
username: Nope.string().regex(/^\w+$/).min(2).max(30).required(),
|
||||
password: Nope.string()
|
||||
.regex(new RegExp('.*[A-Z].*'), 'One uppercase character')
|
||||
.regex(new RegExp('.*[a-z].*'), 'One lowercase character')
|
||||
.regex(new RegExp('.*\\d.*'), 'One number')
|
||||
.regex(
|
||||
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
|
||||
'One special character',
|
||||
)
|
||||
.min(8, 'Must be at least 8 characters in length')
|
||||
.required('New Password is required'),
|
||||
repeatPassword: Nope.string()
|
||||
.oneOf([Nope.ref('password')], "Passwords don't match")
|
||||
.required(),
|
||||
accessToken: Nope.string(),
|
||||
birthYear: Nope.number().min(1900).max(2013),
|
||||
email: Nope.string().email(),
|
||||
tags: Nope.array().of(Nope.string()).required(),
|
||||
enabled: Nope.boolean(),
|
||||
like: Nope.object().shape({
|
||||
id: Nope.number().required(),
|
||||
name: Nope.string().atLeast(4).required(),
|
||||
}),
|
||||
});
|
||||
|
||||
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',
|
||||
},
|
||||
};
|
||||
43
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/__snapshots__/nope.ts.snap
generated
vendored
Normal file
43
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/__snapshots__/nope.ts.snap
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`nopeResolver > should return a single error from nopeResolver when validation fails 1`] = `
|
||||
{
|
||||
"errors": {
|
||||
"birthYear": {
|
||||
"message": "The field is not a valid number",
|
||||
"ref": undefined,
|
||||
},
|
||||
"like": {
|
||||
"id": {
|
||||
"message": "The field is not a valid number",
|
||||
"ref": undefined,
|
||||
},
|
||||
"name": {
|
||||
"message": "This field is required",
|
||||
"ref": undefined,
|
||||
},
|
||||
},
|
||||
"password": {
|
||||
"message": "One uppercase character",
|
||||
"ref": {
|
||||
"name": "password",
|
||||
},
|
||||
},
|
||||
"repeatPassword": {
|
||||
"message": "This field is required",
|
||||
"ref": undefined,
|
||||
},
|
||||
"tags": {
|
||||
"message": "One or more elements are of invalid type",
|
||||
"ref": undefined,
|
||||
},
|
||||
"username": {
|
||||
"message": "This field is required",
|
||||
"ref": {
|
||||
"name": "username",
|
||||
},
|
||||
},
|
||||
},
|
||||
"values": {},
|
||||
}
|
||||
`;
|
||||
28
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/nope.ts
generated
vendored
Normal file
28
install/config-ui/node_modules/@hookform/resolvers/nope/src/__tests__/nope.ts
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
|
||||
import { nopeResolver } from '..';
|
||||
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
||||
|
||||
const shouldUseNativeValidation = false;
|
||||
|
||||
describe('nopeResolver', () => {
|
||||
it('should return values from nopeResolver when validation pass', async () => {
|
||||
const schemaSpy = vi.spyOn(schema, 'validate');
|
||||
|
||||
const result = await nopeResolver(schema)(validData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(schemaSpy).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual({ errors: {}, values: validData });
|
||||
});
|
||||
|
||||
it('should return a single error from nopeResolver when validation fails', async () => {
|
||||
const result = await nopeResolver(schema)(invalidData, undefined, {
|
||||
fields,
|
||||
shouldUseNativeValidation,
|
||||
});
|
||||
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
2
install/config-ui/node_modules/@hookform/resolvers/nope/src/index.ts
generated
vendored
Normal file
2
install/config-ui/node_modules/@hookform/resolvers/nope/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './nope';
|
||||
export * from './types';
|
||||
46
install/config-ui/node_modules/@hookform/resolvers/nope/src/nope.ts
generated
vendored
Normal file
46
install/config-ui/node_modules/@hookform/resolvers/nope/src/nope.ts
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
|
||||
import type { ShapeErrors } from 'nope-validator/lib/cjs/types';
|
||||
import type { FieldError, FieldErrors } from 'react-hook-form';
|
||||
import type { Resolver } from './types';
|
||||
|
||||
const parseErrors = (
|
||||
errors: ShapeErrors,
|
||||
parsedErrors: FieldErrors = {},
|
||||
path = '',
|
||||
) => {
|
||||
return Object.keys(errors).reduce((acc, key) => {
|
||||
const _path = path ? `${path}.${key}` : key;
|
||||
const error = errors[key];
|
||||
|
||||
if (typeof error === 'string') {
|
||||
acc[_path] = {
|
||||
message: error,
|
||||
} as FieldError;
|
||||
} else {
|
||||
parseErrors(error, acc, _path);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, parsedErrors);
|
||||
};
|
||||
|
||||
export const nopeResolver: Resolver =
|
||||
(
|
||||
schema,
|
||||
schemaOptions = {
|
||||
abortEarly: false,
|
||||
},
|
||||
) =>
|
||||
(values, context, options) => {
|
||||
const result = schema.validate(values, context, schemaOptions) as
|
||||
| ShapeErrors
|
||||
| undefined;
|
||||
|
||||
if (result) {
|
||||
return { values: {}, errors: toNestErrors(parseErrors(result), options) };
|
||||
}
|
||||
|
||||
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
|
||||
|
||||
return { values, errors: {} };
|
||||
};
|
||||
19
install/config-ui/node_modules/@hookform/resolvers/nope/src/types.ts
generated
vendored
Normal file
19
install/config-ui/node_modules/@hookform/resolvers/nope/src/types.ts
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { NopeObject } from 'nope-validator/lib/cjs/NopeObject';
|
||||
import type {
|
||||
FieldValues,
|
||||
ResolverOptions,
|
||||
ResolverResult,
|
||||
} from 'react-hook-form';
|
||||
|
||||
type ValidateOptions = Parameters<NopeObject['validate']>[2];
|
||||
type Context = Parameters<NopeObject['validate']>[1];
|
||||
|
||||
export type Resolver = <T extends NopeObject>(
|
||||
schema: T,
|
||||
schemaOptions?: ValidateOptions,
|
||||
resolverOptions?: { mode?: 'async' | 'sync'; rawValues?: boolean },
|
||||
) => <TFieldValues extends FieldValues, TContext extends Context>(
|
||||
values: TFieldValues,
|
||||
context: TContext | undefined,
|
||||
options: ResolverOptions<TFieldValues>,
|
||||
) => ResolverResult<TFieldValues>;
|
||||
Reference in New Issue
Block a user