Skip to content

Commit 7346219

Browse files
committed
Added length rule
1 parent 1ef2e4e commit 7346219

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ Built in rules are:
3636
- `integer`
3737
- `decimal`
3838
- `money`
39+
- `length`
3940

4041
Decimal can be configured using arguments. e.g `decimal(1,2)` to ensure that (if a decimal point is given) then 1 to 2 digits follow it. `money` allows up to 2 decimal places, any more and it will be invalid.
4142

43+
Length can be configured using arguments. e.g `length(1, 100)` ensures a min length of 1 and max length of 100 characters. You can exclude either parameter to check only the min OR max, for example `length(,100)` ensures a max length of 100 characters.
44+
4245
## Adding rules
4346

4447
Custom rules can be added

src/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class Validator {
6666
integer: (key) => `The ${cleanKey(key)} field must be an integer value.`,
6767
decimal: (key) => `The ${cleanKey(key)} field must be a decimal value.`,
6868
money: (key) => `The ${cleanKey(key)} field must be a valid monetary value.`,
69+
length: (key, data, args = []) =>
70+
`The ${cleanKey(key)} field must be ${
71+
args[0] && args[0] !== '0' ? `greater than ${args[0]}` : ''
72+
}${
73+
args[0] && args[0] !== '0' && args[1] ? ' and ' : ''
74+
}${
75+
args[1] ? `less than ${args[1]}` : ''
76+
} in length.`,
6977
}
7078
this.#rules = {
7179
required: (key, data) => !!data[key].toString() ? undefined : this.#errorMessages.required(key, data),
@@ -77,6 +85,17 @@ class Validator {
7785
money: (key, data) => data[key] && !createDecimalRegex([1, 2]).test(data[key])
7886
? this.#errorMessages.money(key, data)
7987
: undefined,
88+
length: (key, data, args = []) => {
89+
const minLength = args[0] ? parseInt(args[0]) : 0;
90+
const maxLength = args[1] ? parseInt(args[1]) : Number.MAX_SAFE_INTEGER;
91+
const value = data[key];
92+
if (minLength > maxLength) {
93+
throw new Error(`Invalid validation defintion for ${key}; minLength must be smaller than maxLength`)
94+
}
95+
return value.length < minLength || value.length > maxLength
96+
? this.#errorMessages.length(key, data, args)
97+
: undefined;
98+
},
8099
};
81100
}
82101

src/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,26 @@ describe('integration tests', () => {
276276
expect(result.errors.value).toHaveLength(0);
277277
expect(result.errorCount).toBe(0);
278278
});
279+
280+
[
281+
{ value: 'abc', minLength: '4', expectedError: 'The value field must be greater than 4 in length.' },
282+
{ value: 'abcd', minLength: '4', expectedError: undefined },
283+
{ value: 'abcd', minLength: '0', maxLength: '3', expectedError: 'The value field must be less than 3 in length.' },
284+
{ value: 'abc', minLength: '0', maxLength: '3', expectedError: undefined },
285+
{ value: 'abcd', maxLength: '3', expectedError: 'The value field must be less than 3 in length.' },
286+
{ value: 'abc', maxLength: '3', expectedError: undefined },
287+
{ value: 'abcd', minLength: '1', maxLength: '3', expectedError: 'The value field must be greater than 1 and less than 3 in length.' },
288+
].forEach(({ value, expectedError, minLength = '', maxLength = '' }) =>
289+
test(`should validate "${value}" as ${expectedError ? 'a valid': 'an invalid'} length when size should be in the bounds (${minLength},${maxLength})`, async () => {
290+
const result = await validator.validate(
291+
{ value },
292+
{ value: `length(${minLength ?? ''},${maxLength ?? ''})`},
293+
);
294+
if (expectedError) {
295+
expect(result.errors.value).toContain(expectedError);
296+
}
297+
expect(result.errors.value).toHaveLength(expectedError ? 1 : 0);
298+
expect(result.errorCount).toBe(expectedError ? 1 : 0);
299+
}),
300+
);
279301
})

0 commit comments

Comments
 (0)