added apps

This commit is contained in:
berkay 2025-04-10 20:11:36 +03:00
parent 3613f9030e
commit c3b7556e7e
346 changed files with 265054 additions and 159 deletions

View File

@ -446,7 +446,6 @@ def authentication_page_valid(
): ):
""" """
Verify if page is valid returns application that can user reach Verify if page is valid returns application that can user reach
page: { url = /building/create}
result: { "sites": ['/dashboard', '/building/create'] } result: { "sites": ['/dashboard', '/building/create'] }
""" """
token = request.headers.get(api_config.ACCESS_TOKEN_TAG, None) token = request.headers.get(api_config.ACCESS_TOKEN_TAG, None)
@ -463,7 +462,9 @@ def authentication_page_valid(
status_code=status.HTTP_406_NOT_ACCEPTABLE, status_code=status.HTTP_406_NOT_ACCEPTABLE,
headers=headers, headers=headers,
) )
result = AuthHandlers.PageHandlers.retrieve_valid_sites_via_token(access_token=token) result = AuthHandlers.PageHandlers.retrieve_valid_sites_via_token(
access_token=token
)
if not result: if not result:
return JSONResponse( return JSONResponse(
content={"error": "EYS_0004"}, content={"error": "EYS_0004"},
@ -471,8 +472,7 @@ def authentication_page_valid(
headers=headers, headers=headers,
) )
return JSONResponse( return JSONResponse(
content={"sites": result}, content={"sites": list(set(result))},
status_code=status.HTTP_202_ACCEPTED, status_code=status.HTTP_202_ACCEPTED,
headers=headers, headers=headers,
) )

View File

@ -13,8 +13,6 @@ from ApiServices.AuthService.config import api_config
from Schemas import ( from Schemas import (
Users, Users,
People, People,
UsersTokens,
Credentials,
BuildLivingSpace, BuildLivingSpace,
BuildParts, BuildParts,
OccupantTypes, OccupantTypes,
@ -70,7 +68,6 @@ class RedisHandlers:
result_delete = RedisActions.delete( result_delete = RedisActions.delete(
list_keys=[RedisHandlers.AUTH_TOKEN, "*", str(user.uu_id)] list_keys=[RedisHandlers.AUTH_TOKEN, "*", str(user.uu_id)]
) )
print("result_delete", result_delete)
generated_access_token = PasswordModule.generate_access_token() generated_access_token = PasswordModule.generate_access_token()
keys = [RedisHandlers.AUTH_TOKEN, generated_access_token, str(user.uu_id)] keys = [RedisHandlers.AUTH_TOKEN, generated_access_token, str(user.uu_id)]
RedisActions.set_json( RedisActions.set_json(
@ -81,7 +78,9 @@ class RedisHandlers:
return generated_access_token return generated_access_token
@classmethod @classmethod
def update_token_at_redis(cls, token: str, add_payload: Union[CompanyToken, OccupantToken]): def update_token_at_redis(
cls, token: str, add_payload: Union[CompanyToken, OccupantToken]
):
if already_token_data := RedisActions.get_json( if already_token_data := RedisActions.get_json(
list_keys=[RedisHandlers.AUTH_TOKEN, token, "*"] list_keys=[RedisHandlers.AUTH_TOKEN, token, "*"]
).first: ).first:
@ -99,7 +98,6 @@ class RedisHandlers:
value=already_token.model_dump(), value=already_token.model_dump(),
expires={"hours": 1, "minutes": 30}, expires={"hours": 1, "minutes": 30},
) )
print("result.first", result.first)
return result.first return result.first
raise ValueError("Something went wrong") raise ValueError("Something went wrong")
@ -124,18 +122,12 @@ class UserHandlers:
return found_user return found_user
@staticmethod @staticmethod
def check_password_valid(domain: str, id_: str, password: str, password_hashed: str) -> bool: def check_password_valid(
domain: str, id_: str, password: str, password_hashed: str
) -> bool:
""" """
Check if the password is valid. Check if the password is valid.
""" """
print(
dict(
domain=domain,
id_=id_,
password=password,
password_hashed=password_hashed,
)
)
if PasswordModule.check_password( if PasswordModule.check_password(
domain=domain, id_=id_, password=password, password_hashed=password_hashed domain=domain, id_=id_, password=password, password_hashed=password_hashed
): ):
@ -158,7 +150,9 @@ class LoginHandler:
return str(email).split("@")[1] == api_config.ACCESS_EMAIL_EXT return str(email).split("@")[1] == api_config.ACCESS_EMAIL_EXT
@classmethod @classmethod
def do_employee_login(cls, request: Any, data: Any, extra_dict: Optional[Dict[str, Any]] = None): def do_employee_login(
cls, request: Any, data: Any, extra_dict: Optional[Dict[str, Any]] = None
):
""" """
Handle employee login. Handle employee login.
""" """
@ -268,7 +262,9 @@ class LoginHandler:
raise ValueError("Something went wrong") raise ValueError("Something went wrong")
@classmethod @classmethod
def do_employee_occupant(cls, request: Any, data: Any, extra_dict: Optional[Dict[str, Any]] = None): def do_employee_occupant(
cls, request: Any, data: Any, extra_dict: Optional[Dict[str, Any]] = None
):
""" """
Handle occupant login. Handle occupant login.
""" """
@ -417,7 +413,9 @@ class LoginHandler:
return request.headers.get(api_config.ACCESS_TOKEN_TAG) return request.headers.get(api_config.ACCESS_TOKEN_TAG)
@classmethod @classmethod
def handle_employee_selection(cls, access_token: str, data: Any, token_dict: TokenDictType): def handle_employee_selection(
cls, access_token: str, data: Any, token_dict: TokenDictType
):
with Users.new_session() as db: with Users.new_session() as db:
if data.company_uu_id not in token_dict.companies_uu_id_list: if data.company_uu_id not in token_dict.companies_uu_id_list:
ValueError("EYS_0011") ValueError("EYS_0011")
@ -491,7 +489,7 @@ class LoginHandler:
employee_id=employee.id, employee_id=employee.id,
employee_uu_id=employee.uu_id.__str__(), employee_uu_id=employee.uu_id.__str__(),
reachable_event_codes=reachable_event_codes, reachable_event_codes=reachable_event_codes,
reachable_app_codes=reachable_app_codes reachable_app_codes=reachable_app_codes,
) )
redis_handler = RedisHandlers() redis_handler = RedisHandlers()
redis_result = redis_handler.update_token_at_redis( redis_result = redis_handler.update_token_at_redis(
@ -502,7 +500,9 @@ class LoginHandler:
} }
@classmethod @classmethod
def handle_occupant_selection(cls, access_token: str, data: Any, token_dict: TokenDictType): def handle_occupant_selection(
cls, access_token: str, data: Any, token_dict: TokenDictType
):
"""Handle occupant type selection""" """Handle occupant type selection"""
with BuildLivingSpace.new_session() as db: with BuildLivingSpace.new_session() as db:
# Get selected occupant type # Get selected occupant type
@ -635,13 +635,10 @@ class PasswordHandler:
raise ValueError("") raise ValueError("")
collection_name = f"{found_user.related_company}*Domain" collection_name = f"{found_user.related_company}*Domain"
print("collection_name", collection_name)
with mongo_handler.collection(collection_name) as mongo_engine: with mongo_handler.collection(collection_name) as mongo_engine:
print({"user_uu_id": str(found_user.uu_id)})
domain_via_user = mongo_engine.find_one( domain_via_user = mongo_engine.find_one(
{"user_uu_id": str(found_user.uu_id)} {"user_uu_id": str(found_user.uu_id)}
) )
print("domain_via_user", domain_via_user)
if not domain_via_user: if not domain_via_user:
raise ValueError("EYS_0024") raise ValueError("EYS_0024")
domain_via_user = domain_via_user.get("main_domain", None) domain_via_user = domain_via_user.get("main_domain", None)
@ -705,7 +702,7 @@ class PasswordHandler:
class PageHandlers: class PageHandlers:
@classmethod @classmethod
def retrieve_valid_page_via_token(cls, access_token: str, page_url: str) -> str: def retrieve_valid_page_via_token(cls, access_token: str, page_url: str) -> str:
""" """
@ -718,14 +715,17 @@ class PageHandlers:
""" """
if result := RedisHandlers.get_object_from_redis(access_token=access_token): if result := RedisHandlers.get_object_from_redis(access_token=access_token):
if result.is_employee: if result.is_employee:
if application := result.selected_company.reachable_app_codes.get(page_url, None): if application := result.selected_company.reachable_app_codes.get(
page_url, None
):
return application return application
elif result.is_occupant: elif result.is_occupant:
if application := result.selected_company.reachable_app_codes.get(page_url, None): if application := result.selected_company.reachable_app_codes.get(
page_url, None
):
return application return application
raise ValueError("EYS_0013") raise ValueError("EYS_0013")
@classmethod @classmethod
def retrieve_valid_sites_via_token(cls, access_token: str) -> list: def retrieve_valid_sites_via_token(cls, access_token: str) -> list:
""" """

View File

@ -59,7 +59,9 @@ class OccupantToken(BaseModel):
responsible_employee_id: Optional[int] = None responsible_employee_id: Optional[int] = None
responsible_employee_uuid: Optional[str] = None responsible_employee_uuid: Optional[str] = None
reachable_event_codes: Optional[dict[str, str]] = None # ID list of reachable modules reachable_event_codes: Optional[dict[str, str]] = (
None # ID list of reachable modules
)
reachable_app_codes: Optional[dict[str, str]] = None # ID list of reachable modules reachable_app_codes: Optional[dict[str, str]] = None # ID list of reachable modules
@ -83,7 +85,9 @@ class CompanyToken(BaseModel):
bulk_duties_id: int bulk_duties_id: int
reachable_event_codes: Optional[dict[str, str]] = None # ID list of reachable modules reachable_event_codes: Optional[dict[str, str]] = (
None # ID list of reachable modules
)
reachable_app_codes: Optional[dict[str, str]] = None # ID list of reachable modules reachable_app_codes: Optional[dict[str, str]] = None # ID list of reachable modules

View File

