Skip to content

Commit

Permalink
Merge pull request #4 from zamarawka/feat/string-validation
Browse files Browse the repository at this point in the history
feat: add string length validation rules
  • Loading branch information
zamarawka authored Mar 5, 2024
2 parents 8d1cec5 + 6d0f713 commit 8b3e986
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ export const max =
}
};

export const minLength =
(min: number): CheckerFn<string> =>
(t, val) => {
if (val.length < min) {
return t('minLength', { min });
}
};

export const maxLength =
(max: number): CheckerFn<string> =>
(t, val) => {
if (val.length > max) {
return t('maxLength', { max });
}
};

export const email: CheckerFn<string> = (t, val) => {
// eslint-disable-next-line max-len
const emailRE =
Expand Down
24 changes: 24 additions & 0 deletions test/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
different,
min,
max,
minLength,
maxLength,
email,
accepted,
} from '../src';
Expand Down Expand Up @@ -174,6 +176,28 @@ describe('Rule: max', () => {
});
});

describe('Rule: minLength', () => {
it('Error case', () => {
expect(minLength(6)(t, '1234')).toBeDefined();
});

it('Valid case', () => {
expect(minLength(3)(t, '1234')).toBeUndefined();
expect(minLength(4)(t, '1234')).toBeUndefined();
});
});

describe('Rule: maxLength', () => {
it('Error case', () => {
expect(maxLength(3)(t, '1234')).toBeDefined();
});

it('Valid case', () => {
expect(maxLength(6)(t, '123456')).toBeUndefined();
expect(maxLength(4)(t, '1234')).toBeUndefined();
});
});

describe('Rule: email', () => {
it('Error case', () => {
expect(email(t, 'not email value')).toBeDefined();
Expand Down

0 comments on commit 8b3e986

Please sign in to comment.