From b6fa15a7e00337815836f2f7bbfca32f7c466b7c Mon Sep 17 00:00:00 2001 From: Vahn Gomes Date: Tue, 21 Nov 2023 11:57:18 -0500 Subject: [PATCH] Add options for tracking passed fields and strict mode --- __tests__/validator.test.js | 45 +++++++++++++++++++++++++++++++++++++ src/validator.js | 22 ++++++++++-------- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/__tests__/validator.test.js b/__tests__/validator.test.js index a31dbf1..cfe1901 100644 --- a/__tests__/validator.test.js +++ b/__tests__/validator.test.js @@ -237,3 +237,48 @@ describe("Validator - Regex Validation", () => { expect(validator.validate({ username: "John Doe!" })).resolves.toBe(false); }); }); + +describe("Validator - Options", () => { + it("Should track the passed fields", () => { + const rules = { + name: { type: "string", required: true }, + age: { type: "number", required: true }, + }; + const messages = { + name: { + type: "Name must be a string", + required: "Name is required", + }, + age: { + type: "Age must be a number", + required: "Age is required", + }, + }; + const options = { trackPassedFields: true }; + + const validator = new Validator(rules, messages, options); + validator.validate({ name: "John Doe", age: 20 }); + + // Contains { name: "John Doe", age: 20} + expect(validator.passed).toEqual({ name: "John Doe", age: 20 }); + }); + + it("Should give error for fields not in rules", () => { + const rules = { + name: { type: "string", required: true }, + }; + const messages = { + name: { + type: "Name must be a string", + required: "Name is required", + }, + }; + const options = { trackPassedFields: true, strictMode: true }; + + const validator = new Validator(rules, messages, options); + validator.validate({ name: "John Doe", age: 20 }); + + // Contains { name: "John Doe", age: 20} + expect(validator.errors).toEqual({ age: "Field is not allowed" }); + }); +}); diff --git a/src/validator.js b/src/validator.js index b2f0add..21ff86c 100644 --- a/src/validator.js +++ b/src/validator.js @@ -28,7 +28,7 @@ /** * @typedef {Object} Options - * @property {boolean} [trackPassed] - Whether to track fields that passed validation. + * @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. */ @@ -126,11 +126,11 @@ class Validator { throw new Error("The options should be an object."); } if ( - options.trackPassed !== undefined && - typeof options.trackPassed !== "boolean" + options.trackPassedFields !== undefined && + typeof options.trackPassedFields !== "boolean" ) { throw new Error( - `The 'trackPassed' property for options must be of type boolean.` + `The 'trackPassedFields' property for options must be of type boolean.` ); } } @@ -369,17 +369,21 @@ class Validator { isValid = false; } } + } - // Strict mode check - if (this.options.strictMode && !(key in input)) { - this.errors[key] = "Field is not allowed"; - isValid = false; + // If strict mode is enabled, check for fields that are not defined in rules + if (this.options.strictMode) { + for (const key in input) { + if (!this.rules[key]) { + this.errors[key] = "Field is not allowed"; + isValid = false; + } } } // Iterate over the fields after validation checks for (const key in this.rules) { - if (!this.errors[key] && this.options.trackPassed) { + if (!this.errors[key] && this.options.trackPassedFields) { this.passed[key] = input[key]; } }