Skip to content

Commit

Permalink
Add options for tracking passed fields and strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Codycody31 committed Nov 21, 2023
1 parent f8f4d81 commit b6fa15a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
45 changes: 45 additions & 0 deletions __tests__/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
});
});
22 changes: 13 additions & 9 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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.`
);
}
}
Expand Down Expand Up @@ -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];
}
}
Expand Down

0 comments on commit b6fa15a

Please sign in to comment.