Skip to content

Commit

Permalink
Refactor Rule and Message interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Codycody31 committed Nov 29, 2023
1 parent 1d642e1 commit f850a2c
Showing 1 changed file with 57 additions and 27 deletions.
84 changes: 57 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,85 @@
/**
* @typedef {Object} Rule
* @property {'string' | 'number' | 'boolean' | 'integer' | 'float' | 'date'} type - The data type of the field.
* @property {boolean} [required] - Whether the field is required.
* @property {number} [min] - Minimum value/length.
* @property {number} [max] - Maximum value/length.
* @property {string} [match] - Field must match the value of this field.
* @property {string} [validate] - Special validation ('email', etc.).
* @property {Function} [custom] - Custom validation function. Should return true if valid.
* @property {RegExp} [regex] - Regular expression to validate the field.
*/
interface Rule {
/**
* The data type of the field.
*/
type: "string" | "number" | "boolean" | "integer" | "float" | "date";
/**
* Whether the field is required.
*/
required?: boolean;
/**
* Minimum value/length.
*/
min?: number;
/**
* Maximum value/length.
*/
max?: number;
/**
* Field must match the value of this field.
*/
match?: string;
/**
* Special validation ('email', etc.).
*/
validate?: string;
/**
* Custom validation function. Should return true if valid.
*/
custom?: (value: any) => boolean | Promise<boolean>;
/**
* Regular expression to validate the field.
*/
skip?: (input: any) => boolean;
/**
* Regular expression to validate the field.
*/
regex?: RegExp;
}

/**
* @typedef {Object} Message
* @property {string} type - The error message for incorrect type.
* @property {string} [required] - The error message for missing required field.
* @property {string} [min] - The error message for value/length below minimum.
* @property {string} [max] - The error message for value/length above maximum.
* @property {string} [match] - The error message for non-matching fields.
* @property {string} [validate] - The error message for invalid email.
* @property {string} [custom] - The error message for custom validation failure.
* @property {string} [regex] - The error message for regex validation failure.
*/
interface Message {
/**
* The error message for incorrect type.
*/
type: string;
/**
* The error message for missing required field.
*/
required?: string;
/**
* The error message for value/length below minimum.
*/
min?: string;
/**
* The error message for value/length above maximum.
*/
max?: string;
/**
* The error message for non-matching fields.
*/
match?: string;
/**
* The error message for invalid email.
*/
validate?: string;
/**
* The error message for custom validation failure.
*/
custom?: string;
/**
* The error message for regex validation failure.
*/
regex?: string;
}

/**
* @typedef {Object} Options
* @property {boolean} [trackPassedFields] - Whether to track fields that passed validation.
* @property {boolean} [strictMode] - Flag to enable strict mode. If true, fields not defined in rules will be flagged as errors.
*/
interface Options {
/**
* Whether to track fields that passed validation.
*/
trackPassedFields?: boolean;
/**
* Flag to enable strict mode. If true, fields not defined in rules will be flagged as errors.
*/
strictMode?: boolean;
}

Expand Down

0 comments on commit f850a2c

Please sign in to comment.