diff --git a/src/index.ts b/src/index.ts index c79488a..3fa98d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -147,6 +147,22 @@ export const max = } }; +export const minLength = + (min: number): CheckerFn => + (t, val) => { + if (val.length < min) { + return t('minLength', { min }); + } + }; + +export const maxLength = + (max: number): CheckerFn => + (t, val) => { + if (val.length > max) { + return t('maxLength', { max }); + } + }; + export const email: CheckerFn = (t, val) => { // eslint-disable-next-line max-len const emailRE = diff --git a/test/rules.test.ts b/test/rules.test.ts index 1243c2b..4bd2db9 100644 --- a/test/rules.test.ts +++ b/test/rules.test.ts @@ -11,6 +11,8 @@ import { different, min, max, + minLength, + maxLength, email, accepted, } from '../src'; @@ -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();