Skip to content

Commit

Permalink
Add list rule
Browse files Browse the repository at this point in the history
  • Loading branch information
apih committed Mar 24, 2024
1 parent 7c2605c commit d2f97a9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Checkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export default class Checkers {
return true;
}

checkList(attribute, value, parameters) {
return Array.isArray(value);
}

checkBoolean(attribute, value, parameters) {
return [true, false, 0, 1, '0', '1'].includes(value);
}
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
ipv4: 'The :attribute field must be a valid IPv4 address.',
ipv6: 'The :attribute field must be a valid IPv6 address.',
json: 'The :attribute field must be a valid JSON string.',
list: 'The :attribute field must be a list.',
lowercase: 'The :attribute field must be lowercase.',
lt: {
array: 'The :attribute field must have less than :value items.',
Expand Down
1 change: 1 addition & 0 deletions src/locales/ms.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
ipv4: 'Medan :attribute mesti alamat IPv4 yang sah.',
ipv6: 'Medan :attribute mesti alamat IPv6 yang sah.',
json: 'Medan :attribute mesti rentetan JSON yang sah.',
list: 'Medan :attribute mesti berbentuk senarai.',
lowercase: 'Medan :attribute mesti dalam huruf kecil.',
lt: {
array: 'Medan :attribute mesti mempunyai kurang daripada :value item.',
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,26 @@ describe('Validation', () => {
});
});

describe(`Rule 'list'`, () => {
const rules = { field: 'list' };

it(`Passes when the field is a list`, async () => {
const validator = new Validator({ field: [1, 2, 3] }, rules);
assert(await validator.passes());
});

it(`Fails when the field is not a list`, async () => {
const validator = new Validator({ field: { a: 1, b: 2, c: 3} }, rules);
assert(await validator.fails());

validator.setData({ field: 123 });
assert(await validator.fails());

validator.setData({ field: 'abc' });
assert(await validator.fails());
});
});

describe(`Rule 'lowercase'`, () => {
const rules = { field: 'lowercase' };

Expand Down

0 comments on commit d2f97a9

Please sign in to comment.