Skip to content

Instantly share code, notes, and snippets.

View chihabhajji's full-sized avatar
🎯
Focusing

Chihab HAJJI chihabhajji

🎯
Focusing
View GitHub Profile
import { useForm, SubmitHandler, SubmitErrorHandler, ValidationMode } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from "@hookform/resolvers/zod";
import { BaseSyntheticEvent, useState } from "react";
interface UseValidationProps<T extends z.ZodType<any, any>> {
schema: T;
onSubmit: SubmitHandler<z.infer<T>>;
}
@chihabhajji
chihabhajji / form-helper.tsx
Created April 21, 2023 07:24
Generic form handler to update the state
type NonArrayFunctionKeys<T> = {
[K in keyof T]-?: T[K] extends Function | unknown[] ? never : K
}[keyof T];
const handleInputChange = <TKey extends NonArrayFunctionKeys<User>, TValue>(
value: TValue,
name: TKey,
// setStateCallback: React.Dispatch<React.SetStateAction<User>>
) => setProfile((prev) => ({...prev, [name]: value}));
type KeysMatching<T, V> = { [K in keyof T]-?: T[K] extends V ? K : never }[keyof T];
@chihabhajji
chihabhajji / NestJS-IRepository.ts
Last active March 30, 2023 11:21
Generic'ish nestjs mongoose repository
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
FilterQuery,
HydratedDocument,
LeanDocument,
Model,
model,
ProjectionType,