Skip to content

Commit

Permalink
fix: allow empty rules (pipes) in string rules closes #2386 (#2389)
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Sep 29, 2019
1 parent 1eec0db commit 94d09d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/utils/index.ts
Expand Up @@ -183,6 +183,10 @@ export const normalizeRules = (rules: any) => {

return rules.split('|').reduce((prev, rule) => {
const parsedRule = parseRule(rule);
if (!parsedRule.name) {
return prev;
}

prev[parsedRule.name] = parsedRule.params;

return prev;
Expand Down
11 changes: 10 additions & 1 deletion tests/validate.spec.js
@@ -1,6 +1,6 @@
import { validate } from '@/validate';
import { extend } from '@/extend';
import { confirmed } from '@/rules';
import { confirmed, numeric } from '@/rules';

test('returns custom error messages passed in ValidationOptions', async () => {
extend('truthy', {
Expand Down Expand Up @@ -77,3 +77,12 @@ describe('target field placeholder', () => {
expect(result.errors[0]).toEqual('Foo must be the sum of Bar and Baz');
});
});

test('allows empty rules for the string format', async () => {
extend('numeric', numeric);
let result = await validate(100, '|numeric');
expect(result.valid).toBe(true);

result = await validate(100, '||||numeric');
expect(result.valid).toBe(true);
});

0 comments on commit 94d09d6

Please sign in to comment.