v1.0 with SW PWA enabled

This commit is contained in:
Blomios
2026-01-01 17:40:53 +01:00
parent 1c0e22aac1
commit 3c8bebb2ad
29775 changed files with 2197201 additions and 119080 deletions

View File

@ -0,0 +1,2 @@
export * from './typanion';
export * from './types';

View File

@ -0,0 +1,2 @@
import type { Resolver } from './types';
export declare const typanionResolver: Resolver;

View File

@ -0,0 +1,2 @@
var r=require("@hookform/resolvers");exports.typanionResolver=function(e,o){return void 0===o&&(o={}),function(s,i,t){var n=[],a=e(s,Object.assign({},{errors:n},o)),u=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.indexOf(":"),s=e.slice(1,o),i=e.slice(o+1).trim();return r[s]={message:i},r},e)}(n);return a?(t.shouldUseNativeValidation&&r.validateFieldsNatively(u,t),{values:s,errors:{}}):{values:{},errors:r.toNestErrors(u,t)}}};
//# sourceMappingURL=typanion.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"typanion.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n"],"names":["validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"8DAoBE,SAACA,EAAWC,eAAgB,IAAhBA,IAAAA,EAAmB,CAAE,GAChCC,SAAAA,EAAQC,EAAGC,GACV,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA7BU,SAACD,EAAkBC,GACrC,YADqCA,IAAAA,IAAAA,EAA4B,CAAE,GAC5DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,QAAAA,GAGKN,CACT,EAAGF,EACL,CAgByBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,yBAAuBZ,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,CAAA,IAGpB,CAAEP,OAAQ,GAAIO,OAAQc,EAAYA,aAACb,EAAcN,GAC1D,CAAC"}

View File

@ -0,0 +1,2 @@
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";var o=function(o,i){return void 0===i&&(i={}),function(n,s,t){var u=[],a=o(n,Object.assign({},{errors:u},i)),v=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.indexOf(":"),i=e.slice(1,o),n=e.slice(o+1).trim();return r[i]={message:n},r},e)}(u);return a?(t.shouldUseNativeValidation&&r(v,t),{values:n,errors:{}}):{values:{},errors:e(v,t)}}};export{o as typanionResolver};
//# sourceMappingURL=typanion.module.js.map

View File

@ -0,0 +1,2 @@
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";const s=(s,o={})=>(t,i,n)=>{const a=[],c=s(t,Object.assign({},{errors:a},o)),l=((r,e={})=>r.reduce((r,e)=>{const s=e.indexOf(":"),o=e.slice(1,s),t=e.slice(s+1).trim();return r[o]={message:t},r},e))(a);return c?(n.shouldUseNativeValidation&&r(l,n),{values:t,errors:{}}):{values:{},errors:e(l,n)}};export{s as typanionResolver};
//# sourceMappingURL=typanion.modern.mjs.map

View File

