Skip to content

Commit 7089b5d

Browse files
committed
♻️ Add types for auth form (#2446)
1 parent e5fa034 commit 7089b5d

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

src/lib/types/auth_forms.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Defines validation constraints that can be applied to form fields.
3+
*
4+
* @interface FieldConstraints
5+
* @property {number} [minlength] - Minimum number of characters required for the field value
6+
* @property {number} [maxlength] - Maximum number of characters allowed for the field value
7+
* @property {boolean} [required] - Whether the field is mandatory and must have a value
8+
* @property {string} [pattern] - Regular expression pattern that the field value must match
9+
*/
10+
type FieldConstraints = {
11+
minlength?: number;
12+
maxlength?: number;
13+
required?: boolean;
14+
pattern?: string;
15+
};
16+
17+
type AuthFormConstraints = {
18+
username?: FieldConstraints;
19+
password?: FieldConstraints;
20+
};
21+
22+
/**
23+
* Represents the state and data structure for authentication forms.
24+
*
25+
* @interface AuthForm
26+
* @property {string} id - Unique identifier for the form instance
27+
* @property {boolean} valid - Indicates whether the form data passes validation
28+
* @property {boolean} posted - Indicates whether the form has been submitted
29+
* @property {Object} data - The form input data
30+
* @property {string} data.username - The username field value
31+
* @property {string} data.password - The password field value
32+
* @property {Record<string, unknown>} errors - Collection of validation errors keyed by field name
33+
* @property {AuthFormConstraints} [constraints] - Optional validation constraints for the form
34+
* @property {Record<string, unknown>} [shape] - Optional form schema or structure definition
35+
* @property {string} message - General message associated with the form (success, error, etc.)
36+
*/
37+
export type AuthForm = {
38+
id: string;
39+
valid: boolean;
40+
posted: boolean;
41+
data: { username: string; password: string };
42+
errors: Record<string, unknown>;
43+
constraints?: AuthFormConstraints;
44+
shape?: Record<string, unknown>;
45+
message: string;
46+
};
47+
48+
/**
49+
* Represents a strategy for creating authentication forms.
50+
*
51+
* @interface AuthFormCreationStrategy
52+
* @property {string} name - The unique identifier or display name for this creation strategy
53+
* @property {() => Promise<{ form: AuthForm }>} run - Asynchronous function that executes the strategy and returns the created authentication form
54+
*/
55+
export type AuthFormCreationStrategy = {
56+
name: string;
57+
run: () => Promise<{ form: AuthForm }>;
58+
};
59+
60+
export type AuthFormCreationStrategies = AuthFormCreationStrategy[];
61+
62+
/**
63+
* Defines a validation strategy for authentication forms.
64+
*
65+
* A validation strategy encapsulates the logic for validating authentication
66+
* form data from HTTP requests and returning the processed form object.
67+
*
68+
* @example
69+
* ```typescript
70+
* const loginStrategy: AuthFormValidationStrategy = {
71+
* name: 'login',
72+
* run: async (request) => {
73+
* // Validation logic here
74+
* return { form: validatedForm };
75+
* }
76+
* };
77+
* ```
78+
*/
79+
export type AuthFormValidationStrategy = {
80+
name: string;
81+
run: (request: Request) => Promise<{ form: AuthForm }>;
82+
};
83+
84+
export type AuthFormValidationStrategies = AuthFormValidationStrategy[];

src/lib/utils/auth_forms.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { redirect } from '@sveltejs/kit';
33
import { superValidate } from 'sveltekit-superforms/server';
44
import { zod } from 'sveltekit-superforms/adapters';
55

6+
import type {
7+
AuthFormCreationStrategies,
8+
AuthFormValidationStrategies,
9+
} from '$lib/types/auth_forms';
10+
11+
import { authSchema } from '$lib/zod/schema';
612
import { SEE_OTHER } from '$lib/constants/http-response-status-codes';
713
import { HOME_PAGE } from '$lib/constants/navbar-links';
8-
import { authSchema } from '$lib/zod/schema';
914

1015
/**
1116
* Initialize authentication form pages (login/signup)
@@ -56,7 +61,7 @@ export const createAuthFormWithFallback = async () => {
5661
* https://superforms.rocks/concepts/client-validation
5762
* https://superforms.rocks/api#supervalidate-options
5863
*/
59-
const formCreationStrategies = [
64+
const formCreationStrategies: AuthFormCreationStrategies = [
6065
{
6166
name: '(Basic case) Use standard superValidate',
6267
async run() {
@@ -112,7 +117,7 @@ export const validateAuthFormWithFallback = async (request: Request) => {
112117
* Form validation strategies for action handlers
113118
* Each strategy attempts a different approach to validate form data from requests
114119
*/
115-
const formValidationStrategies = [
120+
const formValidationStrategies: AuthFormValidationStrategies = [
116121
{
117122
name: '(Basic Case) Use standard superValidate with request',
118123
async run(request: Request) {

0 commit comments

Comments
 (0)