Skip to content

Commit

Permalink
Validation results contain only properties with actual results
Browse files Browse the repository at this point in the history
Empty result collections are no longer returned
  • Loading branch information
gius committed Feb 5, 2022
1 parent 76f5c77 commit 906ec39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/validation/__tests__/automaticEntityValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { testCoreValidatorFunctions, expectInvalid, expectValid } from "./testHe
beforeAll(() => {
configuration.valueValidators.set("required", value => ({ code: "required", isValid: !!value }));
configuration.valueValidators.set("mustBeJohn", value => ({ code: "mustBeJohn", isValid: value === "John" }));
configuration.valueValidators.set("mockValidation", value => undefined);
});

describe("AutomaticEntityValidator", () => {
Expand Down Expand Up @@ -93,4 +94,23 @@ describe("AutomaticEntityValidator", () => {
const validationErrors = validator.getResults("firstName");
expect(validationErrors[0].code).toBe("required");
});

describe("getAllResults", () => {
it("return only non-empty results", () => {
const target = observable({
firstName: "John",
});

const validator = new AutomaticEntityValidator(
target,
{
firstName: { mockValidation: true },
},
false
);

const validationErrors = Array.from(validator.getAllResults());
expect(validationErrors).toHaveLength(0);
});
});
});
4 changes: 2 additions & 2 deletions packages/validation/src/automaticEntityValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export default class AutomaticEntityValidator<TEntity = any> extends EntityValid
this.buildObservableResults(target, validationRules);
}

*getAllResults(): Iterable<[PropertyName<TEntity>, Iterable<ValidationResult>]> {
*getAllResults(): Iterable<[PropertyName<TEntity>, ValidationResult[]]> {
if (!this.isEnabled) {
return;
}

for (const propertyName of this._validatedProperties) {
const propertyResults = get(this._results, propertyName) as ValidationResult[] | undefined;
if (propertyResults) {
if (propertyResults?.length) {
yield [propertyName, propertyResults];
}
}
Expand Down

0 comments on commit 906ec39

Please sign in to comment.