|
| 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[]; |
0 commit comments