import type { ValidationHelper } from "./validation-helper"; //all web component that has validation will implements this interface export interface WithValidation { readonly isAutoValidationDisabled: boolean, readonly validation: ValidationHelper, required: boolean, readonly validity: ValidityState | undefined, validationMessage: string | null, clearValidationError: ClearValidationErrorCallback, checkValidity: () => boolean reportValidity: () => boolean } export type ValidationResultSummary = { isValid: boolean | null; message: string | null; } export type ValidationResultItem = { isValid: boolean | null; message: string | null; validation: ValidationItem; } export type ValidationResult = { validationList: ValidationResultItem[]; isAllValid: boolean; } export type ValidatorFunction = (value: ValidationValue) => boolean | string | Promise //TValue will be different for some inputs like date input export type ValidationItem = { /** * @property key that will be used to identify validation item */ key?: any; /** * @property validation function or regex to match value with * @description if you want to use async validation you can return promise from validator function * @description if you leave it empty (undefined) it will always considered as invalid (used for manual error handler like server side validation or yup , zod,... validations) */ validator?: RegExp | ValidatorFunction; /** * @property error message that will be shown if validator return false or regex failed */ message: string; /** * @property type of error (category) its optional */ stateType?: keyof ValidityStateFlags; /** * @property defer validator execution until all non-deferred validation executed.(good for async validator) */ defer?: boolean } export type checkValidityParameters = { /** * @property showError determine if you want to show error in invalidate state or not. default is true */ showError?: boolean, /** * @property the value you want to check validation on its optional and if not set it will get from component value) */ value?: ValidationValue } //Error showing and clearing mechanism types export type ShowValidationErrorParameters = { message: string, } //callbacks export type ShowValidationErrorCallback = (error: ShowValidationErrorParameters) => void; export type ClearValidationErrorCallback = () => void; export type GetValueCallback = () => (ValidationValue | Promise); export type GetValueStringCallback = (value: ValidationValue) => string; export type GetValidationsCallback = () => ValidationItem[]; export type SetValidationResultCallback = (result: ValidationResult) => void; // export type Callbacks = { getValue: GetValueCallback, getValueString: GetValueStringCallback, showValidationError: ShowValidationErrorCallback[], clearValidationError: ClearValidationErrorCallback[], getValidations: GetValidationsCallback[], setValidationResult: SetValidationResultCallback[] } export type CallbacksInput = { [key in keyof Callbacks]?: Callbacks[key] extends Array ? Callbacks[key][0] : Callbacks[key] }