Skip to content

Commit

Permalink
Making valid length validator pass for no value (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Ing-Simmons authored Jun 7, 2019
1 parent 0c9b3a0 commit a4921e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/validators/valid-length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const validLength = (options: ValidLengthOptions) => {
}

return createValidator<string>(s =>
(options.min === undefined || s.length >= options.min) && (options.max === undefined || s.length <= options.max)
!s
|| ((options.min === undefined || s.length >= options.min) && (options.max === undefined || s.length <= options.max))
, getMessage(options)
);
};
17 changes: 17 additions & 0 deletions test/validators/valid-length.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { validLength } from "../../src/validators/valid-length";
@TestFixture("ValidLength")
export class ValidLengthTests {

@TestCase(null)
@TestCase(undefined)
@TestCase("")
@Test("should pass for null, undefined or empty")
public shouldPassForNullUndefinedOrEmpty(value: string) {
const message = "Invalid length";

const validator = validLength({
min: 3,
max: 5,
message
});

const results = validator(value);
Expect(results).toBeAPass();
}

@TestCase(2, 10, "12345")
@TestCase(5, undefined, "12345")
@TestCase(undefined, 6, "123")
Expand Down

0 comments on commit a4921e5

Please sign in to comment.