Skip to content

Commit 07ebd04

Browse files
bug resolve
1 parent 36c8a23 commit 07ebd04

File tree

11 files changed

+83
-18
lines changed

11 files changed

+83
-18
lines changed

dist/FormValidator.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import type { ValidatorFunction, AsyncValidatorFunction, ValidationResult, ValidationSchema } from './types';
12
export declare class FormValidator {
23
private validators;
34
private asyncValidators;
45
constructor();
6+
addValidator(name: string, validator: ValidatorFunction): void;
7+
addAsyncValidator(name: string, validator: AsyncValidatorFunction): void;
8+
validateForm(formData: Record<string, any>, schema: ValidationSchema): Promise<ValidationResult>;
59
private validateAsync;
610
}
11+
export declare const formValidator: FormValidator;

dist/FormValidator.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
3333
};
3434
})();
3535
Object.defineProperty(exports, "__esModule", { value: true });
36-
exports.FormValidator = void 0;
36+
exports.formValidator = exports.FormValidator = void 0;
3737
const validators = __importStar(require("./validators"));
3838
class FormValidator {
3939
validators;
@@ -42,6 +42,42 @@ class FormValidator {
4242
this.validators = { ...validators };
4343
this.asyncValidators = {};
4444
}
45+
// Add custom sync validator
46+
addValidator(name, validator) {
47+
this.validators[name] = validator;
48+
}
49+
// Add custom async validator
50+
addAsyncValidator(name, validator) {
51+
this.asyncValidators[name] = validator;
52+
}
53+
// Public validation method
54+
async validateForm(formData, schema) {
55+
const errors = [];
56+
const asyncTasks = [];
57+
for (const [field, rules] of Object.entries(schema)) {
58+
const value = formData[field];
59+
// Sync validation
60+
for (const rule of rules.filter(r => !r.async)) {
61+
const validator = this.validators[rule.type];
62+
if (validator && !validator(value, rule.options)) {
63+
errors.push({
64+
field,
65+
message: rule.message || `Invalid ${field}`
66+
});
67+
}
68+
}
69+
// Async validation
70+
const asyncRules = rules.filter(r => r.async);
71+
if (asyncRules.length) {
72+
asyncTasks.push(this.validateAsync(field, value, asyncRules));
73+
}
74+
}
75+
const asyncErrors = await Promise.all(asyncTasks);
76+
return {
77+
isValid: errors.length === 0 && asyncErrors.flat().length === 0,
78+
errors: [...errors, ...asyncErrors.flat()]
79+
};
80+
}
4581
async validateAsync(field, value, rules) {
4682
const errors = [];
4783
for (const rule of rules) {
@@ -57,3 +93,4 @@ class FormValidator {
5793
}
5894
}
5995
exports.FormValidator = FormValidator;
96+
exports.formValidator = new FormValidator();

dist/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './validators';
2-
export * from './FormValidator';
3-
export * from './ValidationChain';
42
export * from './types';
3+
export { FormValidator, formValidator } from './FormValidator';
4+
export { ValidationChain } from './ValidationChain';
55
import { ValidationChain } from './ValidationChain';
66
export declare const validate: (value: any) => ValidationChain;

dist/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
1414
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
1515
};
1616
Object.defineProperty(exports, "__esModule", { value: true });
17-
exports.validate = void 0;
17+
exports.validate = exports.ValidationChain = exports.formValidator = exports.FormValidator = void 0;
1818
__exportStar(require("./validators"), exports);
19-
__exportStar(require("./FormValidator"), exports);
20-
__exportStar(require("./ValidationChain"), exports);
2119
__exportStar(require("./types"), exports);
22-
const ValidationChain_1 = require("./ValidationChain");
23-
const validate = (value) => new ValidationChain_1.ValidationChain(value);
20+
var FormValidator_1 = require("./FormValidator");
21+
Object.defineProperty(exports, "FormValidator", { enumerable: true, get: function () { return FormValidator_1.FormValidator; } });
22+
Object.defineProperty(exports, "formValidator", { enumerable: true, get: function () { return FormValidator_1.formValidator; } });
23+
var ValidationChain_1 = require("./ValidationChain");
24+
Object.defineProperty(exports, "ValidationChain", { enumerable: true, get: function () { return ValidationChain_1.ValidationChain; } });
25+
const ValidationChain_2 = require("./ValidationChain");
26+
const validate = (value) => new ValidationChain_2.ValidationChain(value);
2427
exports.validate = validate;

dist/validators/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './usernameValidator';
88
export * from './ipAddressValidator';
99
export * from './validateNumber';
1010
export * from './validateFile';
11+
export * from './validateNumber';

dist/validators/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ __exportStar(require("./usernameValidator"), exports);
2424
__exportStar(require("./ipAddressValidator"), exports);
2525
__exportStar(require("./validateNumber"), exports);
2626
__exportStar(require("./validateFile"), exports);
27+
__exportStar(require("./validateNumber"), exports);

dist/validators/validateFile.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@ exports.validateFile = validateFile;
88
* @returns boolean - True if the file is valid
99
*/
1010
function validateFile(file, options = {}) {
11+
// Input validation
1112
if (!(file instanceof File))
1213
return false;
13-
if (options.maxSize && file.size > options.maxSize)
14-
return false;
15-
if (options.allowedTypes && !options.allowedTypes.includes(file.type))
16-
return false;
14+
// Validate file size if maxSize is specified
15+
if (typeof options.maxSize === 'number') {
16+
if (file.size > options.maxSize) {
17+
return false;
18+
}
19+
}
20+
// Validate file type if allowedTypes is specified
21+
if (Array.isArray(options.allowedTypes) && options.allowedTypes.length > 0) {
22+
if (!options.allowedTypes.includes(file.type)) {
23+
return false;
24+
}
25+
}
1726
return true;
1827
}

dist/validators/validateNumber.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ exports.validateNumber = validateNumber;
77
* @returns boolean - True if the number is valid
88
*/
99
function validateNumber(value, options = {}) {
10+
if (!value || value.trim() === '')
11+
return false;
12+
// Convert to number and validate
1013
const num = Number(value);
1114
if (isNaN(num))
1215
return false;
13-
if (options.min !== undefined && num < options.min)
16+
// Check minimum if specified
17+
if (options.min !== undefined && num < options.min) {
1418
return false;
15-
if (options.max !== undefined && num > options.max)
19+
}
20+
// Check maximum if specified
21+
if (options.max !== undefined && num > options.max) {
1622
return false;
23+
}
1724
return true;
1825
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "form-validation-lib-js",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A comprehensive JavaScript form validation library",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/FormValidator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ export class FormValidator {
7878
}
7979
return errors;
8080
}
81-
}
81+
}
82+
83+
export const formValidator = new FormValidator();

0 commit comments

Comments
 (0)