?? ??? ??? ?? ? ??????? ??? ?????. RHF(React Hook Form) ? Zod? ?? ?????? ???? ?? ??? ?? ??? ??? ???? ?? ??? ????? ??? ? ????. ? ????? Zod? RHF? ?? ???? ??? discriminateUnion? ???? ???? ??? ???? ??? ?????.
DiscriminatedUnion?? ??????
discriminatedUnion? ??? ???? ?? ?? ??? ???? ??? ????? ? ???? ?? ??? ?????. ? ?? ??? ?? ?? ?? ??? ???? ?? ?? ?? ?? ??? ???? ? ?????.
?? ?? ? ?? ??? ?? ??? ??? ?? ??? ??? ???.
- ????(?? ?? ? CVV ??)
- PayPal(??? ?? ??)
- ?? ??(?? ?? ? ?? ?? ??) ??? ? ??? discriminateUnion? ???? ???? ? ????. Zod? ??? ??? ??? ??
import * as zod from "zod" const IsNotEmptyString = (message: string) => zod.string({message}).min(1, {message}) export enum PaymentMethodEnum { CREDIT_CARD ="creditCard", PAYPAL = "paypal", BANKTRANSFER ="bankTransfer" } const creditCardSchema = zod.object({ type: zod.literal(PaymentMethodEnum.CREDIT_CARD), cardNumber: zod.string().regex(/^\d{16}$/, "The card number must contain 16 digits"), cvv: zod.string().regex(/^\d{3}$/, "The Card Validation Code must contain 3 digits"), }) const paypalSchema = zod.object({ type: zod.literal(PaymentMethodEnum.PAYPAL), email: zod.string().email("PayPal email is invalid"), }) const bankTransferSchema = zod.object({ type: zod.literal(PaymentMethodEnum.BANKTRANSFER), accountNumber: IsNotEmptyString("The account number must contain at least 10 characters"), bankCode: IsNotEmptyString("The bank code must contain at least 4 characters") }) export const paymentMethodSchema = () => zod.discriminatedUnion("type",[ creditCardSchema, paypalSchema,bankTransferSchema ]); export type PaymentMethodSchemaType = zod.infer < ReturnType <typeof paymentMethodSchema>>
**
??? ??? ??? ??????
**
-
DiscriminatedUnion? ?? ??? ?????.
- ??? 'creditCard'? ?? Zod? ??? ?? ???? ?????. ? ?? ??? ??? ?? ??
- ??? 'paypal'? ?? ??? ??? ?????. PayPal ???.
- ??? 'bankTransfer'? ?? ??? ?? ???? ?????. ???? ??
-
??? ?? :
- ? ??? ??????. ???? ?? ??? ??? ????? ?? ??? ???????. Zod? ?? ??? ?????.
-
React Hook ??? ???:
- ???? ?? RHF? ??? ??? ???? ??? ? ????. ?? ??? ?? ??? ??????. ?????.
**
??? discriminateUnion? ???? ??? ??????
**
- ??? ?? ??
- ? ?? ??? ??? ??? ????. ?? ?????.
- ???
- ??? ?? ??(?: ??? ??) ??)? ?? ??? ? ????. ??(?: ??? ?? ??)
- ??
- ??? ?? ?????? ?????. ?? ?????.
**
React Hook ??? ??
**
React Hook Form? ???? ???? ???? ????? ??? ?? ??? ? ????. DiscriminateUnion? ???? ??? ???? ?? Zod? RHF? ???? ??? ??? ????.
'react-hook-form'?? { useForm, SubmitHandler, FieldErrors } ????; import { zodResolver } from '@hookform/resolvers/zod'; ???? './??.css'; ?? { ?? ??Enum, ?? ?????, ?? ????? ??, } '../validators/validate-??-???'??; const ?? = () => { const form = useForm<PaymentMethodSchemaType>({ ?? ????: zodResolver(?? ?? ???()), ???: { ??: PaymentMethodEnum.CREDIT_CARD, }, }); const { ????, formState, handlerSubmit } = ??; const { ?? } = formState; const PaymentType = form.watch().type; const handlerChangePaymentType = (??: PaymentMethodEnum) => { form.setValue('type', ??); }; const handlerResetForm = () => { form.reset(GetErrorState(?? ??)); }; const onSubmit: SubmitHandler<PaymentMethodSchemaType> = (???) => { console.log('???', ???); }; const PaymentTypeFormNode: React.ReactNode = (() => { ???(?? ??) { ?? PaymentMethodEnum.BANKTRANSFER: constbankTransferErrors = getErrorsByPaymentType(errors, PaymentType); ?? ( <div> <div className="form"> <??>????</??> <?? {...??('????')} placeholder="????? ?????" /> {bankTransferErrors?.accountNumber?.message && ( <span className="errormessage"> {bankTransferErrors.accountNumber.message} </??> )} </div> <div className="form"> <??>?? ??</??> <?? {...??('????')} placeholder="?? ??? ?????" /> {bankTransferErrors?.bankCode?.message && ( <span className="errormessage"> {bankTransferErrors.bankCode.message} </??> )} </div> </div> ); ?? PaymentMethodEnum.CREDIT_CARD: const CreditCardErrors = getErrorsByPaymentType(errors, PaymentType); ?? ( <div> <div className="form"> <??>????</??> <?? {...??('????')} placeholder="????? ?????" /> {creditCardErrors?.cardNumber && ( <span className="errormessage"> {creditCardErrors.cardNumber.message} </??> )} </div> <div className="form"> <??>CVV</label> <?? {...??('cvv')} placeholder="?? ?? ??? ?????" /> {creditCardErrors?.cvv && ( <span className="errormessage"> {creditCardErrors.cvv.message} </??> )} </div> </div> ); ?? PaymentMethodEnum.PAYPAL: const paypalErrors = getErrorsByPaymentType(errors, PaymentType); ?? ( <div className="form"> <??>???</??> <?? ??="???" {...??('???')} placeholder="???? ?????" /> {paypalErrors?.email?.message && ( <span className="errormessage">{paypalErrors.email.message}</span> )} </div> ); ??: ??? ?? ??( '??? ?? ??: ?? ?' ?? ?? ); } })(); ?? ( <form className="form-wrapper" onSubmit={handleSubmit(onSubmit)}> <div className="??"> <p>**</p> <h2> ?? </h2> <p>**<br> ?? ????. ? ??? ??? ???? ????. discriminateUnion? ??? ???? ??? ? ?? ???? ?????. DiscriminateUnion? ???? ? ?? ??? ??? ????? ??? ?????. ? ??? ????? ?????. ?? ??? ?? ???? ? ????.</p> <p>??? ?? ??: <br> https://stackblitz.com/edit/vitejs-vite-ppgw9zrb?file=src/pages/paids.tsx</p>
? ??? DiscriminateUnion ? React Hook Form? ??? ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Node.js?? HTTP ??? ???? ? ?? ???? ??? ????. 1. ?? ????? ????? ??? ??? ? ?? ????? ?? ?? ? https.get () ??? ?? ??? ??? ? ?? ????? ?? ??? ?????. 2.axios? ??? ???? ? ?? ??????. ??? ??? ??? ??? ??? ??? ???/???, ?? JSON ??, ???? ?? ?????. ??? ?? ??? ????? ?? ????. 3. ?? ??? ??? ??? ??? ???? ???? ??? ??? ???? ?????.

JavaScript ??? ??? ?? ?? ? ?? ???? ????. ?? ???? ???, ??, ??, ?, ???? ?? ? ??? ?????. ?? ????? ?? ?? ? ? ??? ????? ?? ??? ??? ????. ??, ?? ? ??? ?? ?? ??? ??? ??? ???? ??? ??? ???? ??? ?? ??? ????. ?? ? ????? ??? ???? ? ??? ? ??? TypeofNull? ??? ?????? ??? ? ????. ? ? ?? ??? ???? ?????? ????? ???? ??? ???? ? ??? ? ? ????.

?????, JavaScript ???! ?? ? JavaScript ??? ?? ?? ?????! ?? ?? ??? ??? ??? ? ????. Deno?? Oracle? ?? ??, ??? JavaScript ?? ??? ????, Google Chrome ???? ? ??? ??? ???? ?????. ?????! Deno Oracle? "JavaScript"??? ????? Oracle? ?? ??? ??? ??????. Node.js? Deno? ??? ? Ryan Dahl? ??? ?????? ???? ????? JavaScript? ??? ???? Oracle? ????? ???? ?????.

Cacheapi? ?????? ?? ???? ??? ???? ???, ?? ??? ??? ?? ???? ? ??? ?? ? ???? ??? ??????. 1. ???? ????, ??? ??, ?? ?? ?? ???? ???? ??? ? ????. 2. ??? ?? ?? ??? ?? ? ? ????. 3. ?? ?? ?? ?? ?? ??? ??? ?? ?????. 4. ??? ???? ?? ?? ???? ?? ?? ?? ?? ?? ???? ?? ?? ??? ??? ? ????. 5. ?? ???? ??, ??? ??? ? ??? ??, ?? ??? ? ?? ???? ???? ???? ? ?? ?????. 6.?? ??? ?? ?? ?? ??, ???? ?? ? HTTP ?? ????? ?????? ???????.

??? JavaScript?? ??? ??? ?????? ?? ???????. ?? ??, ?? ?? ? ??? ??? ?? ????? ????? ?????. 1. ?? ??? ??? ????? ???? ??. ()? ?? ??? ??? ?????. ?. ()? ?? ??? ?? ??? ??? ?? ? ? ????. 2. ?? ??? .catch ()? ???? ?? ??? ??? ?? ??? ??????, ??? ???? ???? ????? ??? ? ????. 3. Promise.all ()? ?? ????? (?? ?? ?? ? ??????? ??), Promise.Race () (? ?? ??? ?? ?) ? Promise.AllSettled () (?? ??? ???? ??)

.map (), .filter () ? .reduce ()? ?? JavaScript ?? ?? ???? ??? ??? ??? ? ? ????. 1) .map ()? ??? ??? ??? ???? ? ??? ???? ? ?????. 2) .filter ()? ???? ??? ????? ? ?????. 3) .reduce ()? ???? ?? ??? ???? ? ?????. ???? ??? ????? ??? ?? ?? ??? ?????.

