Skip to content
This repository was archived by the owner on Aug 9, 2026. It is now read-only.

Commit 24e37f1

Browse files
(react-hooks) feat: add phone number validation (#31)
This pull request introduces a new phone number validation feature to the `@magnidev/react-hooks` package and improves code maintainability and documentation. The main changes include adding a `phoneNumberSchema`, implementing a `useValidatePhoneNumber` hook, updating exports for easier usage, and enhancing documentation for validation hooks. ### Phone Number Validation * Added a new `phoneNumberSchema` to `auth-schema.ts` for validating phone numbers, supporting international formats and length checks. * Implemented a new `useValidatePhoneNumber` React hook in `use-validations.ts` to validate phone numbers using the new schema. * Exported `phoneNumberSchema` and `useValidatePhoneNumber` for use in other modules. [[1]](diffhunk://#diff-22e8fc7bf75946e4c7e38e1e5e564c5495734705a91fb49f8f9d85be24f7abe0R195) [[2]](diffhunk://#diff-32f91bd59d518355999d9c413973fa10bffb51c75d3bc3f05984058a92f0fdc1L70-R82) ### Code Organization and Exports * Updated `index.ts` to use wildcard exports for both hooks and schemas, simplifying imports for library consumers. ### Documentation Improvements * Added JSDoc-style comments to all validation hooks in `use-validations.ts` to clarify their usage and parameters. [[1]](diffhunk://#diff-22e8fc7bf75946e4c7e38e1e5e564c5495734705a91fb49f8f9d85be24f7abe0R16-R20) [[2]](diffhunk://#diff-22e8fc7bf75946e4c7e38e1e5e564c5495734705a91fb49f8f9d85be24f7abe0R43-R47) [[3]](diffhunk://#diff-22e8fc7bf75946e4c7e38e1e5e564c5495734705a91fb49f8f9d85be24f7abe0R70-R73) [[4]](diffhunk://#diff-22e8fc7bf75946e4c7e38e1e5e564c5495734705a91fb49f8f9d85be24f7abe0R96-R100) [[5]](diffhunk://#diff-22e8fc7bf75946e4c7e38e1e5e564c5495734705a91fb49f8f9d85be24f7abe0R132-R162) ### Version Update * Bumped the package version from `1.0.1` to `1.1.0` to reflect the addition of new features.
2 parents 05e1f59 + 7abc40b commit 24e37f1

4 files changed

Lines changed: 68 additions & 16 deletions

File tree

packages/react-hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@magnidev/react-hooks",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "A collection of reusable React hooks for modern web development, designed to simplify state management, validation, and other common patterns in React applications.",
55
"keywords": [
66
"react",

packages/react-hooks/src/hooks/use-validations.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
emailSchema,
55
nameSchema,
66
passwordSchema,
7+
phoneNumberSchema,
78
usernameSchema,
89
} from "../schemas/auth-schema";
910

@@ -12,6 +13,11 @@ type ValidationResult = {
1213
errors: string[];
1314
};
1415

16+
/**
17+
* Validates an email address.
18+
* @param email - The email address to validate.
19+
* @returns An object containing the validation result and any error messages.
20+
*/
1521
const useValidateEmail = (email?: string): ValidationResult =>
1622
useMemo(() => {
1723
const { success, error } = emailSchema().safeParse(email);
@@ -34,6 +40,11 @@ const useValidateEmail = (email?: string): ValidationResult =>
3440
};
3541
}, [email]);
3642

43+
/**
44+
* Validates a password.
45+
* @param password - The password to validate.
46+
* @returns An object containing the validation result and any error messages.
47+
*/
3748
const useValidatePassword = (password?: string): ValidationResult =>
3849
useMemo(() => {
3950
const { success, error } = passwordSchema().safeParse(password);
@@ -56,6 +67,10 @@ const useValidatePassword = (password?: string): ValidationResult =>
5667
};
5768
}, [password]);
5869

70+
/** * Validates a name.
71+
* @param name - The name to validate.
72+
* @returns An object containing the validation result and any error messages.
73+
*/
5974
const useValidateName = (name?: string): ValidationResult =>
6075
useMemo(() => {
6176
const { success, error } = nameSchema().safeParse(name);
@@ -78,6 +93,11 @@ const useValidateName = (name?: string): ValidationResult =>
7893
};
7994
}, [name]);
8095

96+
/** * Validates a username.
97+
* @param username - The username to validate.
98+
* @param options - Optional settings for validation.
99+
* @returns An object containing the validation result and any error messages.
100+
*/
81101
const useValidateUsername = (
82102
username?: string,
83103
options?: {
@@ -109,6 +129,37 @@ const useValidateUsername = (
109129
};
110130
}, [username, options]);
111131

132+
/** * Validates a phone number.
133+
* @param phoneNumber - The phone number to validate.
134+
* @returns An object containing the validation result and any error messages.
135+
*/
136+
const useValidatePhoneNumber = (phoneNumber?: string): ValidationResult =>
137+
useMemo(() => {
138+
const { success, error } = phoneNumberSchema.safeParse(phoneNumber);
139+
140+
if (success) {
141+
return { isValid: true, errors: [] };
142+
}
143+
144+
const errors: string[] = [];
145+
146+
if (error.issues.length > 0) {
147+
for (const issue of error.issues) {
148+
errors.push(issue.message);
149+
}
150+
}
151+
152+
return {
153+
isValid: errors.length === 0,
154+
errors,
155+
};
156+
}, [phoneNumber]);
157+
158+
/** * Validates text against a provided schema.
159+
* @param text - The text to validate.
160+
* @param schema - The Zod schema to validate against.
161+
* @returns An object containing the validation result and any error messages.
162+
*/
112163
const useValidateText = (text: string, schema: z.ZodType): ValidationResult => {
113164
if (!schema) {
114165
throw new Error("Schema is required for validation");
@@ -141,6 +192,7 @@ export {
141192
useValidatePassword,
142193
useValidateName,
143194
useValidateUsername,
195+
useValidatePhoneNumber,
144196
useValidateText,
145197
type ValidationResult,
146198
};

packages/react-hooks/src/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
// biome-ignore lint/performance/noBarrelFile: This is a library entry point, not a barrel file
2-
export {
3-
useValidateEmail,
4-
useValidateName,
5-
useValidatePassword,
6-
useValidateText,
7-
useValidateUsername,
8-
type ValidationResult,
9-
} from "./hooks/use-validations";
2+
export * from "./hooks/use-validations";
103

11-
export {
12-
emailSchema,
13-
nameSchema,
14-
passwordSchema,
15-
usernameSchema,
16-
} from "./schemas/auth-schema";
4+
export * from "./schemas/auth-schema";

packages/react-hooks/src/schemas/auth-schema.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@ const passwordSchema = (): z.ZodType =>
6767
error: "Must include at least one special character",
6868
});
6969

70-
export { nameSchema, usernameSchema, emailSchema, passwordSchema };
70+
const phoneNumberSchema = z
71+
.string()
72+
.min(10, "Phone number must be at least 10 digits")
73+
.max(15, "Phone number must be at most 15 digits")
74+
.regex(/^\+?[1-9]\d{1,14}$/, "Invalid phone number format");
75+
76+
export {
77+
nameSchema,
78+
usernameSchema,
79+
emailSchema,
80+
passwordSchema,
81+
phoneNumberSchema,
82+
};

0 commit comments

Comments
 (0)