@ -33,4 +33,3 @@ from Schemas import (
application_code=APP001 application_code=APP001
""" """

View File

@ -14,11 +14,15 @@ if __name__ == "__main__":
""" """
with get_db() as db_session: with get_db() as db_session:
if super_man := Users.filter_one( if super_man := Users.filter_one(
Users.email == "karatay.berkay.sup@evyos.com.tr", db=db_session Users.email == "karatay.berkay.sup@evyos.com.tr", db=db_session
).data: ).data:
super_employee = Employees.filter_one( super_employee = Employees.filter_one(
Employees.people_id == super_man.person_id, db=db_session Employees.people_id == super_man.person_id, db=db_session
).data ).data
init_service_to_event_matches_for_super_user(super_user=super_employee, db_session=db_session) init_service_to_event_matches_for_super_user(
init_applications_for_super_user(super_user=super_employee, db_session=db_session) super_user=super_employee, db_session=db_session
)
init_applications_for_super_user(
super_user=super_employee, db_session=db_session
)

View File

@ -1,40 +1,111 @@
from Schemas import ( from Schemas import Applications, Application2Employee, Employees
Applications,
Application2Employee,
Employees
)
def init_applications_for_super_user(super_user: Employees, db_session=None) -> None: def init_applications_for_super_user(super_user: Employees, db_session=None) -> None:
list_of_created_apps = [ list_of_created_apps = [
dict( dict(
name="Dashboard1", name="Dashboard1",
application_code = "app000001", application_code="app000001",
site_url = "/dashboard", site_url="/dashboard",
application_type = "info", application_type="info",
description = "Dashboard Page" description="Dashboard Page",
), ),
dict( dict(
name="Dashboard2", name="Individual",
application_code = "app000002", application_code="app000003",
site_url = "/buildings/list", site_url="/individual",
application_type = "CreateFrom", application_type="Dash",
description = "Dashboard Page" description="Individual Page for people",
),
dict(
name="User",
application_code="app000004",
site_url="/user",
application_type="Dash",
description="Individual Page for user",
),
dict(
name="Build",
application_code="app000005",
site_url="/build",
application_type="Dash",
description="Individual Page for build",
),
dict(
name="BuildParts",
application_code="app000006",
site_url="/build/parts",
application_type="Dash",
description="Individual Page for build parts",
),
dict(
name="BuildArea",
application_code="app000007",
site_url="/build/area",
application_type="Dash",
description="Individual Page for build area",
),
dict(
name="ManagementAccounting",
application_code="app000008",
site_url="/management/accounting",
application_type="Dash",
description="Individual Page for management accounting",
),
dict(
name="ManagementBudget",
application_code="app000009",
site_url="/management/budget",
application_type="Dash",
description="Individual Page for management accounting2",
),
dict(
name="ManagementMeetingClose",
application_code="app000010",
site_url="/annual/meeting/close",
application_type="Dash",
description="Individual Page for management accounting3",
),
dict(
name="EmergencyMeeting",
application_code="app000011",
site_url="/emergency/meeting",
application_type="Dash",
description="Individual Page for management accounting4",
),
dict(
name="EmergencyMeetingClose",
application_code="app000012",
site_url="/emergency/meeting/close",
application_type="Dash",
description="Individual Page for management accounting5",
),
dict(
name="MeetingParticipation",
application_code="app000013",
site_url="/meeting/participation",
application_type="Dash",
description="Individual Page for management accounting6",
), ),
] ]
for list_of_created_app in list_of_created_apps: for list_of_created_app in list_of_created_apps:
dashboard_page = Applications.find_or_create( created_page = Applications.find_or_create(
**list_of_created_app, db=db_session, **list_of_created_app,
db=db_session,
is_confirmed=True,
) )
print('dashboard_page', dashboard_page) if created_page.meta_data.created:
if dashboard_page.meta_data.created: created_page.save(db=db_session)
dashboard_page.save(db=db_session)
Application2Employee.find_or_create( application_employee_created = Application2Employee.find_or_create(
employee_id=super_user.id, employee_id=super_user.id,
employee_uu_id=str(super_user.uu_id), employee_uu_id=str(super_user.uu_id),
site_url=dashboard_page.site_url, site_url=created_page.site_url,
application_code=dashboard_page.application_code, application_code=created_page.application_code,
application_id=dashboard_page.id, application_id=created_page.id,
application_uu_id=str(dashboard_page.uu_id), application_uu_id=str(created_page.uu_id),
db=db_session, is_confirmed=True,
) db=db_session,
)
if application_employee_created.meta_data.created:
application_employee_created.save(db=db_session)

View File

@ -15,7 +15,8 @@ list_of_event_codes = []
def init_service_to_event_matches_for_super_user(super_user, db_session=None) -> None: def init_service_to_event_matches_for_super_user(super_user, db_session=None) -> None:
service_match = Services.filter_one( service_match = Services.filter_one(
Services.service_name == "Super User", db=db_session, Services.service_name == "Super User",
db=db_session,
).data ).data
for list_of_event_code in list_of_event_codes: for list_of_event_code in list_of_event_codes:
created_service = Service2Events.find_or_create( created_service = Service2Events.find_or_create(
@ -38,7 +39,7 @@ def init_service_to_event_matches_for_super_user(super_user, db_session=None) ->
employee_id=super_user.id, employee_id=super_user.id,
employee_uu_id=str(super_user.uu_id), employee_uu_id=str(super_user.uu_id),
is_confirmed=True, is_confirmed=True,
db=db_session db=db_session,
) )
if employee_added_service.meta_data.created: if employee_added_service.meta_data.created:
employee_added_service.save(db=db_session) employee_added_service.save(db=db_session)

View File

@ -43,7 +43,7 @@ class MongoDBConfig:
def __init__( def __init__(
self, self,
uri: str = "mongodb://localhost:27017/", uri: str = "mongodb://localhost:27017/",
max_pool_size: int = 50, max_pool_size: int = 20,
min_pool_size: int = 10, min_pool_size: int = 10,
max_idle_time_ms: int = 30000, max_idle_time_ms: int = 30000,
wait_queue_timeout_ms: int = 2000, wait_queue_timeout_ms: int = 2000,
@ -84,11 +84,11 @@ class MongoDBHandler(MongoDBConfig):
def __init__( def __init__(
self, self,
uri: str, uri: str,
max_pool_size: int = 50, max_pool_size: int = 20,
min_pool_size: int = 10, min_pool_size: int = 5,
max_idle_time_ms: int = 30000, max_idle_time_ms: int = 10000,
wait_queue_timeout_ms: int = 2000, wait_queue_timeout_ms: int = 1000,
server_selection_timeout_ms: int = 5000, server_selection_timeout_ms: int = 3000,
**additional_options, **additional_options,
): ):
""" """
@ -201,9 +201,11 @@ class CollectionContext:
self.collection = None self.collection = None
mongo_handler = MongoDBHandler( mongo_handler = MongoDBHandler(uri=mongo_configs.url)
uri=mongo_configs.url, """
max_pool_size=100, max_pool_size: int = 20,
min_pool_size=20, min_pool_size: int = 10,
max_idle_time_ms=60000, max_idle_time_ms: int = 30000,
) wait_queue_timeout_ms: int = 2000,
server_selection_timeout_ms: int = 5000,
"""

View File

@ -412,16 +412,19 @@ class Application2Employee(CrudCollection):
) )
@classmethod @classmethod
def get_application_codes(cls, employee_id: int, db) -> dict[str , str]: def get_application_codes(cls, employee_id: int, db) -> dict[str, str]:
print('employee_id', employee_id) print("employee_id", employee_id)
employee_applications = cls.filter_all( employee_applications = cls.filter_all(
Application2Employee.employee_id == employee_id, db=db, Application2Employee.employee_id == employee_id,
db=db,
).data ).data
applications_dict = {} applications_dict = {}
print('employee_applications', employee_applications) print("employee_applications", employee_applications)
for employee_application in employee_applications: for employee_application in employee_applications:
if employee_application.site_url not in applications_dict: if employee_application.site_url not in applications_dict:
applications_dict[str(employee_application.site_url)] = str(employee_application.application_code) applications_dict[str(employee_application.site_url)] = str(
employee_application.application_code
)
return applications_dict return applications_dict
__table_args__ = ( __table_args__ = (
@ -462,14 +465,17 @@ class Application2Occupant(CrudCollection):
) )
@classmethod @classmethod
def get_application_codes(cls, build_living_space_id: int, db) -> dict[str , str]: def get_application_codes(cls, build_living_space_id: int, db) -> dict[str, str]:
occupant_applications = cls.filter_all( occupant_applications = cls.filter_all(
cls.build_living_space_id == build_living_space_id, db=db, cls.build_living_space_id == build_living_space_id,
db=db,
).data ).data
applications_dict = {} applications_dict = {}
for occupant_application in occupant_applications: for occupant_application in occupant_applications:
if occupant_application.site_url not in applications_dict: if occupant_application.site_url not in applications_dict:
applications_dict[str(occupant_application.site_url)] = str(occupant_application.application_code) applications_dict[str(occupant_application.site_url)] = str(
occupant_application.application_code
)
return applications_dict return applications_dict
__table_args__ = ( __table_args__ = (

View File

@ -7,4 +7,3 @@
### Management-Backend ### Management-Backend
4 Management use only 4 Management use only

View File

@ -1,9 +1,10 @@
"use server"; "use server";
import { fetchDataWithToken, fetchData } from "../api-fetcher"; import { fetchDataWithToken } from "../api-fetcher";
import { baseUrlAuth, tokenSecret } from "../basics"; import { baseUrlAuth, tokenSecret } from "../basics";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
import NextCrypto from "next-crypto"; import NextCrypto from "next-crypto";
const checkToken = `${baseUrlAuth}/authentication/token/check`; const checkToken = `${baseUrlAuth}/authentication/token/check`;
const pageValid = `${baseUrlAuth}/authentication/page/valid`; const pageValid = `${baseUrlAuth}/authentication/page/valid`;
const siteUrls = `${baseUrlAuth}/authentication/sites/list`; const siteUrls = `${baseUrlAuth}/authentication/sites/list`;
@ -18,11 +19,13 @@ async function checkAccessTokenIsValid() {
async function retrievePageList() { async function retrievePageList() {
const response = await fetchDataWithToken(siteUrls, {}, "GET", false); const response = await fetchDataWithToken(siteUrls, {}, "GET", false);
return response?.status === 200 || response?.status === 202 return response?.status === 200 || response?.status === 202
? response.data?.site_urls ? response.data?.sites
: null; : null;
} }
async function retrievePagebyUrl(pageUrl: string) { async function retrievePagebyUrl(pageUrl: string) {
const response = await fetchDataWithToken( const response = await fetchDataWithToken(
pageValid, pageValid,
{ {

View File

@ -9,8 +9,8 @@ export const metadata: Metadata = {
export default function LoginPage() { export default function LoginPage() {
return ( return (
<div className="min-h-screen bg-gray-100"> <>
<Login /> <Login />
</div> </>
); );
} }

View File

@ -12,8 +12,18 @@ export default function AuthLayout({
children: React.ReactNode; children: React.ReactNode;
}) { }) {
return ( return (
<div className="min-h-screen flex"> <div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense> <div className="w-1/4">
<div className="flex flex-col items-center justify-center h-screen bg-purple-600">
<div className="text-2xl font-bold">WAG Frontend</div>
<div className="text-sm text-gray-500 mt-4">
Welcome to the WAG Frontend Application
</div>
</div>
</div>
<div className="w-3/4 text-black">
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense>
</div>
</div> </div>
); );
} }

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/annual/meeting/close";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/annual/meeting";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -1,27 +1,17 @@
"use server";
import React from "react"; import React from "react";
import {
checkAccessTokenIsValid,
retrievePageList,
retrievePagebyUrl,
} from "@/apicalls/cookies/token";
import { redirect } from "next/navigation";
import { retrievePage } from "@/components/NavigatePages";
import LeftMenu from "@/components/menu/leftMenu"; import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList } from "@/apicalls/cookies/token";
export default async function DashboardLayout({ export default async function DashboardLayout({
searchParams, searchParams,
}: { }: {
searchParams: Promise<{ [key: string]: string | undefined }>; searchParams: Promise<{ [key: string]: string | undefined }>;
}) { }) {
const token_is_valid = await checkAccessTokenIsValid();
const siteUrlsList = (await retrievePageList()) || []; const siteUrlsList = (await retrievePageList()) || [];
if (!token_is_valid) {
redirect("/auth/login");
}
const lang = "tr"; const lang = "tr";
const searchParamsInstance = await searchParams; const searchParamsInstance = await searchParams;
const pageToDirect = await retrievePagebyUrl("/dashboard"); const activePage = "/dashboard";
const PageComponent = retrievePage(pageToDirect);
return ( return (
<> <>
@ -32,6 +22,7 @@ export default async function DashboardLayout({
pageUuidList={siteUrlsList} pageUuidList={siteUrlsList}
lang={lang} lang={lang}
searchParams={searchParamsInstance} searchParams={searchParamsInstance}
pageSelected={activePage}
/> />
</aside> </aside>
@ -49,7 +40,6 @@ export default async function DashboardLayout({
<div className="w-10 h-10 bg-gray-300 rounded-full"></div> <div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div> </div>
</header> </header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div> </div>
</div> </div>
</> </>

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/emergency/meeting/close";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/emergency/meeting";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
import LeftMenu from "@/components/menu/leftMenu";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/individual";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -1,19 +1,24 @@
import type { Metadata } from "next"; import type { Metadata } from "next";
import { Suspense } from "react"; import { checkAccessTokenIsValid } from "@/apicalls/cookies/token";
import { redirect } from "next/navigation";
export const metadata: Metadata = { export const metadata: Metadata = {
title: "Create Next App", title: "Create Next App",
description: "Generated by create next app", description: "Generated by create next app",
}; };
export default function DashLayout({ export default async function DashLayout({
children, children,
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) { }) {
const token_is_valid = await checkAccessTokenIsValid();
if (!token_is_valid) {
redirect("/auth/login");
}
return ( return (
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden"> <div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense> {children}
</div> </div>
); );
} }

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
import LeftMenu from "@/components/menu/leftMenu";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/management/accounting";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
import LeftMenu from "@/components/menu/leftMenu";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/management/budget";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,52 @@
"use server";
import React from "react";
import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/meeting/participation";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,51 @@
"use server";
import React from "react";
import LeftMenu from "@/components/menu/leftMenu";
import { retrievePageList, retrievePagebyUrl } from "@/apicalls/cookies/token";
import { retrievePage } from "@/components/NavigatePages";
export default async function Dashboard({
searchParams,
}: {
searchParams: Promise<{ [key: string]: string | undefined }>;
}) {
const siteUrlsList = (await retrievePageList()) || [];
const lang = "tr";
const searchParamsInstance = await searchParams;
const activePage = "/user";
const pageToDirect = await retrievePagebyUrl(activePage);
const PageComponent = retrievePage(pageToDirect);
return (
<>
<div className="min-h-screen min-w-screen flex h-screen w-screen overflow-hidden">
{/* Sidebar */}
<aside className="w-1/4 border-r p-4 overflow-y-auto">
<LeftMenu
pageUuidList={siteUrlsList}
lang={lang}
searchParams={searchParamsInstance}
pageSelected={activePage}
/>
</aside>
{/* Main Content Area */}
<div className="flex flex-col w-3/4">
{/* Sticky Header */}
<header className="sticky top-0 bg-white shadow-md z-10 p-4 flex justify-between items-center">
<h1 className="text-2xl font-semibold">{activePage}</h1>
<div className="flex items-center space-x-4">
<input
type="text"
placeholder="Search..."
className="border px-3 py-2 rounded-lg"
/>
<div className="w-10 h-10 bg-gray-300 rounded-full"></div>
</div>
</header>
<PageComponent lang={lang} queryParams={searchParamsInstance} />
</div>
</div>
</>
);
}

View File

@ -0,0 +1,102 @@
"use client";
import React from "react";
import { Pencil, Plus } from "lucide-react";
// Define types
interface CardData {
id: number;
title: string;
description: string;
status: string;
lastUpdated: string;
}
interface CardProps {
data: CardData;
onUpdate: (id: number) => void;
}
// Mock data
const mockData: CardData[] = [
{
id: 1,
title: "Project Alpha",
description: "A cutting-edge project for automation",
status: "In Progress",
lastUpdated: "2024-03-15",
},
{
id: 2,
title: "Project Beta",
description: "Machine learning integration project",
status: "Completed",
lastUpdated: "2024-03-10",
},
{
id: 3,
title: "Project Gamma",
description: "Cloud infrastructure optimization",
status: "Planning",
lastUpdated: "2024-03-05",
},
];
// Card component
const Card: React.FC<CardProps> = ({ data, onUpdate }) => (
<div className="bg-white rounded-lg shadow-md p-6 mb-4 hover:shadow-lg transition-shadow">
<div className="flex justify-between items-start">
<div>
<h3 className="text-xl font-semibold mb-2">{data.title}</h3>
<p className="text-gray-600 mb-2">{data.description}</p>
<div className="flex items-center gap-4">
<span className="text-sm text-gray-500">Status: {data.status}</span>
<span className="text-sm text-gray-500">
Last Updated: {data.lastUpdated}
</span>
</div>
</div>
<button
onClick={() => onUpdate(data.id)}
className="text-blue-500 hover:text-blue-700 p-2"
aria-label="Update"
>
<Pencil />
</button>
</div>
</div>
);
function app000002() {
const handleUpdate = (id: number) => {
console.log(`Update clicked for item ${id}`);
// Add your update logic here
};
const handleCreate = () => {
console.log("Create clicked");
// Add your create logic here
};
return (
<div className="container mx-auto p-6">
<div className="flex justify-between items-center mb-6">
<h1 className="text-2xl font-bold">Projects Dashboard</h1>
<button
onClick={handleCreate}
className="bg-blue-500 text-white px-4 py-2 rounded-lg flex items-center gap-2 hover:bg-blue-600 transition-colors"
>
<Plus />
Create New
</button>
</div>
<div className="grid gap-4">
{mockData.map((item) => (
<Card key={item.id} data={item} onUpdate={handleUpdate} />
))}
</div>
</div>
);
}
export default app000002;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000003() {
return <div>app000003</div>;
}
export default app000003;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000004() {
return <div>app000004</div>;
}
export default app000004;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000005() {
return <div>app000005</div>;
}
export default app000005;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000006() {
return <div>app000006</div>;
}
export default app000006;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000007() {
return <div>app000007</div>;
}
export default app000007;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000008() {
return <div>app000008</div>;
}
export default app000008;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000009() {
return <div>app000009</div>;
}
export default app000009;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000010() {
return <div>app000010</div>;
}
export default app000010;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000011() {
return <div>app000011</div>;
}
export default app000011;

View File

@ -0,0 +1,9 @@
import React from 'react'
function app000012() {
return (
<div>app000012</div>
)
}
export default app000012

View File

@ -0,0 +1,7 @@
import React from "react";
function app000013() {
return <div>app000013</div>;
}
export default app000013;

View File

@ -0,0 +1,9 @@
import React from 'react'
function app000014() {
return (
<div>app000014</div>
)
}
export default app000014

View File

@ -0,0 +1,7 @@
import React from "react";
function app000015() {
return <div>app000015</div>;
}
export default app000015;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000016() {
return <div>app000016</div>;
}
export default app000016;

View File

@ -0,0 +1,7 @@
import React from "react";
function app000017() {
return <div>app000017</div>;
}
export default app000017;

View File

@ -0,0 +1,9 @@
import React from 'react'
function app000018() {
return (
<div>app000018</div>
)
}
export default app000018

View File

@ -0,0 +1,9 @@
import React from 'react'
function app000019() {
return (
<div>app000019</div>
)
}
export default app000019

View File

@ -1,11 +1,12 @@
import React from "react"; import React from "react";
import App000001 from "./app000001"; import App000001 from "./app000001";
import Page0001 from "./page0001"; import App000002 from "./app000002";
import { PageProps } from "./interFaces"; import { PageProps } from "./interFaces";
export const PageIndexs = { export const PageIndexs = {
"6015129b-f665-479c-a440-04fb82ea6114": Page0001,
app000001: App000001, app000001: App000001,
app000002: App000002,
}; };
function UnAuthorizedPage({ lang, queryParams }: PageProps) { function UnAuthorizedPage({ lang, queryParams }: PageProps) {

View File

@ -1,6 +1,6 @@
"use server"; "use server";
import React from "react"; import React from "react";
import { Home, User, Settings, Mail, Calendar } from "lucide-react"; import { Home } from "lucide-react";
import { transformMenu, LanguageTranslation } from "@/components/menu/runner"; import { transformMenu, LanguageTranslation } from "@/components/menu/runner";
import Link from "next/link"; import Link from "next/link";
@ -8,10 +8,12 @@ async function LeftMenu({
searchParams, searchParams,
pageUuidList, pageUuidList,
lang, lang,
pageSelected,
}: { }: {
pageUuidList: string[]; pageUuidList: string[];
lang: keyof LanguageTranslation; lang: keyof LanguageTranslation;
searchParams: { [key: string]: string | string[] | undefined }; searchParams: { [key: string]: string | string[] | undefined };
pageSelected: string;
}) { }) {
const transformedMenu = transformMenu(pageUuidList) || []; const transformedMenu = transformMenu(pageUuidList) || [];
@ -28,8 +30,6 @@ async function LeftMenu({
secondLayerIndex = indices[1] || 0; secondLayerIndex = indices[1] || 0;
} }
const pageSelected = searchParams?.page;
return ( return (
<div> <div>
<nav className="flex flex-col space-y-2"> <nav className="flex flex-col space-y-2">
@ -38,9 +38,7 @@ async function LeftMenu({
transformedMenu.map((item, firstIndex) => ( transformedMenu.map((item, firstIndex) => (
<div key={item.name} className="mb-4"> <div key={item.name} className="mb-4">
<Link <Link
href={`/dashboard?${ href={`${pageSelected}?menu=${firstIndex}*0`}
pageSelected ? `page=${pageSelected}` : ""
}&menu=${firstIndex}*0`}
className={`text-xl font-semibold pl-5 my-2 py-2 block ${ className={`text-xl font-semibold pl-5 my-2 py-2 block ${
firstIndex === firstLayerIndex firstIndex === firstLayerIndex
? "text-emerald-600" ? "text-emerald-600"
@ -56,9 +54,7 @@ async function LeftMenu({
{item.subList.map((subItem, secondIndex) => ( {item.subList.map((subItem, secondIndex) => (
<div key={subItem.name}> <div key={subItem.name}>
<Link <Link
href={`/dashboard?${ href={`${pageSelected}?menu=${firstIndex}*${secondIndex}`}
pageSelected ? `page=${pageSelected}` : ""
}&menu=${firstIndex}*${secondIndex}`}
className={`ml-5 my-4 pl-4 text-xl font-semibold block ${ className={`ml-5 my-4 pl-4 text-xl font-semibold block ${
secondIndex === secondLayerIndex secondIndex === secondLayerIndex
? "text-emerald-700" ? "text-emerald-700"
@ -71,24 +67,35 @@ async function LeftMenu({
{firstIndex === firstLayerIndex && {firstIndex === firstLayerIndex &&
secondIndex === secondLayerIndex && ( secondIndex === secondLayerIndex && (
<div className="ml-5"> <div className="ml-5">
{subItem.subList.map((subSubItem) => ( {subItem.subList.map((subSubItem) =>
<Link `${pageSelected}` !== subSubItem.siteUrl ? (
key={subSubItem.name} <Link
href={`/dashboard?page=${subSubItem.name}&menu=${firstIndex}*${secondIndex}`} key={subSubItem.name}
className={`flex flex-row text-xl py-4 my-4 w-full space-x-2 p-2 rounded ${ href={`${subSubItem?.siteUrl}?menu=${firstIndex}*${secondIndex}`}
pageSelected === subSubItem.name className={`flex flex-row text-xl py-4 my-4 w-full space-x-2 p-2 rounded hover:bg-gray-200`}
? " bg-gray-100 cursor-not-allowed" >
: "hover:bg-gray-200" <span className="text-gray-400">
}`} <Home />
> </span>
<span className="text-gray-400"> <span className="ml-5 text-gray-700">
<Home /> {subSubItem.lg[lang]}
</span> </span>
<span className="ml-5 text-gray-700"> </Link>
{subSubItem.lg[lang]} ) : (
</span> <a
</Link> key={subSubItem.name}
))} href={`${subSubItem?.siteUrl}?menu=${firstIndex}*${secondIndex}`}
className={`flex flex-row text-xl py-4 my-4 w-full space-x-2 p-2 rounded bg-gray-100 cursor-not-allowed"`}
>
<span className="text-gray-400">
<Home />
</span>
<span className="ml-5 text-gray-700">
{subSubItem.lg[lang]}
</span>
</a>
)
)}
</div> </div>
)} )}
</div> </div>

View File

@ -25,11 +25,20 @@ const Build = {
siteUrl: "/build", siteUrl: "/build",
}; };
const Dashboard = {
name: "Dashboard",
lg: {
tr: "Pano",
en: "Dashboard",
},
siteUrl: "/dashboard",
};
const BuildParts = { const BuildParts = {
name: "BuildParts", name: "BuildParts",
lg: { lg: {
tr: "Daire", tr: "Daireler",
en: "BuildParts", en: "Build Parts",
}, },
siteUrl: "/build/parts", siteUrl: "/build/parts",
}; };
@ -37,8 +46,8 @@ const BuildParts = {
const BuildArea = { const BuildArea = {
name: "BuildArea", name: "BuildArea",
lg: { lg: {
tr: "Daire", tr: "Daire Alanları",
en: "BuildArea", en: "Build Area",
}, },
siteUrl: "/build/area", siteUrl: "/build/area",
}; };
@ -94,7 +103,7 @@ const EmergencyMeeting = {
tr: "Acil Toplantı Tanımlama ve Davet", tr: "Acil Toplantı Tanımlama ve Davet",
en: "Emergency Meeting and Invitations", en: "Emergency Meeting and Invitations",
}, },
siteUrl: "emergency/meeting", siteUrl: "/emergency/meeting",
}; };
const EmergencyMeetingClose = { const EmergencyMeetingClose = {
@ -112,10 +121,27 @@ const MeetingParticipations = {
tr: "Toplantı Katılım İşlemleri", tr: "Toplantı Katılım İşlemleri",
en: "Meeting Participations", en: "Meeting Participations",
}, },
siteUrl: "/meeting/participations", siteUrl: "/meeting/participation",
}; };
const Menu = [ const Menu = [
{
name: "Dashboard",
lg: {
tr: "Pano",
en: "Dashboard",
},
subList: [
{
name: "Dashboard",
lg: {
tr: "Pano",
en: "Dashboard",
},
subList: [Dashboard],
},
],
},
{ {
name: "Definitions", name: "Definitions",
lg: { lg: {

View File

@ -91,19 +91,19 @@ services:
# - mongo_service # - mongo_service
# - redis_service # - redis_service
dealer_service: # dealer_service:
container_name: dealer_service # container_name: dealer_service
build: # build:
context: . # context: .
dockerfile: ApiServices/DealerService/Dockerfile # dockerfile: ApiServices/DealerService/Dockerfile
networks: # networks:
- wag-services # - wag-services
env_file: # env_file:
- api_env.env # - api_env.env
depends_on: # depends_on:
- postgres-service # - postgres-service
- mongo_service # - mongo_service
- redis_service # - redis_service
# template_service: # template_service:
# container_name: template_service # container_name: template_service

100
node_modules/.package-lock.json generated vendored Normal file
View File

@ -0,0 +1,100 @@
{
"name": "prod-wag-backend-automate-services",
"lockfileVersion": 3,
"requires": true,
"packages": {
"node_modules/@hookform/resolvers": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-5.0.1.tgz",
"integrity": "sha512-u/+Jp83luQNx9AdyW2fIPGY6Y7NG68eN2ZW8FOJYL+M0i4s49+refdJdOp/A9n9HFQtQs3HIDHQvX3ZET2o7YA==",
"dependencies": {
"@standard-schema/utils": "^0.3.0"
},
"peerDependencies": {
"react-hook-form": "^7.55.0"
}
},
"node_modules/@standard-schema/utils": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@standard-schema/utils/-/utils-0.3.0.tgz",
"integrity": "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g=="
},
"node_modules/flatpickr": {
"version": "4.6.13",
"resolved": "https://registry.npmjs.org/flatpickr/-/flatpickr-4.6.13.tgz",
"integrity": "sha512-97PMG/aywoYpB4IvbvUJi0RQi8vearvU0oov1WW3k0WZPBMrTQVqekSX5CjSG/M4Q3i6A/0FKXC7RyAoAUUSPw=="
},
"node_modules/lucide-react": {
"version": "0.487.0",
"resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.487.0.tgz",
"integrity": "sha512-aKqhOQ+YmFnwq8dWgGjOuLc8V1R9/c/yOd+zDY4+ohsR2Jo05lSGc3WsstYPIzcTpeosN7LoCkLReUUITvaIvw==",
"peerDependencies": {
"react": "^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0"
}
},
"node_modules/next-crypto": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/next-crypto/-/next-crypto-1.0.8.tgz",
"integrity": "sha512-6VcrH+xFuuCRGCdDMjFFibhJ97c4s+J/6SEV73RUYJhh38MDW4WXNZNTWIMZBq0B29LOIfAQ0XA37xGUZZCCjA=="
},
"node_modules/react": {
"version": "19.1.0",
"resolved": "https://registry.npmjs.org/react/-/react-19.1.0.tgz",
"integrity": "sha512-FS+XFBNvn3GTAWq26joslQgWNoFu08F4kl0J4CgdNKADkdSGXQyTCnKteIAJy96Br6YbpEU1LSzV5dYtjMkMDg==",
"peer": true,
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/react-dom": {
"version": "19.1.0",
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.1.0.tgz",
"integrity": "sha512-Xs1hdnE+DyKgeHJeJznQmYMIBG3TKIHJJT95Q58nHLSrElKlGQqDTR2HQ9fx5CN/Gk6Vh/kupBTDLU11/nDk/g==",
"peer": true,
"dependencies": {
"scheduler": "^0.26.0"
},
"peerDependencies": {
"react": "^19.1.0"
}
},
"node_modules/react-hook-form": {
"version": "7.55.0",
"resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.55.0.tgz",
"integrity": "sha512-XRnjsH3GVMQz1moZTW53MxfoWN7aDpUg/GpVNc4A3eXRVNdGXfbzJ4vM4aLQ8g6XCUh1nIbx70aaNCl7kxnjog==",
"engines": {
"node": ">=18.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/react-hook-form"
},
"peerDependencies": {
"react": "^16.8.0 || ^17 || ^18 || ^19"
}
},
"node_modules/scheduler": {
"version": "0.26.0",
"resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.26.0.tgz",
"integrity": "sha512-NlHwttCI/l5gCPR3D1nNXtWABUmBwvZpEQiD4IXSbIDq8BzLIK/7Ir5gTFSGZDUu37K5cMNp0hFtzO38sC7gWA==",
"peer": true
},
"node_modules/sonner": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/sonner/-/sonner-2.0.3.tgz",
"integrity": "sha512-njQ4Hht92m0sMqqHVDL32V2Oun9W1+PHO9NDv9FHfJjT3JT22IG4Jpo3FPQy+mouRKCXFWO+r67v6MrHX2zeIA==",
"peerDependencies": {
"react": "^18.0.0 || ^19.0.0 || ^19.0.0-rc",
"react-dom": "^18.0.0 || ^19.0.0 || ^19.0.0-rc"
}
},
"node_modules/zod": {
"version": "3.24.2",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.24.2.tgz",
"integrity": "sha512-lY7CDW43ECgW9u1TcT3IoXHflywfVqDYze4waEz812jR/bZ8FHDsl7pFQoSZTz5N+2NqRXs8GBwnAwo3ZNxqhQ==",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
}
}
}

21
node_modules/@hookform/resolvers/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019-present Beier(Bill) Luo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

926
node_modules/@hookform/resolvers/README.md generated vendored Normal file
View File

@ -0,0 +1,926 @@
<div align="center">
<p align="center">
<a href="https://react-hook-form.com" title="React Hook Form - Simple React forms validation">
<img src="https://raw.githubusercontent.com/bluebill1049/react-hook-form/master/docs/logo.png" alt="React Hook Form Logo - React hook custom hook for form validation" />
</a>
</p>
</div>
<p align="center">Performant, flexible and extensible forms with easy to use validation.</p>
<div align="center">
[![npm downloads](https://img.shields.io/npm/dm/@hookform/resolvers.svg?style=for-the-badge)](https://www.npmjs.com/package/@hookform/resolvers)
[![npm](https://img.shields.io/npm/dt/@hookform/resolvers.svg?style=for-the-badge)](https://www.npmjs.com/package/@hookform/resolvers)
[![npm](https://img.shields.io/bundlephobia/minzip/@hookform/resolvers?style=for-the-badge)](https://bundlephobia.com/result?p=@hookform/resolvers)
</div>
## React Hook Form Resolvers
This function allows you to use any external validation library such as Yup, Zod, Joi, Vest, Ajv and many others. The goal is to make sure you can seamlessly integrate whichever validation library you prefer. If you're not using a library, you can always write your own logic to validate your forms.
## Install
Install your preferred validation library alongside `@hookform/resolvers`.
npm install @hookform/resolvers # npm
yarn add @hookform/resolvers # yarn
pnpm install @hookform/resolvers # pnpm
bun install @hookform/resolvers # bun
<details>
<summary>Resolver Comparison</summary>
| resolver | Infer values <br /> from schema | [criteriaMode](https://react-hook-form.com/docs/useform#criteriaMode) |
|---|---|---|
| AJV | ❌ | `firstError | all` |
| Arktype | ✅ | `firstError` |
| class-validator | ✅ | `firstError | all` |
| computed-types | ✅ | `firstError` |
| Effect | ✅ | `firstError | all` |
| fluentvalidation-ts | ❌ | `firstError` |
| io-ts | ✅ | `firstError` |
| joi | ❌ | `firstError | all` |
| Nope | ❌ | `firstError` |
| Standard Schema | ✅ | `firstError | all` |
| Superstruct | ✅ | `firstError` |
| typanion | ✅ | `firstError` |
| typebox | ✅ | `firstError | all` |
| typeschema | ❌ | `firstError | all` |
| valibot | ✅ | `firstError | all` |
| vest | ❌ | `firstError | all` |
| vine | ✅ | `firstError | all` |
| yup | ✅ | `firstError | all` |
| zod | ✅ | `firstError | all` |
</details>
## TypeScript
Most of the resolvers can infer the output type from the schema. See comparison table for more details.
```tsx
useForm<Input, Context, Output>()
```
Example:
```tsx
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
const schema = z.object({
id: z.number(),
});
// Automatically infers the output type from the schema
useForm({
resolver: zodResolver(schema),
});
// Force the output type
useForm<z.input<typeof schema>, any, z.output<typeof schema>>({
resolver: zodResolver(schema),
});
```
## Links
- [React-hook-form validation resolver documentation ](https://react-hook-form.com/docs/useform#resolver)
### Supported resolvers
- [Install](#install)
- [Links](#links)
- [Supported resolvers](#supported-resolvers)
- [API](#api)
- [Quickstart](#quickstart)
- [Yup](#yup)
- [Zod](#zod)
- [Superstruct](#superstruct)
- [Joi](#joi)
- [Vest](#vest)
- [Class Validator](#class-validator)
- [io-ts](#io-ts)
- [Nope](#nope)
- [computed-types](#computed-types)
- [typanion](#typanion)
- [Ajv](#ajv)
- [TypeBox](#typebox)
- [With `ValueCheck`](#with-valuecheck)
- [With `TypeCompiler`](#with-typecompiler)
- [ArkType](#arktype)
- [Valibot](#valibot)
- [TypeSchema](#typeschema)
- [effect-ts](#effect-ts)
- [VineJS](#vinejs)
- [fluentvalidation-ts](#fluentvalidation-ts)
- [standard-schema](#standard-schema)
- [Backers](#backers)
- [Sponsors](#sponsors)
- [Contributors](#contributors)
## API
```
type Options = {
mode: 'async' | 'sync',
raw?: boolean
}
resolver(schema: object, schemaOptions?: object, resolverOptions: Options)
```
| | type | Required | Description |
| --------------- | -------- | -------- | --------------------------------------------- |
| schema | `object` | ✓ | validation schema |
| schemaOptions | `object` | | validation library schema options |
| resolverOptions | `object` | | resolver options, `async` is the default mode |
## Quickstart
### [Yup](https://github.com/jquense/yup)
Dead simple Object schema validation.
[![npm](https://img.shields.io/bundlephobia/minzip/yup?style=for-the-badge)](https://bundlephobia.com/result?p=yup)
```typescript jsx
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from 'yup';
const schema = yup
.object()
.shape({
name: yup.string().required(),
age: yup.number().required(),
})
.required();
const App = () => {
const { register, handleSubmit } = useForm({
resolver: yupResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
<input type="number" {...register('age')} />
<input type="submit" />
</form>
);
};
```
### [Zod](https://github.com/vriad/zod)
TypeScript-first schema validation with static type inference
[![npm](https://img.shields.io/bundlephobia/minzip/zod?style=for-the-badge)](https://bundlephobia.com/result?p=zod)
> ⚠️ Example below uses the `valueAsNumber`, which requires `react-hook-form` v6.12.0 (released Nov 28, 2020) or later.
```tsx
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
const schema = z.object({
name: z.string().min(1, { message: 'Required' }),
age: z.number().min(10),
});
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
{errors.name?.message && <p>{errors.name?.message}</p>}
<input type="number" {...register('age', { valueAsNumber: true })} />
{errors.age?.message && <p>{errors.age?.message}</p>}
<input type="submit" />
</form>
);
};
```
### [Superstruct](https://github.com/ianstormtaylor/superstruct)
A simple and composable way to validate data in JavaScript (or TypeScript).
[![npm](https://img.shields.io/bundlephobia/minzip/superstruct?style=for-the-badge)](https://bundlephobia.com/result?p=superstruct)
```typescript jsx
import { useForm } from 'react-hook-form';
import { superstructResolver } from '@hookform/resolvers/superstruct';
import { object, string, number } from 'superstruct';
const schema = object({
name: string(),
age: number(),
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: superstructResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
<input type="number" {...register('age', { valueAsNumber: true })} />
<input type="submit" />
</form>
);
};
```
### [Joi](https://github.com/sideway/joi)
The most powerful data validation library for JS.
[![npm](https://img.shields.io/bundlephobia/minzip/joi?style=for-the-badge)](https://bundlephobia.com/result?p=joi)
```typescript jsx
import { useForm } from 'react-hook-form';
import { joiResolver } from '@hookform/resolvers/joi';
import Joi from 'joi';
const schema = Joi.object({
name: Joi.string().required(),
age: Joi.number().required(),
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: joiResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
<input type="number" {...register('age')} />
<input type="submit" />
</form>
);
};
```
### [Vest](https://github.com/ealush/vest)
Vest 🦺 Declarative Validation Testing.
[![npm](https://img.shields.io/bundlephobia/minzip/vest?style=for-the-badge)](https://bundlephobia.com/result?p=vest)
```typescript jsx
import { useForm } from 'react-hook-form';
import { vestResolver } from '@hookform/resolvers/vest';
import { create, test, enforce } from 'vest';
const validationSuite = create((data = {}) => {
test('username', 'Username is required', () => {
enforce(data.username).isNotEmpty();
});
test('password', 'Password is required', () => {
enforce(data.password).isNotEmpty();
});
});
const App = () => {
const { register, handleSubmit, errors } = useForm({
resolver: vestResolver(validationSuite),
});
return (
<form onSubmit={handleSubmit((data) => console.log(data))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
### [Class Validator](https://github.com/typestack/class-validator)
Decorator-based property validation for classes.
[![npm](https://img.shields.io/bundlephobia/minzip/class-validator?style=for-the-badge)](https://bundlephobia.com/result?p=class-validator)
> ⚠️ Remember to add these options to your `tsconfig.json`!
```
"strictPropertyInitialization": false,
"experimentalDecorators": true
```
```tsx
import { useForm } from 'react-hook-form';
import { classValidatorResolver } from '@hookform/resolvers/class-validator';
import { Length, Min, IsEmail } from 'class-validator';
class User {
@Length(2, 30)
username: string;
@IsEmail()
email: string;
}
const resolver = classValidatorResolver(User);
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<User>({ resolver });
return (
<form onSubmit={handleSubmit((data) => console.log(data))}>
<input type="text" {...register('username')} />
{errors.username && <span>{errors.username.message}</span>}
<input type="text" {...register('email')} />
{errors.email && <span>{errors.email.message}</span>}
<input type="submit" value="Submit" />
</form>
);
};
```
### [io-ts](https://github.com/gcanti/io-ts)
Validate your data with powerful decoders.
[![npm](https://img.shields.io/bundlephobia/minzip/io-ts?style=for-the-badge)](https://bundlephobia.com/result?p=io-ts)
```typescript jsx
import React from 'react';
import { useForm } from 'react-hook-form';
import { ioTsResolver } from '@hookform/resolvers/io-ts';
import t from 'io-ts';
// you don't have to use io-ts-types, but it's very useful
import tt from 'io-ts-types';
const schema = t.type({
username: t.string,
age: tt.NumberFromString,
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: ioTsResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="number" {...register('age')} />
<input type="submit" />
</form>
);
};
export default App;
```
### [Nope](https://github.com/bvego/nope-validator)
A small, simple, and fast JS validator
[![npm](https://img.shields.io/bundlephobia/minzip/nope-validator?style=for-the-badge)](https://bundlephobia.com/result?p=nope-validator)
```typescript jsx
import { useForm } from 'react-hook-form';
import { nopeResolver } from '@hookform/resolvers/nope';
import Nope from 'nope-validator';
const schema = Nope.object().shape({
name: Nope.string().required(),
age: Nope.number().required(),
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: nopeResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
<input type="number" {...register('age')} />
<input type="submit" />
</form>
);
};
```
### [computed-types](https://github.com/neuledge/computed-types)
TypeScript-first schema validation with static type inference
[![npm](https://img.shields.io/bundlephobia/minzip/computed-types?style=for-the-badge)](https://bundlephobia.com/result?p=computed-types)
```tsx
import { useForm } from 'react-hook-form';
import { computedTypesResolver } from '@hookform/resolvers/computed-types';
import Schema, { number, string } from 'computed-types';
const schema = Schema({
username: string.min(1).error('username field is required'),
age: number,
});
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: computedTypesResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
{errors.name?.message && <p>{errors.name?.message}</p>}
<input type="number" {...register('age', { valueAsNumber: true })} />
{errors.age?.message && <p>{errors.age?.message}</p>}
<input type="submit" />
</form>
);
};
```
### [typanion](https://github.com/arcanis/typanion)
Static and runtime type assertion library with no dependencies
[![npm](https://img.shields.io/bundlephobia/minzip/typanion?style=for-the-badge)](https://bundlephobia.com/result?p=typanion)
```tsx
import { useForm } from 'react-hook-form';
import { typanionResolver } from '@hookform/resolvers/typanion';
import * as t from 'typanion';
const isUser = t.isObject({
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]),
age: t.applyCascade(t.isNumber(), [
t.isInteger(),
t.isInInclusiveRange(1, 100),
]),
});
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: typanionResolver(isUser),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
{errors.name?.message && <p>{errors.name?.message}</p>}
<input type="number" {...register('age')} />
{errors.age?.message && <p>{errors.age?.message}</p>}
<input type="submit" />
</form>
);
};
```
### [Ajv](https://github.com/ajv-validator/ajv)
The fastest JSON validator for Node.js and browser
[![npm](https://img.shields.io/bundlephobia/minzip/ajv?style=for-the-badge)](https://bundlephobia.com/result?p=ajv)
```tsx
import { useForm } from 'react-hook-form';
import { ajvResolver } from '@hookform/resolvers/ajv';
// must use `minLength: 1` to implement required field
const schema = {
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,
};
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: ajvResolver(schema),
});
return (
<form onSubmit={handleSubmit((data) => console.log(data))}>
<input {...register('username')} />
{errors.username && <span>{errors.username.message}</span>}
<input {...register('password')} />
{errors.password && <span>{errors.password.message}</span>}
<button type="submit">submit</button>
</form>
);
};
```
### [TypeBox](https://github.com/sinclairzx81/typebox)
JSON Schema Type Builder with Static Type Resolution for TypeScript
[![npm](https://img.shields.io/bundlephobia/minzip/@sinclair/typebox?style=for-the-badge)](https://bundlephobia.com/result?p=@sinclair/typebox)
#### With `ValueCheck`
```typescript jsx
import { useForm } from 'react-hook-form';
import { typeboxResolver } from '@hookform/resolvers/typebox';
import { Type } from '@sinclair/typebox';
const schema = Type.Object({
username: Type.String({ minLength: 1 }),
password: Type.String({ minLength: 1 }),
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: typeboxResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
#### With `TypeCompiler`
A high-performance JIT of `TypeBox`, [read more](https://github.com/sinclairzx81/typebox#typecompiler)
```typescript jsx
import { useForm } from 'react-hook-form';
import { typeboxResolver } from '@hookform/resolvers/typebox';
import { Type } from '@sinclair/typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';
const schema = Type.Object({
username: Type.String({ minLength: 1 }),
password: Type.String({ minLength: 1 }),
});
const typecheck = TypeCompiler.Compile(schema);
const App = () => {
const { register, handleSubmit } = useForm({
resolver: typeboxResolver(typecheck),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
### [ArkType](https://github.com/arktypeio/arktype)
TypeScript's 1:1 validator, optimized from editor to runtime
[![npm](https://img.shields.io/bundlephobia/minzip/arktype?style=for-the-badge)](https://bundlephobia.com/result?p=arktype)
```typescript jsx
import { useForm } from 'react-hook-form';
import { arktypeResolver } from '@hookform/resolvers/arktype';
import { type } from 'arktype';
const schema = type({
username: 'string>1',
password: 'string>1',
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: arktypeResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
### [Valibot](https://github.com/fabian-hiller/valibot)
The modular and type safe schema library for validating structural data
[![npm](https://img.shields.io/bundlephobia/minzip/valibot?style=for-the-badge)](https://bundlephobia.com/result?p=valibot)
```typescript jsx
import { useForm } from 'react-hook-form';
import { valibotResolver } from '@hookform/resolvers/valibot';
import * as v from 'valibot';
const schema = v.object({
username: v.pipe(
v.string('username is required'),
v.minLength(3, 'Needs to be at least 3 characters'),
v.endsWith('cool', 'Needs to end with `cool`'),
),
password: v.string('password is required'),
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: valibotResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
### [TypeSchema](https://typeschema.com)
Universal adapter for schema validation, compatible with [any validation library](https://typeschema.com/#coverage)
[![npm](https://img.shields.io/bundlephobia/minzip/@typeschema/main?style=for-the-badge)](https://bundlephobia.com/result?p=@typeschema/main)
```typescript jsx
import { useForm } from 'react-hook-form';
import { typeschemaResolver } from '@hookform/resolvers/typeschema';
import { z } from 'zod';
// Use your favorite validation library
const schema = z.object({
username: z.string().min(1, { message: 'Required' }),
password: z.number().min(1, { message: 'Required' }),
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: typeschemaResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
### [effect-ts](https://github.com/Effect-TS/effect)
A powerful TypeScript framework that provides a fully-fledged functional effect system with a rich standard library.
[![npm](https://img.shields.io/bundlephobia/minzip/effect?style=for-the-badge)](https://bundlephobia.com/result?p=effect)
```typescript jsx
import React from 'react';
import { useForm } from 'react-hook-form';
import { effectTsResolver } from '@hookform/resolvers/effect-ts';
import { Schema } from 'effect';
const schema = Schema.Struct({
username: Schema.String.pipe(
Schema.nonEmptyString({ message: () => 'username required' }),
),
password: Schema.String.pipe(
Schema.nonEmptyString({ message: () => 'password required' }),
),
});
type FormData = typeof schema.Type;
interface Props {
onSubmit: (data: FormData) => void;
}
function TestComponent({ onSubmit }: Props) {
const {
register,
handleSubmit,
formState: { errors },
// provide generic if TS has issues inferring types
} = useForm<FormData>({
resolver: effectTsResolver(schema),
});
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>
);
}
```
### [VineJS](https://github.com/vinejs/vine)
VineJS is a form data validation library for Node.js
[![npm](https://img.shields.io/bundlephobia/minzip/@vinejs/vine?style=for-the-badge)](https://bundlephobia.com/result?p=@vinejs/vine)
```typescript jsx
import { useForm } from 'react-hook-form';
import { vineResolver } from '@hookform/resolvers/vine';
import vine from '@vinejs/vine';
const schema = vine.compile(
vine.object({
username: vine.string().minLength(1),
password: vine.string().minLength(1),
}),
);
const App = () => {
const { register, handleSubmit } = useForm({
resolver: vineResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<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>
);
};
```
### [fluentvalidation-ts](https://github.com/AlexJPotter/fluentvalidation-ts)
A TypeScript-first library for building strongly-typed validation rules
[![npm](https://img.shields.io/bundlephobia/minzip/@vinejs/vine?style=for-the-badge)](https://bundlephobia.com/result?p=@vinejs/vine)
```typescript jsx
import { useForm } from 'react-hook-form';
import { fluentValidationResolver } from '@hookform/resolvers/fluentvalidation-ts';
import { Validator } from 'fluentvalidation-ts';
class FormDataValidator extends Validator<FormData> {
constructor() {
super();
this.ruleFor('username')
.notEmpty()
.withMessage('username is a required field');
this.ruleFor('password')
.notEmpty()
.withMessage('password is a required field');
}
}
const App = () => {
const { register, handleSubmit } = useForm({
resolver: fluentValidationResolver(new FormDataValidator()),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<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>
);
};
```
### [standard-schema](https://github.com/standard-schema/standard-schema)
A standard interface for TypeScript schema validation libraries
[![npm](https://img.shields.io/bundlephobia/minzip/@standard-schema/spec?style=for-the-badge)](https://bundlephobia.com/result?p=@standard-schema/spec)
Example zod
```typescript jsx
import { useForm } from 'react-hook-form';
import { standardSchemaResolver } from '@hookform/resolvers/standard-schema';
import { z } from 'zod';
const schema = z.object({
name: z.string().min(1, { message: 'Required' }),
age: z.number().min(10),
});
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: standardSchemaResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('name')} />
{errors.name?.message && <p>{errors.name?.message}</p>}
<input type="number" {...register('age', { valueAsNumber: true })} />
{errors.age?.message && <p>{errors.age?.message}</p>}
<input type="submit" />
</form>
);
};
```
Example arkType
```typescript jsx
import { useForm } from 'react-hook-form';
import { standardSchemaResolver } from '@hookform/resolvers/standard-schema';
import { type } from 'arktype';
const schema = type({
username: 'string>1',
password: 'string>1',
});
const App = () => {
const { register, handleSubmit } = useForm({
resolver: standardSchemaResolver(schema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
<input {...register('username')} />
<input type="password" {...register('password')} />
<input type="submit" />
</form>
);
};
```
## Backers
Thanks go to all our backers! [[Become a backer](https://opencollective.com/react-hook-form#backer)].
<a href="https://opencollective.com/react-hook-form#backers">
<img src="https://opencollective.com/react-hook-form/backers.svg?width=950" />
</a>
## Contributors
Thanks go to these wonderful people! [[Become a contributor](CONTRIBUTING.md)].
<a href="https://github.com/react-hook-form/react-hook-form/graphs/contributors">
<img src="https://opencollective.com/react-hook-form/contributors.svg?width=950" />
</a>

19
node_modules/@hookform/resolvers/ajv/package.json generated vendored Normal file
View 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"
}
}

View 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('');
});

View 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();
});

View 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',
},
};

View 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',
},
};

View 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": {},
}
`;

View 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": {},
}
`;

View 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();
});
});

View 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
View 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
View File

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

20
node_modules/@hookform/resolvers/ajv/src/types.ts generated vendored Normal file
View 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;

18
node_modules/@hookform/resolvers/arktype/package.json generated vendored Normal file
View File

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

View File

@ -0,0 +1,82 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { type } from 'arktype';
import React from 'react';
import { useForm } from 'react-hook-form';
import { arktypeResolver } from '..';
const schema = type({
username: 'string>1',
password: 'string>1',
});
type FormData = typeof schema.infer;
interface Props {
onSubmit: (data: FormData) => void;
}
function TestComponent({ onSubmit }: Props) {
const { register, handleSubmit } = useForm<FormData>({
resolver: arktypeResolver(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 Arktype", 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 must be at least length 2',
);
// password
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
expect(passwordField.validity.valid).toBe(false);
expect(passwordField.validationMessage).toBe(
'password must be at least length 2',
);
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,54 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { type } from 'arktype';
import React from 'react';
import { useForm } from 'react-hook-form';
import { arktypeResolver } from '..';
const schema = type({
username: 'string>1',
password: 'string>1',
});
function TestComponent({
onSubmit,
}: {
onSubmit: (data: typeof schema.infer) => void;
}) {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: arktypeResolver(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 arkType 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 must be at least length 2'),
).toBeInTheDocument();
expect(
screen.getByText('password must be at least length 2'),
).toBeInTheDocument();
expect(handleSubmit).not.toHaveBeenCalled();
});

View File

@ -0,0 +1,65 @@
import { type } from 'arktype';
import { Field, InternalFieldName } from 'react-hook-form';
export const schema = type({
username: 'string>2',
password: '/.*[A-Za-z].*/>8|/.*\\d.*/',
repeatPassword: 'string>1',
accessToken: 'string|number',
birthYear: '1900<number<2013',
email: 'string.email',
tags: 'string[]',
enabled: 'boolean',
url: 'string>1',
'like?': type({
id: 'number',
name: 'string>3',
}).array(),
dateStr: 'Date',
});
export const validData: typeof schema.infer = {
username: 'Doe',
password: 'Password123_',
repeatPassword: 'Password123_',
birthYear: 2000,
email: 'john@doe.com',
tags: ['tag1', 'tag2'],
enabled: true,
accessToken: 'accessToken',
url: 'https://react-hook-form.com/',
like: [
{
id: 1,
name: 'name',
},
],
dateStr: new Date('2020-01-01'),
};
export const invalidData = {
password: '___',
email: '',
birthYear: 'birthYear',
like: [{ id: 'z' }],
url: 'abc',
} as any as typeof schema.infer;
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,74 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`arktypeResolver > should return a single error from arktypeResolver when validation fails 1`] = `
{
"errors": {
"accessToken": {
"message": "accessToken must be a number or a string (was missing)",
"ref": undefined,
"type": "required",
},
"birthYear": {
"message": "birthYear must be a number (was a string)",
"ref": undefined,
"type": "domain",
},
"dateStr": {
"message": "dateStr must be a Date (was missing)",
"ref": undefined,
"type": "required",
},
"email": {
"message": "email must be an email address (was "")",
"ref": {
"name": "email",
},
"type": "pattern",
},
"enabled": {
"message": "enabled must be boolean (was missing)",
"ref": undefined,
"type": "required",
},
"like": [
{
"id": {
"message": "like[0].id must be a number (was a string)",
"ref": undefined,
"type": "domain",
},
"name": {
"message": "like[0].name must be a string (was missing)",
"ref": undefined,
"type": "required",
},
},
],
"password": {
"message": "password must be matched by .*[A-Za-z].* or matched by .*\\d.* (was "___")",
"ref": {
"name": "password",
},
"type": "union",
},
"repeatPassword": {
"message": "repeatPassword must be a string (was missing)",
"ref": undefined,
"type": "required",
},
"tags": {
"message": "tags must be an array (was missing)",
"ref": undefined,
"type": "required",
},
"username": {
"message": "username must be a string (was missing)",
"ref": {
"name": "username",
},
"type": "required",
},
},
"values": {},
}
`;

View File

@ -0,0 +1,82 @@
import { type } from 'arktype';
import { Resolver, useForm } from 'react-hook-form';
import { SubmitHandler } from 'react-hook-form';
import { arktypeResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
const shouldUseNativeValidation = false;
describe('arktypeResolver', () => {
it('should return values from arktypeResolver when validation pass & raw=true', async () => {
const result = await arktypeResolver(schema, undefined, {
raw: true,
})(validData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return a single error from arktypeResolver when validation fails', async () => {
const result = await arktypeResolver(schema)(invalidData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toMatchSnapshot();
});
/**
* Type inference tests
*/
it('should correctly infer the output type from a arktype schema', () => {
const resolver = arktypeResolver(type({ id: 'number' }));
expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: number }, unknown, { id: number }>
>();
});
it('should correctly infer the output type from a arktype schema using a transform', () => {
const resolver = arktypeResolver(
type({ id: type('string').pipe((s) => Number.parseInt(s)) }),
);
expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: string }, unknown, { id: number }>
>();
});
it('should correctly infer the output type from a arktype schema for the handleSubmit function in useForm', () => {
const schema = type({ id: 'number' });
const form = useForm({
resolver: arktypeResolver(schema),
});
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
SubmitHandler<{
id: number;
}>
>();
});
it('should correctly infer the output type from a arktype schema with a transform for the handleSubmit function in useForm', () => {
const schema = type({ id: type('string').pipe((s) => Number.parseInt(s)) });
const form = useForm({
resolver: arktypeResolver(schema),
});
expectTypeOf(form.watch('id')).toEqualTypeOf<string>();
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
SubmitHandler<{
id: number;
}>
>();
});
});

View File

@ -0,0 +1,98 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import { StandardSchemaV1 } from '@standard-schema/spec';
import { getDotPath } from '@standard-schema/utils';
import { FieldError, FieldValues, Resolver } from 'react-hook-form';
function parseErrorSchema(
issues: readonly StandardSchemaV1.Issue[],
validateAllFieldCriteria: boolean,
) {
const errors: Record<string, FieldError> = {};
for (let i = 0; i < issues.length; i++) {
const error = issues[i];
const path = getDotPath(error);
if (path) {
if (!errors[path]) {
errors[path] = { message: error.message, type: '' };
}
if (validateAllFieldCriteria) {
const types = errors[path].types || {};
errors[path].types = {
...types,
[Object.keys(types).length]: error.message,
};
}
}
}
return errors;
}
export function arktypeResolver<Input extends FieldValues, Context, Output>(
schema: StandardSchemaV1<Input, Output>,
_schemaOptions?: never,
resolverOptions?: {
raw?: false;
},
): Resolver<Input, Context, Output>;
export function arktypeResolver<Input extends FieldValues, Context, Output>(
schema: StandardSchemaV1<Input, Output>,
_schemaOptions: never | undefined,
resolverOptions: {
raw: true;
},
): Resolver<Input, Context, Input>;
/**
* Creates a resolver for react-hook-form using Arktype schema validation
* @param {Schema} schema - The Arktype schema to validate against
* @param {Object} resolverOptions - Additional resolver configuration
* @param {string} [resolverOptions.mode='raw'] - Return the raw input values rather than the parsed values
* @returns {Resolver<Schema['inferOut']>} A resolver function compatible with react-hook-form
* @example
* const schema = type({
* username: 'string>2'
* });
*
* useForm({
* resolver: arktypeResolver(schema)
* });
*/
export function arktypeResolver<Input extends FieldValues, Context, Output>(
schema: StandardSchemaV1<Input, Output>,
_schemaOptions?: never,
resolverOptions: {
raw?: boolean;
} = {},
): Resolver<Input, Context, Input | Output> {
return async (values: Input, _, options) => {
let result = schema['~standard'].validate(values);
if (result instanceof Promise) {
result = await result;
}
if (result.issues) {
const errors = parseErrorSchema(
result.issues,
!options.shouldUseNativeValidation && options.criteriaMode === 'all',
);
return {
values: {},
errors: toNestErrors(errors, options),
};
}
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return {
values: resolverOptions.raw ? Object.assign({}, values) : result.value,
errors: {},
};
};
}

View File

@ -0,0 +1 @@
export * from './arktype';

View File

@ -0,0 +1,19 @@
{
"name": "@hookform/resolvers/class-validator",
"amdName": "hookformResolversClassValidator",
"version": "1.0.0",
"private": true,
"description": "React Hook Form validation resolver: class-validator",
"main": "dist/class-validator.js",
"module": "dist/class-validator.module.js",
"umd:main": "dist/class-validator.umd.js",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"license": "MIT",
"peerDependencies": {
"react-hook-form": ">=6.6.0",
"@hookform/resolvers": ">=2.0.0",
"class-transformer": "^0.4.0",
"class-validator": "^0.12.0"
}
}

View File

@ -0,0 +1,79 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { IsNotEmpty } from 'class-validator';
import React from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { classValidatorResolver } from '..';
class Schema {
@IsNotEmpty()
username: string;
@IsNotEmpty()
password: string;
}
interface Props {
onSubmit: SubmitHandler<Schema>;
}
function TestComponent({ onSubmit }: Props) {
const { register, handleSubmit } = useForm<Schema>({
resolver: classValidatorResolver(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 Class Validator", 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 should not be empty');
// password
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement;
expect(passwordField.validity.valid).toBe(false);
expect(passwordField.validationMessage).toBe('password should not be empty');
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,53 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { IsNotEmpty } from 'class-validator';
import React from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { classValidatorResolver } from '..';
class Schema {
@IsNotEmpty()
username: string;
@IsNotEmpty()
password: string;
}
interface Props {
onSubmit: SubmitHandler<Schema>;
}
function TestComponent({ onSubmit }: Props) {
const {
register,
formState: { errors },
handleSubmit,
} = useForm<Schema>({
resolver: classValidatorResolver(Schema),
});
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 Class Validator 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 should not be empty/i)).toBeInTheDocument();
expect(screen.getByText(/password should not be empty/i)).toBeInTheDocument();
expect(handleSubmit).not.toHaveBeenCalled();
});

View File

@ -0,0 +1,88 @@
import 'reflect-metadata';
import { Type } from 'class-transformer';
import {
IsEmail,
IsNotEmpty,
Length,
Matches,
Max,
Min,
ValidateNested,
} from 'class-validator';
import { Field, InternalFieldName } from 'react-hook-form';
class Like {
@IsNotEmpty()
id: number;
@Length(4)
name: string;
}
export class Schema {
@Matches(/^\w+$/)
@Length(3, 30)
username: string;
@Matches(/^[a-zA-Z0-9]{3,30}/)
password: string;
@Min(1900)
@Max(2013)
birthYear: number;
@IsEmail()
email: string;
accessToken: string;
tags: string[];
enabled: boolean;
@ValidateNested({ each: true })
@Type(() => Like)
like: Like[];
}
export const validData: Schema = {
username: 'Doe',
password: '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' }],
} as any as Schema;
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,242 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`classValidatorResolver > should return a single error from classValidatorResolver when validation fails 1`] = `
{
"errors": {
"birthYear": {
"message": "birthYear must not be greater than 2013",
"ref": undefined,
"type": "max",
},
"email": {
"message": "email must be an email",
"ref": {
"name": "email",
},
"type": "isEmail",
},
"like": [
{
"name": {
"message": "name must be longer than or equal to 4 characters",
"ref": undefined,
"type": "isLength",
},
},
],
"password": {
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
"ref": {
"name": "password",
},
"type": "matches",
},
"username": {
"message": "username must be longer than or equal to 3 characters",
"ref": {
"name": "username",
},
"type": "isLength",
},
},
"values": {},
}
`;
exports[`classValidatorResolver > should return a single error from classValidatorResolver with \`mode: sync\` when validation fails 1`] = `
{
"errors": {
"birthYear": {
"message": "birthYear must not be greater than 2013",
"ref": undefined,
"type": "max",
},
"email": {
"message": "email must be an email",
"ref": {
"name": "email",
},
"type": "isEmail",
},
"like": [
{
"name": {
"message": "name must be longer than or equal to 4 characters",
"ref": undefined,
"type": "isLength",
},
},
],
"password": {
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
"ref": {
"name": "password",
},
"type": "matches",
},
"username": {
"message": "username must be longer than or equal to 3 characters",
"ref": {
"name": "username",
},
"type": "isLength",
},
},
"values": {},
}
`;
exports[`classValidatorResolver > should return all the errors from classValidatorResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
{
"errors": {
"birthYear": {
"message": "birthYear must not be greater than 2013",
"ref": undefined,
"type": "max",
"types": {
"max": "birthYear must not be greater than 2013",
"min": "birthYear must not be less than 1900",
},
},
"email": {
"message": "email must be an email",
"ref": {
"name": "email",
},
"type": "isEmail",
"types": {
"isEmail": "email must be an email",
},
},
"like": [
{
"name": {
"message": "name must be longer than or equal to 4 characters",
"ref": undefined,
"type": "isLength",
"types": {
"isLength": "name must be longer than or equal to 4 characters",
},
},
},
],
"password": {
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
"ref": {
"name": "password",
},
"type": "matches",
"types": {
"matches": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
},
},
"username": {
"message": "username must be longer than or equal to 3 characters",
"ref": {
"name": "username",
},
"type": "isLength",
"types": {
"isLength": "username must be longer than or equal to 3 characters",
"matches": "username must match /^\\w+$/ regular expression",
},
},
},
"values": {},
}
`;
exports[`classValidatorResolver > should return all the errors from classValidatorResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
{
"errors": {
"birthYear": {
"message": "birthYear must not be greater than 2013",
"ref": undefined,
"type": "max",
"types": {
"max": "birthYear must not be greater than 2013",
"min": "birthYear must not be less than 1900",
},
},
"email": {
"message": "email must be an email",
"ref": {
"name": "email",
},
"type": "isEmail",
"types": {
"isEmail": "email must be an email",
},
},
"like": [
{
"name": {
"message": "name must be longer than or equal to 4 characters",
"ref": undefined,
"type": "isLength",
"types": {
"isLength": "name must be longer than or equal to 4 characters",
},
},
},
],
"password": {
"message": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
"ref": {
"name": "password",
},
"type": "matches",
"types": {
"matches": "password must match /^[a-zA-Z0-9]{3,30}/ regular expression",
},
},
"username": {
"message": "username must be longer than or equal to 3 characters",
"ref": {
"name": "username",
},
"type": "isLength",
"types": {
"isLength": "username must be longer than or equal to 3 characters",
"matches": "username must match /^\\w+$/ regular expression",
},
},
},
"values": {},
}
`;
exports[`validate data with transformer option 1`] = `
{
"errors": {
"random": {
"message": "All fields must be defined.",
"ref": undefined,
"type": "isDefined",
"types": {
"isDefined": "All fields must be defined.",
"isNumber": "Must be a number",
"max": "Cannot be greater than 255",
"min": "Cannot be lower than 0",
},
},
},
"values": {},
}
`;
exports[`validate data with validator option 1`] = `
{
"errors": {
"random": {
"message": "All fields must be defined.",
"ref": undefined,
"type": "isDefined",
"types": {
"isDefined": "All fields must be defined.",
},
},
},
"values": {},
}
`;

View File

@ -0,0 +1,199 @@
import { Expose, Type } from 'class-transformer';
import * as classValidator from 'class-validator';
import { IsDefined, IsNumber, Max, Min } from 'class-validator';
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
import { classValidatorResolver } from '..';
import { Schema, fields, invalidData, validData } from './__fixtures__/data';
const shouldUseNativeValidation = false;
describe('classValidatorResolver', () => {
it('should return values from classValidatorResolver when validation pass', async () => {
const schemaSpy = vi.spyOn(classValidator, 'validate');
const schemaSyncSpy = vi.spyOn(classValidator, 'validateSync');
const result = await classValidatorResolver(Schema)(validData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(schemaSpy).toHaveBeenCalledTimes(1);
expect(schemaSyncSpy).not.toHaveBeenCalled();
expect(result).toEqual({ errors: {}, values: validData });
expect(result.values).toBeInstanceOf(Schema);
});
it('should return values as a raw object from classValidatorResolver when `rawValues` set to true', async () => {
const result = await classValidatorResolver(Schema, undefined, {
raw: true,
})(validData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toEqual({ errors: {}, values: validData });
expect(result.values).not.toBeInstanceOf(Schema);
});
it('should return values from classValidatorResolver with `mode: sync` when validation pass', async () => {
const validateSyncSpy = vi.spyOn(classValidator, 'validateSync');
const validateSpy = vi.spyOn(classValidator, 'validate');
const result = await classValidatorResolver(Schema, undefined, {
mode: 'sync',
})(validData, undefined, { fields, shouldUseNativeValidation });
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
expect(validateSpy).not.toHaveBeenCalled();
expect(result).toEqual({ errors: {}, values: validData });
expect(result.values).toBeInstanceOf(Schema);
});
it('should return a single error from classValidatorResolver when validation fails', async () => {
const result = await classValidatorResolver(Schema)(
invalidData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return a single error from classValidatorResolver with `mode: sync` when validation fails', async () => {
const validateSyncSpy = vi.spyOn(classValidator, 'validateSync');
const validateSpy = vi.spyOn(classValidator, 'validate');
const result = await classValidatorResolver(Schema, undefined, {
mode: 'sync',
})(invalidData, undefined, { fields, shouldUseNativeValidation });
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
expect(validateSpy).not.toHaveBeenCalled();
expect(result).toMatchSnapshot();
});
it('should return all the errors from classValidatorResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
const result = await classValidatorResolver(Schema)(
invalidData,
undefined,
{
fields,
criteriaMode: 'all',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return all the errors from classValidatorResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
const result = await classValidatorResolver(Schema, undefined, {
mode: 'sync',
})(invalidData, undefined, {
fields,
criteriaMode: 'all',
shouldUseNativeValidation,
});
expect(result).toMatchSnapshot();
});
});
it('validate data with transformer option', async () => {
class SchemaTest {
@Expose({ groups: ['find', 'create', 'update'] })
@Type(() => Number)
@IsDefined({
message: `All fields must be defined.`,
groups: ['publish'],
})
@IsNumber({}, { message: `Must be a number`, always: true })
@Min(0, { message: `Cannot be lower than 0`, always: true })
@Max(255, { message: `Cannot be greater than 255`, always: true })
random: number;
}
const result = await classValidatorResolver(
SchemaTest,
{ transformer: { groups: ['update'] } },
{
mode: 'sync',
},
)(
// @ts-expect-error - Just for testing purposes
invalidData,
undefined,
{
fields,
criteriaMode: 'all',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('validate data with validator option', async () => {
class SchemaTest {
@Expose({ groups: ['find', 'create', 'update'] })
@Type(() => Number)
@IsDefined({
message: `All fields must be defined.`,
groups: ['publish'],
})
@IsNumber({}, { message: `Must be a number`, always: true })
@Min(0, { message: `Cannot be lower than 0`, always: true })
@Max(255, { message: `Cannot be greater than 255`, always: true })
random: number;
}
const result = await classValidatorResolver(
SchemaTest,
{ validator: { stopAtFirstError: true } },
{
mode: 'sync',
},
)(
// @ts-expect-error - Just for testing purposes
invalidData,
undefined,
{
fields,
criteriaMode: 'all',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return from classValidatorResolver with `excludeExtraneousValues` set to true', async () => {
class SchemaTest {
@Expose()
@IsNumber({}, { message: `Must be a number`, always: true })
random: number;
}
const result = await classValidatorResolver(SchemaTest, {
transformer: {
excludeExtraneousValues: true,
},
})(
{
random: 10,
// @ts-expect-error - Just for testing purposes
extraneousField: true,
},
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(result).toEqual({ errors: {}, values: { random: 10 } });
expect(result.values).toBeInstanceOf(SchemaTest);
});

View File

@ -0,0 +1,101 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import {
ClassConstructor,
ClassTransformOptions,
plainToClass,
} from 'class-transformer';
import {
ValidationError,
ValidatorOptions,
validate,
validateSync,
} from 'class-validator';
import { FieldErrors, Resolver } from 'react-hook-form';
function parseErrorSchema(
errors: ValidationError[],
validateAllFieldCriteria: boolean,
parsedErrors: FieldErrors = {},
path = '',
) {
return errors.reduce((acc, error) => {
const _path = path ? `${path}.${error.property}` : error.property;
if (error.constraints) {
const key = Object.keys(error.constraints)[0];
acc[_path] = {
type: key,
message: error.constraints[key],
};
const _e = acc[_path];
if (validateAllFieldCriteria && _e) {
Object.assign(_e, { types: error.constraints });
}
}
if (error.children && error.children.length) {
parseErrorSchema(error.children, validateAllFieldCriteria, acc, _path);
}
return acc;
}, parsedErrors);
}
/**
* Creates a resolver for react-hook-form using class-validator schema validation
* @param {ClassConstructor<Schema>} schema - The class-validator 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
* class Schema {
* @Matches(/^\w+$/)
* @Length(3, 30)
* username: string;
* age: number
* }
*
* useForm({
* resolver: classValidatorResolver(Schema)
* });
*/
export function classValidatorResolver<Schema extends Record<string, any>>(
schema: ClassConstructor<Schema>,
schemaOptions: {
validator?: ValidatorOptions;
transformer?: ClassTransformOptions;
} = {},
resolverOptions: { mode?: 'async' | 'sync'; raw?: boolean } = {},
): Resolver<Schema> {
return async (values, _, options) => {
const { transformer, validator } = schemaOptions;
const data = plainToClass(schema, values, transformer);
const rawErrors = await (resolverOptions.mode === 'sync'
? validateSync
: validate)(data, validator);
if (rawErrors.length) {
return {
values: {},
errors: toNestErrors(
parseErrorSchema(
rawErrors,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
),
options,
),
};
}
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return {
values: resolverOptions.raw ? Object.assign({}, values) : data,
errors: {},
};
};
}

View File

@ -0,0 +1 @@
export * from './class-validator';

View File

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

View File

@ -0,0 +1,79 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import Schema, { Type, string } from 'computed-types';
import React from 'react';
import { useForm } from 'react-hook-form';
import { computedTypesResolver } from '..';
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
const schema = Schema({
username: string.min(2).error(USERNAME_REQUIRED_MESSAGE),
password: string.min(2).error(PASSWORD_REQUIRED_MESSAGE),
});
interface Props {
onSubmit: (data: Type<typeof schema>) => void;
}
function TestComponent({ onSubmit }: Props) {
const { register, handleSubmit } = useForm({
resolver: computedTypesResolver(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 computed-types", 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('');
});

View File

@ -0,0 +1,57 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import Schema, { Type, string } from 'computed-types';
import React from 'react';
import { useForm } from 'react-hook-form';
import { computedTypesResolver } from '..';
const schema = Schema({
username: string.min(2).error('username field is required'),
password: string.min(2).error('password field is required'),
address: Schema({
zipCode: string.min(5).max(5).error('zipCode field is required'),
}),
});
function TestComponent({
onSubmit,
}: { onSubmit: (data: Type<typeof schema>) => void }) {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: computedTypesResolver(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>}
<input {...register('address.zipCode')} />
{errors.address?.zipCode && (
<span role="alert">{errors.address.zipCode.message}</span>
)}
<button type="submit">submit</button>
</form>
);
}
test("form's validation with computed-types 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(screen.getByText(/zipCode field is required/i)).toBeInTheDocument();
expect(handleSubmit).not.toHaveBeenCalled();
});

View File

@ -0,0 +1,86 @@
import Schema, { Type, string, number, array, boolean } from 'computed-types';
import { Field, InternalFieldName } from 'react-hook-form';
export const schema = Schema({
username: string.regexp(/^\w+$/).min(3).max(30),
password: string
.regexp(new RegExp('.*[A-Z].*'), 'One uppercase character')
.regexp(new RegExp('.*[a-z].*'), 'One lowercase character')
.regexp(new RegExp('.*\\d.*'), 'One number')
.regexp(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
'One special character',
)
.min(8, 'Must be at least 8 characters in length'),
repeatPassword: string,
accessToken: Schema.either(string, number).optional(),
birthYear: number.min(1900).max(2013).optional(),
email: string
.regexp(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/)
.error('Incorrect email'),
tags: array.of(string),
enabled: boolean,
like: array
.of({
id: number,
name: string.min(4).max(4),
})
.optional(),
address: Schema({
city: string.min(3, 'Is required'),
zipCode: string
.min(5, 'Must be 5 characters long')
.max(5, 'Must be 5 characters long'),
}),
});
export const validData: Type<typeof schema> = {
username: 'Doe',
password: 'Password123_',
repeatPassword: 'Password123_',
accessToken: 'accessToken',
birthYear: 2000,
email: 'john@doe.com',
tags: ['tag1', 'tag2'],
enabled: true,
like: [
{
id: 1,
name: 'name',
},
],
address: {
city: 'Awesome city',
zipCode: '12345',
},
};
export const invalidData = {
password: '___',
email: '',
birthYear: 'birthYear',
like: [{ id: 'z' }],
address: {
city: '',
zipCode: '123',
},
} as any as Type<typeof schema>;
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,74 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`computedTypesResolver > should return a single error from computedTypesResolver when validation fails 1`] = `
{
"errors": {
"address": {
"city": {
"message": "Is required",
"ref": undefined,
"type": "ValidationError",
},
"zipCode": {
"message": "Must be 5 characters long",
"ref": undefined,
"type": "ValidationError",
},
},
"birthYear": {
"message": "Expect value to be "number"",
"ref": undefined,
"type": "ValidationError",
},
"email": {
"message": "Incorrect email",
"ref": {
"name": "email",
},
"type": "ValidationError",
},
"enabled": {
"message": "Expect value to be "boolean"",
"ref": undefined,
"type": "ValidationError",
},
"like": {
"id": {
"message": "Expect value to be "number"",
"ref": undefined,
"type": "ValidationError",
},
"name": {
"message": "Expect value to be "string"",
"ref": undefined,
"type": "ValidationError",
},
},
"password": {
"message": "One uppercase character",
"ref": {
"name": "password",
},
"type": "ValidationError",
},
"repeatPassword": {
"message": "Expect value to be "string"",
"ref": undefined,
"type": "ValidationError",
},
"tags": {
"message": "Expecting value to be an array",
"ref": undefined,
"type": "ValidationError",
},
"username": {
"message": "Expect value to be "string"",
"ref": {
"name": "username",
},
"type": "ValidationError",
},
},
"values": {},
}
`;

View File

@ -0,0 +1,96 @@
import Schema, { number } from 'computed-types';
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
import { computedTypesResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
const shouldUseNativeValidation = false;
describe('computedTypesResolver', () => {
it('should return values from computedTypesResolver when validation pass', async () => {
const result = await computedTypesResolver(schema)(validData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return a single error from computedTypesResolver when validation fails', async () => {
const result = await computedTypesResolver(schema)(invalidData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toMatchSnapshot();
});
it('should throw any error unrelated to computed-types', async () => {
const schemaWithCustomError = schema.transform(() => {
throw Error('custom error');
});
const promise = computedTypesResolver(schemaWithCustomError)(
validData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
await expect(promise).rejects.toThrow('custom error');
});
/**
* Type inference tests
*/
it('should correctly infer the output type from a computedTypes schema', () => {
const resolver = computedTypesResolver(Schema({ id: number }));
expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: number }, unknown, { id: number }>
>();
});
it('should correctly infer the output type from a computedTypes schema using a transform', () => {
const resolver = computedTypesResolver(
Schema({ id: number.transform((val) => String(val)) }),
);
expectTypeOf(resolver).toEqualTypeOf<
Resolver<{ id: number }, unknown, { id: string }>
>();
});
it('should correctly infer the output type from a computedTypes schema for the handleSubmit function in useForm', () => {
const schema = Schema({ id: number });
const form = useForm({
resolver: computedTypesResolver(schema),
});
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
SubmitHandler<{
id: number;
}>
>();
});
it('should correctly infer the output type from a computedTypes schema with a transform for the handleSubmit function in useForm', () => {
const schema = Schema({ id: number.transform((val) => String(val)) });
const form = useForm({
resolver: computedTypesResolver(schema),
});
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
SubmitHandler<{
id: string;
}>
>();
});
});

View File

@ -0,0 +1,61 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import { ValidationError } from 'computed-types';
import FunctionType from 'computed-types/lib/schema/FunctionType';
import type { FieldErrors, FieldValues, Resolver } from 'react-hook-form';
const isValidationError = (error: any): error is ValidationError =>
error.errors != null;
function parseErrorSchema(computedTypesError: ValidationError) {
const parsedErrors: FieldErrors = {};
return (computedTypesError.errors || []).reduce((acc, error) => {
acc[error.path.join('.')] = {
type: error.error.name,
message: error.error.message,
};
return acc;
}, parsedErrors);
}
/**
* Creates a resolver for react-hook-form using computed-types schema validation
* @param {Schema} schema - The computed-types schema to validate against
* @returns {Resolver<Type<typeof schema>>} A resolver function compatible with react-hook-form
* @example
* const schema = Schema({
* name: string,
* age: number
* });
*
* useForm({
* resolver: computedTypesResolver(schema)
* });
*/
export function computedTypesResolver<
Input extends FieldValues,
Context,
Output,
>(schema: FunctionType<Output, [Input]>): Resolver<Input, Context, Output> {
return async (values, _, options) => {
try {
const data = await schema(values);
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return {
errors: {},
values: data,
};
} catch (error: any) {
if (isValidationError(error)) {
return {
values: {},
errors: toNestErrors(parseErrorSchema(error), options),
};
}
throw error;
}
};
}

View File

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

View File

@ -0,0 +1,18 @@
{
"name": "@hookform/resolvers/effect-ts",
"amdName": "hookformResolversEffectTs",
"version": "1.0.0",
"private": true,
"description": "React Hook Form validation resolver: effect-ts",
"main": "dist/effect-ts.js",
"module": "dist/effect-ts.module.js",
"umd:main": "dist/effect-ts.umd.js",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"license": "MIT",
"peerDependencies": {
"@hookform/resolvers": "^2.0.0",
"effect": "^3.10.3",
"react-hook-form": "^7.55.0"
}
}

View File

@ -0,0 +1,88 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { Schema } from 'effect';
import React from 'react';
import { useForm } from 'react-hook-form';
import { effectTsResolver } from '..';
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
const schema = Schema.Struct({
username: Schema.String.pipe(
Schema.nonEmptyString({ message: () => USERNAME_REQUIRED_MESSAGE }),
),
password: Schema.String.pipe(
Schema.nonEmptyString({ message: () => PASSWORD_REQUIRED_MESSAGE }),
),
});
interface FormData {
username: string;
password: string;
}
interface Props {
onSubmit: (data: FormData) => void;
}
function TestComponent({ onSubmit }: Props) {
const { register, handleSubmit } = useForm<FormData>({
resolver: effectTsResolver(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 effect-ts", 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('');
});

View File

@ -0,0 +1,85 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { Schema } from 'effect';
import React from 'react';
import { useForm } from 'react-hook-form';
import { effectTsResolver } from '..';
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
const schema = Schema.Struct({
username: Schema.String.pipe(
Schema.nonEmptyString({ message: () => USERNAME_REQUIRED_MESSAGE }),
),
password: Schema.String.pipe(
Schema.nonEmptyString({ message: () => PASSWORD_REQUIRED_MESSAGE }),
),
});
type FormData = Schema.Schema.Type<typeof schema>;
function TestComponent({
onSubmit,
}: {
onSubmit: (data: FormData) => void;
}) {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: effectTsResolver(schema),
});
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 Zod 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();
});
export function TestComponentManualType({
onSubmit,
}: {
onSubmit: (data: FormData) => void;
}) {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<Schema.Schema.Type<typeof schema>, undefined, FormData>({
resolver: effectTsResolver(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>
);
}

View File

@ -0,0 +1,124 @@
import { Schema } from 'effect';
import { Field, InternalFieldName } from 'react-hook-form';
export const schema = Schema.Struct({
username: Schema.String.pipe(
Schema.nonEmptyString({ message: () => 'A username is required' }),
),
password: Schema.String.pipe(
Schema.pattern(new RegExp('.*[A-Z].*'), {
message: () => 'At least 1 uppercase letter.',
}),
Schema.pattern(new RegExp('.*[a-z].*'), {
message: () => 'At least 1 lowercase letter.',
}),
Schema.pattern(new RegExp('.*\\d.*'), {
message: () => 'At least 1 number.',
}),
Schema.pattern(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
{
message: () => 'At least 1 special character.',
},
),
Schema.minLength(8, { message: () => 'Must be at least 8 characters.' }),
),
accessToken: Schema.Union(Schema.String, Schema.Number),
birthYear: Schema.Number.pipe(
Schema.greaterThan(1900, {
message: () => 'Must be greater than the year 1900',
}),
Schema.filter((value) => value < new Date().getFullYear(), {
message: () => 'Must be before the current year.',
}),
),
email: Schema.String.pipe(
Schema.pattern(
new RegExp(
/^(?!\.)(?!.*\.\.)([A-Z0-9_+-.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i,
),
{
message: () => 'A valid email address is required.',
},
),
),
tags: Schema.Array(
Schema.Struct({
name: Schema.String,
}),
),
luckyNumbers: Schema.Array(Schema.Number),
enabled: Schema.Boolean,
animal: Schema.Union(Schema.String, Schema.Literal('bird', 'snake')),
vehicles: Schema.Array(
Schema.Union(
Schema.Struct({
type: Schema.Literal('car'),
brand: Schema.String,
horsepower: Schema.Number,
}),
Schema.Struct({
type: Schema.Literal('bike'),
speed: Schema.Number,
}),
),
),
});
export const validData: Schema.Schema.Type<typeof schema> = {
accessToken: 'abcd1234',
animal: 'dog',
birthYear: 2000,
email: 'johnDoe@here.there',
enabled: true,
luckyNumbers: [1, 2, 3, 4, 5],
password: 'Super#Secret123',
tags: [{ name: 'move' }, { name: 'over' }, { name: 'zod' }, { name: ';)' }],
username: 'johnDoe',
vehicles: [
{ type: 'bike', speed: 5 },
{ type: 'car', brand: 'BMW', horsepower: 150 },
],
};
export const invalidData = {
username: 'test',
password: 'Password123',
repeatPassword: 'Password123',
birthYear: 2000,
accessToken: '1015d809-e99d-41ec-b161-981a3c243df8',
email: 'john@doe.com',
tags: [{ name: 'test' }],
enabled: true,
animal: ['dog'],
luckyNumbers: [1, 2, '3'],
like: [
{
id: '1',
name: 'name',
},
],
vehicles: [
{ type: 'car', brand: 'BMW', horsepower: 150 },
{ type: 'car', brand: 'Mercedes' },
],
} as any as Schema.Schema.Type<typeof schema>;
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,74 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`effectTsResolver > should return a single error from effectTsResolver when validation fails 1`] = `
{
"errors": {
"animal": {
"message": "Expected string, actual ["dog"]",
"ref": undefined,
"type": "Type",
},
"luckyNumbers": [
,
,
{
"message": "Expected number, actual "3"",
"ref": undefined,
"type": "Type",
},
],
"password": {
"message": "At least 1 special character.",
"ref": {
"name": "password",
},
"type": "Refinement",
},
"vehicles": [
,
{
"horsepower": {
"message": "is missing",
"ref": undefined,
"type": "Missing",
},
},
],
},
"values": {},
}
`;
exports[`effectTsResolver > should return all the errors from effectTsResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
{
"errors": {
"phoneNumber": {
"message": "Please enter a valid phone number in international format.",
"ref": {
"name": "phoneNumber",
},
"type": "Refinement",
"types": {
"Refinement": "Please enter a valid phone number in international format.",
"Type": "Expected undefined, actual "123"",
},
},
},
"values": {},
}
`;
exports[`effectTsResolver > should return the first error from effectTsResolver when validation fails with \`validateAllFieldCriteria\` set to firstError 1`] = `
{
"errors": {
"phoneNumber": {
"message": "Please enter a valid phone number in international format.",
"ref": {
"name": "phoneNumber",
},
"type": "Refinement",
},
},
"values": {},
}
`;

View File

@ -0,0 +1,146 @@
import { Schema } from 'effect';
import { Resolver, useForm } from 'react-hook-form';
import { SubmitHandler } from 'react-hook-form';
import { effectTsResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
const shouldUseNativeValidation = false;
describe('effectTsResolver', () => {
it('should return values from effectTsResolver when validation pass', async () => {
const result = await effectTsResolver(schema)(validData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return a single error from effectTsResolver when validation fails', async () => {
const result = await effectTsResolver(schema)(invalidData, undefined, {
fields,
shouldUseNativeValidation,
});
expect(result).toMatchSnapshot();
});
it('should return the first error from effectTsResolver when validation fails with `validateAllFieldCriteria` set to firstError', async () => {
const SignupSchema = Schema.Struct({
phoneNumber: Schema.optional(
Schema.String.pipe(
Schema.pattern(/^\+\d{7,15}$/, {
message: () =>
'Please enter a valid phone number in international format.',
}),
),
),
});
const result = await effectTsResolver(SignupSchema)(
{ phoneNumber: '123' },
undefined,
{
fields: {
phoneNumber: {
ref: { name: 'phoneNumber' },
name: 'phoneNumber',
},
},
criteriaMode: 'firstError',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return all the errors from effectTsResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
const SignupSchema = Schema.Struct({
phoneNumber: Schema.optional(
Schema.String.pipe(
Schema.pattern(/^\+\d{7,15}$/, {
message: () =>
'Please enter a valid phone number in international format.',
}),
),
),
});
const result = await effectTsResolver(SignupSchema)(
{ phoneNumber: '123' },
undefined,
{
fields: {
phoneNumber: {
ref: { name: 'phoneNumber' },
name: 'phoneNumber',
},
},
criteriaMode: 'all',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
/**
* Type inference tests
*/
it('should correctly infer the output type from a effectTs schema', () => {
const resolver = effectTsResolver(Schema.Struct({ id: Schema.Number }));
expectTypeOf(resolver).toEqualTypeOf<
Resolver<Readonly<{ id: number }>, unknown, Readonly<{ id: number }>>
>();
});
it('should correctly infer the output type from a effectTs schema using a transform', () => {
const resolver = effectTsResolver(
Schema.Struct({
id: Schema.transform(Schema.Number, Schema.String, {
decode: (val) => String(val),
encode: (val) => Number(val),
}),
}),
);
expectTypeOf(resolver).toEqualTypeOf<
Resolver<Readonly<{ id: number }>, unknown, Readonly<{ id: string }>>
>();
});
it('should correctly infer the output type from a effectTs schema for the handleSubmit function in useForm', () => {
const schema = Schema.Struct({ id: Schema.Number });
const form = useForm({
resolver: effectTsResolver(schema),
});
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit)
.parameter(0)
.toEqualTypeOf<SubmitHandler<Readonly<{ id: number }>>>();
});
it('should correctly infer the output type from a effectTs schema with a transform for the handleSubmit function in useForm', () => {
const schema = Schema.Struct({
id: Schema.transform(Schema.Number, Schema.String, {
decode: (val) => String(val),
encode: (val) => Number(val),
}),
});
const form = useForm({
resolver: effectTsResolver(schema),
});
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
expectTypeOf(form.handleSubmit)
.parameter(0)
.toEqualTypeOf<SubmitHandler<Readonly<{ id: string }>>>();
});
});

View File

@ -0,0 +1,105 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import { Effect, Schema } from 'effect';
import { ArrayFormatter, decodeUnknown } from 'effect/ParseResult';
import { ParseOptions } from 'effect/SchemaAST';
import {
type FieldError,
FieldValues,
Resolver,
appendErrors,
} from 'react-hook-form';
export function effectTsResolver<Input extends FieldValues, Context, Output>(
schema: Schema.Schema<Output, Input>,
schemaOptions?: ParseOptions,
resolverOptions?: {
mode?: 'async' | 'sync';
raw?: false;
},
): Resolver<Input, Context, Output>;
export function effectTsResolver<Input extends FieldValues, Context, Output>(
schema: Schema.Schema<Output, Input>,
schemaOptions: ParseOptions | undefined,
resolverOptions: {
mode?: 'async' | 'sync';
raw: true;
},
): Resolver<Input, Context, Input>;
/**
* Creates a resolver for react-hook-form using Effect.ts schema validation
* @param {Schema.Schema<TFieldValues, I>} schema - The Effect.ts schema to validate against
* @param {ParseOptions} [schemaOptions] - Optional Effect.ts validation options
* @returns {Resolver<Schema.Schema.Type<typeof schema>>} A resolver function compatible with react-hook-form
* @example
* const schema = Schema.Struct({
* name: Schema.String,
* age: Schema.Number
* });
*
* useForm({
* resolver: effectTsResolver(schema)
* });
*/
export function effectTsResolver<Input extends FieldValues, Context, Output>(
schema: Schema.Schema<Output, Input>,
schemaOptions: ParseOptions = { errors: 'all', onExcessProperty: 'ignore' },
): Resolver<Input, Context, Output | Input> {
return (values, _, options) => {
return decodeUnknown(
schema,
schemaOptions,
)(values).pipe(
Effect.catchAll((parseIssue) =>
Effect.flip(ArrayFormatter.formatIssue(parseIssue)),
),
Effect.mapError((issues) => {
const validateAllFieldCriteria =
!options.shouldUseNativeValidation && options.criteriaMode === 'all';
const errors = issues.reduce(
(acc, error) => {
const key = error.path.join('.');
if (!acc[key]) {
acc[key] = { message: error.message, type: error._tag };
}
if (validateAllFieldCriteria) {
const types = acc[key].types;
const messages = types && types[String(error._tag)];
acc[key] = appendErrors(
key,
validateAllFieldCriteria,
acc,
error._tag,
messages
? ([] as string[]).concat(messages as string[], error.message)
: error.message,
) as FieldError;
}
return acc;
},
{} as Record<string, FieldError>,
);
return toNestErrors(errors, options);
}),
Effect.tap(() =>
Effect.sync(
() =>
options.shouldUseNativeValidation &&
validateFieldsNatively({}, options),
),
),
Effect.match({
onFailure: (errors) => ({ errors, values: {} }),
onSuccess: (result) => ({ errors: {}, values: result }),
}),
Effect.runPromise,
);
};
}

View File

@ -0,0 +1 @@
export * from './effect-ts';

View File

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

View File

@ -0,0 +1,88 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { Validator } from 'fluentvalidation-ts';
import React from 'react';
import { useForm } from 'react-hook-form';
import { fluentValidationResolver } from '../fluentvalidation-ts';
const USERNAME_REQUIRED_MESSAGE = 'username field is required';
const PASSWORD_REQUIRED_MESSAGE = 'password field is required';
type FormData = {
username: string;
password: string;
};
class FormDataValidator extends Validator<FormData> {
constructor() {
super();
this.ruleFor('username').notEmpty().withMessage(USERNAME_REQUIRED_MESSAGE);
this.ruleFor('password').notEmpty().withMessage(PASSWORD_REQUIRED_MESSAGE);
}
}
interface Props {
onSubmit: (data: FormData) => void;
}
function TestComponent({ onSubmit }: Props) {
const { register, handleSubmit } = useForm<FormData>({
resolver: fluentValidationResolver(new FormDataValidator()),
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 fluentvalidation-ts", 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('');
});

View File

@ -0,0 +1,63 @@
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import { Validator } from 'fluentvalidation-ts';
import React from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { fluentValidationResolver } from '../fluentvalidation-ts';
type FormData = {
username: string;
password: string;
};
class FormDataValidator extends Validator<FormData> {
constructor() {
super();
this.ruleFor('username')
.notEmpty()
.withMessage('username is a required field');
this.ruleFor('password')
.notEmpty()
.withMessage('password is a required field');
}
}
interface Props {
onSubmit: SubmitHandler<FormData>;
}
function TestComponent({ onSubmit }: Props) {
const {
register,
formState: { errors },
handleSubmit,
} = useForm({
resolver: fluentValidationResolver(new FormDataValidator()), // 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.getByText(/username is a required field/i)).toBeInTheDocument();
expect(screen.getByText(/password is a required field/i)).toBeInTheDocument();
expect(handleSubmit).not.toHaveBeenCalled();
});

View File

@ -0,0 +1,121 @@
import { Validator } from 'fluentvalidation-ts';
import { Field, InternalFieldName } from 'react-hook-form';
const beNumeric = (value: string | number | undefined) => !isNaN(Number(value));
export type Schema = {
username: string;
password: string;
repeatPassword: string;
accessToken?: string;
birthYear?: number;
email?: string;
tags?: string[];
enabled?: boolean;
like?: {
id: number;
name: string;
}[];
};
export type SchemaWithWhen = {
name: string;
value: string;
};
export class SchemaValidator extends Validator<Schema> {
constructor() {
super();
this.ruleFor('username')
.notEmpty()
.matches(/^\w+$/)
.minLength(3)
.maxLength(30);
this.ruleFor('password')
.notEmpty()
.matches(/.*[A-Z].*/)
.withMessage('One uppercase character')
.matches(/.*[a-z].*/)
.withMessage('One lowercase character')
.matches(/.*\d.*/)
.withMessage('One number')
.matches(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'))
.withMessage('One special character')
.minLength(8)
.withMessage('Must be at least 8 characters in length');
this.ruleFor('repeatPassword')
.notEmpty()
.must((repeatPassword, data) => repeatPassword === data.password);
this.ruleFor('accessToken');
this.ruleFor('birthYear')
.must(beNumeric)
.inclusiveBetween(1900, 2013)
.when((birthYear) => birthYear != undefined);
this.ruleFor('email').emailAddress();
this.ruleFor('tags');
this.ruleFor('enabled');
this.ruleForEach('like').setValidator(() => new LikeValidator());
}
}
export class LikeValidator extends Validator<{
id: number;
name: string;
}> {
constructor() {
super();
this.ruleFor('id').notNull();
this.ruleFor('name').notEmpty().length(4, 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',
},
],
} as Schema;
export const invalidData = {
password: '___',
email: '',
birthYear: 'birthYear',
like: [{ id: 'z' }],
// Must be set to "unknown", otherwise typescript knows that it is invalid
} as unknown as Required<Schema>;
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,129 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`fluentValidationResolver > should return a single error from fluentValidationResolver when validation fails 1`] = `
{
"errors": {
"birthYear": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
"email": {
"message": "Not a valid email address",
"ref": {
"name": "email",
},
"type": "validation",
},
"password": {
"message": "One uppercase character",
"ref": {
"name": "password",
},
"type": "validation",
},
"repeatPassword": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
},
"values": {},
}
`;
exports[`fluentValidationResolver > should return a single error from fluentValidationResolver with \`mode: sync\` when validation fails 1`] = `
{
"errors": {
"birthYear": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
"email": {
"message": "Not a valid email address",
"ref": {
"name": "email",
},
"type": "validation",
},
"password": {
"message": "One uppercase character",
"ref": {
"name": "password",
},
"type": "validation",
},
"repeatPassword": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
},
"values": {},
}
`;
exports[`fluentValidationResolver > should return all the errors from fluentValidationResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = `
{
"errors": {
"birthYear": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
"email": {
"message": "Not a valid email address",
"ref": {
"name": "email",
},
"type": "validation",
},
"password": {
"message": "One uppercase character",
"ref": {
"name": "password",
},
"type": "validation",
},
"repeatPassword": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
},
"values": {},
}
`;
exports[`fluentValidationResolver > should return all the errors from fluentValidationResolver when validation fails with \`validateAllFieldCriteria\` set to true and \`mode: sync\` 1`] = `
{
"errors": {
"birthYear": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
"email": {
"message": "Not a valid email address",
"ref": {
"name": "email",
},
"type": "validation",
},
"password": {
"message": "One uppercase character",
"ref": {
"name": "password",
},
"type": "validation",
},
"repeatPassword": {
"message": "Value is not valid",
"ref": undefined,
"type": "validation",
},
},
"values": {},
}
`;

View File

@ -0,0 +1,113 @@
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */
import { fluentValidationResolver } from '..';
import {
SchemaValidator,
fields,
invalidData,
validData,
} from './__fixtures__/data';
const shouldUseNativeValidation = false;
const validator = new SchemaValidator();
describe('fluentValidationResolver', () => {
it('should return values from fluentValidationResolver when validation pass', async () => {
const validatorSpy = vi.spyOn(validator, 'validate');
const result = await fluentValidationResolver(validator)(
validData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(validatorSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return values from fluentValidationResolver with `mode: sync` when validation pass', async () => {
const validatorSpy = vi.spyOn(validator, 'validate');
const result = await fluentValidationResolver(validator)(
validData,
undefined,
{ fields, shouldUseNativeValidation },
);
expect(validatorSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ errors: {}, values: validData });
});
it('should return a single error from fluentValidationResolver when validation fails', async () => {
const result = await fluentValidationResolver(validator)(
invalidData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return a single error from fluentValidationResolver with `mode: sync` when validation fails', async () => {
const validateSpy = vi.spyOn(validator, 'validate');
const result = await fluentValidationResolver(validator)(
invalidData,
undefined,
{ fields, shouldUseNativeValidation },
);
expect(validateSpy).toHaveBeenCalledTimes(1);
expect(result).toMatchSnapshot();
});
it('should return all the errors from fluentValidationResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
const result = await fluentValidationResolver(validator)(
invalidData,
undefined,
{
fields,
criteriaMode: 'all',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return all the errors from fluentValidationResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
const result = await fluentValidationResolver(validator)(
invalidData,
undefined,
{
fields,
criteriaMode: 'all',
shouldUseNativeValidation,
},
);
expect(result).toMatchSnapshot();
});
it('should return values from fluentValidationResolver when validation pass & raw=true', async () => {
const schemaSpy = vi.spyOn(validator, 'validate');
const result = await fluentValidationResolver(validator)(
validData,
undefined,
{
fields,
shouldUseNativeValidation,
},
);
expect(schemaSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ errors: {}, values: validData });
});
});

View File

@ -0,0 +1,123 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import {
AsyncValidator,
ValidationErrors,
Validator,
} from 'fluentvalidation-ts';
import { FieldError, FieldValues, Resolver } from 'react-hook-form';
function traverseObject<T>(
object: ValidationErrors<T>,
errors: Record<string, FieldError>,
parentIndices: (string | number)[] = [],
) {
for (const key in object) {
const currentIndex = [...parentIndices, key];
const currentValue = object[key];
if (Array.isArray(currentValue)) {
currentValue.forEach((item: any, index: number) => {
traverseObject(item, errors, [...currentIndex, index]);
});
} else if (typeof currentValue === 'object' && currentValue !== null) {
traverseObject(currentValue, errors, currentIndex);
} else if (typeof currentValue === 'string') {
errors[currentIndex.join('.')] = {
type: 'validation',
message: currentValue,
};
}
}
}
function parseErrorSchema<T>(
validationErrors: ValidationErrors<T>,
validateAllFieldCriteria: boolean,
) {
if (validateAllFieldCriteria) {
// TODO: check this but i think its always one validation error
}
const errors: Record<string, FieldError> = {};
traverseObject(validationErrors, errors);
return errors;
}
/**
* Creates a resolver for react-hook-form using FluentValidation schema validation
* @param {Validator<TFieldValues>} validator - The FluentValidation validator to use
* @returns {Resolver<TFieldValues>} A resolver function compatible with react-hook-form
* @example
* import { Validator } from 'fluentvalidation-ts';
*
* class SchemaValidator extends Validator<Schema> {
* constructor() {
* super();
* this.ruleFor('username').notEmpty();
* this.ruleFor('password').notEmpty();
* }
* }
*
* const validator = new SchemaValidator();
*
* useForm({
* resolver: fluentValidationResolver(validator)
* });
*/
export function fluentValidationResolver<TFieldValues extends FieldValues>(
validator: Validator<TFieldValues>,
): Resolver<TFieldValues> {
return async (values, _context, options) => {
const validationResult = validator.validate(values);
const isValid = Object.keys(validationResult).length === 0;
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return isValid
? {
values: values,
errors: {},
}
: {
values: {},
errors: toNestErrors(
parseErrorSchema(
validationResult,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
),
options,
),
};
};
}
export function fluentAsyncValidationResolver<
TFieldValues extends FieldValues,
TValidator extends AsyncValidator<TFieldValues>,
>(validator: TValidator): Resolver<TFieldValues> {
return async (values, _context, options) => {
const validationResult = await validator.validateAsync(values);
const isValid = Object.keys(validationResult).length === 0;
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return isValid
? {
values: values,
errors: {},
}
: {
values: {},
errors: toNestErrors(
parseErrorSchema(
validationResult,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
),
options,
),
};
};
}

Some files were not shown because too many files have changed in this diff Show More