@ -0,0 +1 @@
{"version":3,"file":"typanion.modern.mjs","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n"],"names":["typanionResolver","validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","parseErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"+EAIA,MAeaA,EACXA,CAACC,EAAWC,EAAmB,CAAE,IACjC,CAACC,EAAQC,EAAGC,KACV,MAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA7BUC,EAACF,EAAkBC,EAA4B,CAAA,IAC1DD,EAAOG,OAAO,CAACC,EAAKC,KACzB,MAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,WAGKN,GACNH,GAiBoBC,CAAYN,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,EAAuBZ,EAAcN,GAEhC,CAAEF,SAAQO,OAAQ,KAGpB,CAAEP,OAAQ,CAAA,EAAIO,OAAQc,EAAab,EAAcN"}

View File

@ -0,0 +1,2 @@
import{validateFieldsNatively as r,toNestErrors as e}from"@hookform/resolvers";var o=function(o,i){return void 0===i&&(i={}),function(n,s,t){var u=[],a=o(n,Object.assign({},{errors:u},i)),v=function(r,e){return void 0===e&&(e={}),r.reduce(function(r,e){var o=e.indexOf(":"),i=e.slice(1,o),n=e.slice(o+1).trim();return r[i]={message:n},r},e)}(u);return a?(t.shouldUseNativeValidation&&r(v,t),{values:n,errors:{}}):{values:{},errors:e(v,t)}}};export{o as typanionResolver};
//# sourceMappingURL=typanion.module.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"typanion.module.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n"],"names":["typanionResolver","validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"+EAIA,IAeaA,EACX,SAACC,EAAWC,eAAgB,IAAhBA,IAAAA,EAAmB,CAAE,GAChCC,SAAAA,EAAQC,EAAGC,GACV,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA7BU,SAACD,EAAkBC,GACrC,YADqCA,IAAAA,IAAAA,EAA4B,CAAE,GAC5DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,QAAAA,GAGKN,CACT,EAAGF,EACL,CAgByBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,EAAuBZ,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,CAAA,IAGpB,CAAEP,OAAQ,GAAIO,OAAQc,EAAab,EAAcN,GAC1D,CAAC"}

View File

@ -0,0 +1,2 @@
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@hookform/resolvers")):"function"==typeof define&&define.amd?define(["exports","@hookform/resolvers"],o):o((e||self).hookformResolversTypanion={},e.hookformResolvers)}(this,function(e,o){e.typanionResolver=function(e,r){return void 0===r&&(r={}),function(n,s,i){var t=[],f=e(n,Object.assign({},{errors:t},r)),l=function(e,o){return void 0===o&&(o={}),e.reduce(function(e,o){var r=o.indexOf(":"),n=o.slice(1,r),s=o.slice(r+1).trim();return e[n]={message:s},e},o)}(t);return f?(i.shouldUseNativeValidation&&o.validateFieldsNatively(l,i),{values:n,errors:{}}):{values:{},errors:o.toNestErrors(l,i)}}}});
//# sourceMappingURL=typanion.umd.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"typanion.umd.js","sources":["../src/typanion.ts"],"sourcesContent":["import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';\nimport type { FieldError, FieldErrors } from 'react-hook-form';\nimport type { Resolver } from './types';\n\nconst parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {\n return errors.reduce((acc, error) => {\n const fieldIndex = error.indexOf(':');\n\n const field = error.slice(1, fieldIndex);\n const message = error.slice(fieldIndex + 1).trim();\n\n acc[field] = {\n message,\n } as FieldError;\n\n return acc;\n }, parsedErrors);\n};\n\nexport const typanionResolver: Resolver =\n (validator, validatorOptions = {}) =>\n (values, _, options) => {\n const rawErrors: string[] = [];\n const isValid = validator(\n values,\n Object.assign(\n {},\n {\n errors: rawErrors,\n },\n validatorOptions,\n ),\n );\n const parsedErrors = parseErrors(rawErrors);\n\n if (isValid) {\n options.shouldUseNativeValidation &&\n validateFieldsNatively(parsedErrors, options);\n\n return { values, errors: {} };\n }\n\n return { values: {}, errors: toNestErrors(parsedErrors, options) };\n };\n"],"names":["validator","validatorOptions","values","_","options","rawErrors","isValid","Object","assign","errors","parsedErrors","reduce","acc","error","fieldIndex","indexOf","field","slice","message","trim","parseErrors","shouldUseNativeValidation","validateFieldsNatively","toNestErrors"],"mappings":"iVAoBE,SAACA,EAAWC,eAAgB,IAAhBA,IAAAA,EAAmB,CAAE,GAChCC,SAAAA,EAAQC,EAAGC,GACV,IAAMC,EAAsB,GACtBC,EAAUN,EACdE,EACAK,OAAOC,OACL,GACA,CACEC,OAAQJ,GAEVJ,IAGES,EA7BU,SAACD,EAAkBC,GACrC,YADqCA,IAAAA,IAAAA,EAA4B,CAAE,GAC5DD,EAAOE,OAAO,SAACC,EAAKC,GACzB,IAAMC,EAAaD,EAAME,QAAQ,KAE3BC,EAAQH,EAAMI,MAAM,EAAGH,GACvBI,EAAUL,EAAMI,MAAMH,EAAa,GAAGK,OAM5C,OAJAP,EAAII,GAAS,CACXE,QAAAA,GAGKN,CACT,EAAGF,EACL,CAgByBU,CAAYf,GAEjC,OAAIC,GACFF,EAAQiB,2BACNC,yBAAuBZ,EAAcN,GAEhC,CAAEF,OAAAA,EAAQO,OAAQ,CAAA,IAGpB,CAAEP,OAAQ,GAAIO,OAAQc,EAAYA,aAACb,EAAcN,GAC1D,CAAC"}

View File

@ -0,0 +1,9 @@
import type { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form';
import { AnyStrictValidator, ValidationState } from 'typanion';
type ValidateOptions = Pick<ValidationState, 'coercions' | 'coercion'>;
type RHFResolver = <TFieldValues extends FieldValues, TContext>(values: TFieldValues, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => ResolverResult<TFieldValues>;
export type Resolver = <UnknownValidator extends AnyStrictValidator>(validator: UnknownValidator, validatorOptions?: ValidateOptions, resolverOptions?: {
mode?: 'async' | 'sync';
rawValues?: boolean;
}) => RHFResolver;
export {};

View File

@ -0,0 +1,18 @@
{
"name": "@hookform/resolvers/typanion",
"amdName": "hookformResolversTypanion",
"version": "1.0.0",
"private": true,
"description": "React Hook Form validation resolver: typanion",
"main": "dist/typanion.js",
"module": "dist/typanion.module.js",
"umd:main": "dist/typanion.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",
"typanion": "^3.3.2"
}
}

View File

@ -0,0 +1,85 @@
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 t from 'typanion';
import { typanionResolver } from '..';
const ERROR_MESSAGE =
'Expected to have a length of at least 1 elements (got 0)';
const schema = t.isObject({
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
password: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
});
interface FormData {
unusedProperty: string;
username: string;
password: string;
}
interface Props {
onSubmit: (data: FormData) => void;
}
function TestComponent({ onSubmit }: Props) {
const { register, handleSubmit } = useForm<FormData>({
resolver: typanionResolver(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 Typanion", 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(ERROR_MESSAGE);
// password
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
expect(passwordField.validity.valid).toBe(false);
expect(passwordField.validationMessage).toBe(ERROR_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('');
});

View File

@ -0,0 +1,59 @@
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 t from 'typanion';
import { typanionResolver } from '..';
const schema = t.isObject({
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
password: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
});
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: typanionResolver(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 Typanion 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(
'Expected to have a length of at least 1 elements (got 0)',
),
).toHaveLength(2);
expect(handleSubmit).not.toHaveBeenCalled();
});

View File

@ -0,0 +1,82 @@
import { Field, InternalFieldName } from 'react-hook-form';
import * as t from 'typanion';
export const isSchema = t.isObject({
username: t.applyCascade(t.isString(), [
t.matchesRegExp(/^\w+$/),
t.hasMinLength(2),
t.hasMaxLength(30),
]),
password: t.applyCascade(t.isString(), [
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character
t.matchesRegExp(new RegExp('.*\\d.*')), // one number
t.matchesRegExp(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
), // one special character
t.hasMinLength(8), // Must be at least 8 characters in length
]),
repeatPassword: t.applyCascade(t.isString(), [
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character
t.matchesRegExp(new RegExp('.*\\d.*')), // one number
t.matchesRegExp(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
), // one special character
t.hasMinLength(8), // Must be at least 8 characters in length
]),
accessToken: t.isString(),
birthYear: t.applyCascade(t.isNumber(), [
t.isInteger(),
t.isInInclusiveRange(1900, 2013),
]),
email: t.applyCascade(t.isString(), [t.matchesRegExp(/^\S+@\S+$/)]),
tags: t.isArray(t.isString()),
enabled: t.isBoolean(),
like: t.isObject({
id: t.applyCascade(t.isNumber(), [t.isInteger(), t.isPositive()]),
name: t.applyCascade(t.isString(), [t.hasMinLength(4)]),
}),
});
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',
},
};

View File

@ -0,0 +1,67 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`typanionResolver > should return a single error from typanionResolver when validation fails 1`] = `
{
"errors": {
"accessToken": {
"message": "Expected a string (got undefined)",
"ref": undefined,
},
"birthYear": {
"message": "Expected a number (got "birthYear")",
"ref": undefined,
},
"email": {
"message": "Expected to match the pattern /^\\S+@\\S+$/ (got an empty string)",
"ref": {
"name": "email",
},
},
"enabled": {
"message": "Expected a boolean (got undefined)",
"ref": undefined,
},
"like": {
"id": {
"message": "Expected a number (got "z")",
"ref": undefined,
},
"name": {
"message": "Expected a string (got undefined)",
"ref": undefined,
},
},
"password": {
"message": "Expected to match the pattern /.*[A-Z].*/ (got "___")",
"ref": {
"name": "password",
},
},
"repeatPassword": {
"message": "Expected a string (got undefined)",
"ref": undefined,
},
"tags": [
{
"message": "Expected a string (got 1)",
"ref": undefined,
},
{
"message": "Expected a string (got 2)",
"ref": undefined,
},
{
"message": "Expected a string (got 3)",
"ref": undefined,
},
],
"username": {
"message": "Expected a string (got undefined)",
"ref": {
"name": "username",
},
},
},
"values": {},
}
`;