JavaScript? ??? ??? ?? ??, ? ? ? ?? ???? ???? ??? ??? ?????. 1. ?? ??? ?? ??? ???? ??? ??? ??? ??? ?? WebAPI? ?????. 2. WebAPI? ??????? ??? ?? ? ? ??? ?? ??? (??? ?? ?? ???? ??)? ????. 3. ??? ??? ?? ??? ?? ??? ?????. ?? ??? ??? ????? ??? ??? ?? ? ???? ?????. 4. ???? ?? (? : Promise. 5. ??? ??? ???? ?? ???? ???? ?? ?? ?? ??? ????? ? ??????.

??? ??? ?? ???? ?? ??? ???? ?? ??? ??? ?? ??? ?? ??? ?????. 1. ??? ?? : ?? ??? ?? ? ? ???? ?? ??? ???? ??? ???? ??????. ?? ??, ??? ?? ? ? ?? ??? ?? ? ?? ??? ??????. 2. ??? ?? : ??? ???? ?? ?? ??? ?? ???? ????? ? ?? ?????? ???? ????? ? ?? ?? ??? true? ??????. 3. ?? ???? ?? ?? ??? ?? ??, ?? ??? ? ?? ???? ?????. 4. DOM ??? ???? ??, ?? ? ??? ? ??? ??? ?? ???? ?? ???? ?????.
