@kadconsulting/dry
Version:
KAD Reusable Component Library
43 lines (42 loc) • 1.32 kB
TypeScript
export type StringFieldTypes = 'non-empty-string' | 'email';
export type PhoneNumberValidationRule = {
type: 'phone-number';
/** The value to validate */
value: string;
region: string;
};
export type BooleanValidationRule = {
type: 'boolean';
/** The value to validate */
value: boolean;
/** The value required for the validation rule to be satisfied */
requiredValue: boolean;
/** Error message; while the error message is generally straightforward
* for other types of validation such as an invalid phone number, boolean
* validation isn't specific enough to the user and needs some additional
* context.
*/
errorMessage: string;
};
export type StringValidationRule = {
type: StringFieldTypes;
/** The value to validate */
value: string;
};
export type PostalCode5DigitValidationRule = {
type: 'postal-code-5-digit';
/** The value to validate */
value: string;
};
export type FieldValidationRule = StringValidationRule | BooleanValidationRule | PhoneNumberValidationRule | PostalCode5DigitValidationRule;
export type Form = {
rules: {
[key: string]: FieldValidationRule;
};
};
export declare const useValidation: (form: Form) => {
errors: {
[key: string]: string | null;
};
errorsExist: boolean;
};