View File

@ -0,0 +1,35 @@
import { typanionResolver } from '..';
import { fields, invalidData, isSchema, validData } from './__fixtures__/data';
const tmpObj = {
validate: isSchema,
};
const shouldUseNativeValidation = false;
describe('typanionResolver', () => {
it('should return values from typanionResolver when validation pass', async () => {
const schemaSpy = vi.spyOn(tmpObj, 'validate');
const result = await typanionResolver(schemaSpy as any)(
validData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(schemaSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return a single error from typanionResolver when validation fails', async () => {
const result = await typanionResolver(isSchema)(invalidData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toMatchSnapshot();
});
});

View File

@ -0,0 +1,2 @@
export * from './typanion';
export * from './types';

View File

@ -0,0 +1,44 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import type { FieldError, FieldErrors } from 'react-hook-form';
import type { Resolver } from './types';
const parseErrors = (errors: string[], parsedErrors: FieldErrors = {}) => {
return errors.reduce((acc, error) => {
const fieldIndex = error.indexOf(':');
const field = error.slice(1, fieldIndex);
const message = error.slice(fieldIndex + 1).trim();
acc[field] = {
message,
} as FieldError;
return acc;
}, parsedErrors);
};
export const typanionResolver: Resolver =
(validator, validatorOptions = {}) =>
(values, _, options) => {
const rawErrors: string[] = [];
const isValid = validator(
values,
Object.assign(
{},
{
errors: rawErrors,
},
validatorOptions,
),
);
const parsedErrors = parseErrors(rawErrors);
if (isValid) {
options.shouldUseNativeValidation &&
validateFieldsNatively(parsedErrors, options);
return { values, errors: {} };
}
return { values: {}, errors: toNestErrors(parsedErrors, options) };
};

View File

@ -0,0 +1,20 @@
import type {
FieldValues,
ResolverOptions,
ResolverResult,
} from 'react-hook-form';
import { AnyStrictValidator, ValidationState } from 'typanion';
type ValidateOptions = Pick<ValidationState, 'coercions' | 'coercion'>;
type RHFResolver = <TFieldValues extends FieldValues, TContext>(
values: TFieldValues,
context: TContext | undefined,
options: ResolverOptions<TFieldValues>,
) => ResolverResult<TFieldValues>;
export type Resolver = <UnknownValidator extends AnyStrictValidator>(
validator: UnknownValidator,
validatorOptions?: ValidateOptions,
resolverOptions?: { mode?: 'async' | 'sync'; rawValues?: boolean },
) => RHFResolver;