UNPKG

emr-types

Version:

Comprehensive TypeScript Types Library for Electronic Medical Record (EMR) Applications - Domain-Driven Design with Zod Validation

197 lines 5.43 kB
/** * Form Utility Types for EMR Application * * Provides comprehensive form-related types for building type-safe forms * with validation, state management, and user interaction handling. * * @package emr-types */ /** * Supported form field types */ export var FormFieldType; (function (FormFieldType) { FormFieldType["TEXT"] = "text"; FormFieldType["EMAIL"] = "email"; FormFieldType["PASSWORD"] = "password"; FormFieldType["NUMBER"] = "number"; FormFieldType["TEL"] = "tel"; FormFieldType["DATE"] = "date"; FormFieldType["DATETIME"] = "datetime-local"; FormFieldType["TIME"] = "time"; FormFieldType["SELECT"] = "select"; FormFieldType["MULTISELECT"] = "multiselect"; FormFieldType["CHECKBOX"] = "checkbox"; FormFieldType["RADIO"] = "radio"; FormFieldType["TEXTAREA"] = "textarea"; FormFieldType["FILE"] = "file"; FormFieldType["HIDDEN"] = "hidden"; FormFieldType["CUSTOM"] = "custom"; })(FormFieldType || (FormFieldType = {})); // ============================================================================ // FORM VALIDATION UTILITIES // ============================================================================ /** * Create a form validator from Zod schema */ export function createFormValidator(schema) { return (values) => { const result = schema.safeParse(values); if (result.success) { return { isValid: true, errors: {}, fieldErrors: {}, globalErrors: [] }; } const errors = {}; const fieldErrors = {}; result.error.errors.forEach(error => { const field = error.path.join('.'); if (!errors[field]) { errors[field] = []; } errors[field].push(error.message); fieldErrors[field] = error.message; }); return { isValid: false, errors, fieldErrors, globalErrors: [] }; }; } /** * Create a field validator from Zod schema */ export function createFieldValidator(schema) { return (value) => { const result = schema.safeParse(value); return result.success ? undefined : result.error.errors[0]?.message; }; } /** * Combine multiple field validators */ export function combineFieldValidators(validators) { return (value, allValues) => { for (const validator of validators) { const error = validator(value, allValues); if (error) return error; } return undefined; }; } // ============================================================================ // FORM STATE UTILITIES // ============================================================================ /** * Create initial form state */ export function createInitialFormState(initialValues) { return { values: initialValues, errors: {}, touched: {}, dirty: {}, focused: {}, isValid: true, isDirty: false, isSubmitting: false, isSubmitted: false, submitCount: 0 }; } /** * Check if form is valid */ export function isFormValid(state) { return state.isValid && Object.keys(state.errors).length === 0; } /** * Check if form is dirty */ export function isFormDirty(state) { return state.isDirty || Object.values(state.dirty).some(Boolean); } /** * Get form field value */ export function getFieldValue(state, field) { return state.values[field]; } /** * Get form field error */ export function getFieldError(state, field) { return state.errors[field]; } /** * Check if field is touched */ export function isFieldTouched(state, field) { return state.touched[field] || false; } /** * Check if field is dirty */ export function isFieldDirty(state, field) { return state.dirty[field] || false; } // ============================================================================ // FORM TYPE GUARDS // ============================================================================ /** * Type guard for form field */ export function isFormField(value) { return (typeof value === 'object' && value !== null && 'name' in value && 'label' in value && 'type' in value && 'value' in value); } /** * Type guard for validated form field */ export function isValidatedFormField(value) { return (isFormField(value) && 'isValid' in value && 'validationErrors' in value && 'isTouched' in value && 'isDirty' in value && 'isFocused' in value); } /** * Type guard for form state */ export function isFormState(value) { return (typeof value === 'object' && value !== null && 'values' in value && 'errors' in value && 'touched' in value && 'dirty' in value && 'focused' in value && 'isValid' in value && 'isDirty' in value && 'isSubmitting' in value && 'isSubmitted' in value && 'submitCount' in value); } /** * Type guard for form validation result */ export function isFormValidationResult(value) { return (typeof value === 'object' && value !== null && 'isValid' in value && 'errors' in value && 'fieldErrors' in value && 'globalErrors' in value); } //# sourceMappingURL=form.js.map