Skip to content

Commit

Permalink
Add ability to get skipped attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
apih committed Jul 14, 2024
1 parent b58f400 commit 3e82725
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class Validator {
#checkers;
#replacers;
#errors;
#skippedAttributes = [];

#implicitAttributes = {};
#stopOnFirstFailure = false;
Expand Down Expand Up @@ -241,11 +242,13 @@ export default class Validator {
this.#errors = new ErrorBag();

const tasks = [];
const skippedAttributes = [];

for (const [attribute, rules] of Object.entries(this.#rules)) {
let value = this.getValue(attribute);

if (Object.hasOwn(rules, 'sometimes') && typeof value === 'undefined') {
skippedAttributes.push(attribute);
continue;
}

Expand All @@ -255,14 +258,12 @@ export default class Validator {
let noError = true;

for (const [rule, parameters] of Object.entries(rules)) {
if (rule === '') {
continue;
}

if (
!Validator.#implicitRules.includes(rule) &&
(typeof value === 'undefined' || (typeof value === 'string' && value.trim() === '') || (isNullable && value === null))
rule === '' ||
(!Validator.#implicitRules.includes(rule) &&
(typeof value === 'undefined' || (typeof value === 'string' && value.trim() === '') || (isNullable && value === null)))
) {
skippedAttributes.push(attribute);
continue;
}

Expand Down Expand Up @@ -310,6 +311,8 @@ export default class Validator {
this.#errors.sortByKeys(Object.keys(this.#rules));
}

this.#skippedAttributes = skippedAttributes.filter((value, index, array) => array.indexOf(value) === index);

return this.#errors;
}

Expand Down Expand Up @@ -483,4 +486,8 @@ export default class Validator {
errors() {
return this.#errors;
}

skippedAttributes() {
return this.#skippedAttributes;
}
}

0 comments on commit 3e82725

Please sign in to comment.