Skip to content

Commit

Permalink
add callback validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
unexge committed Apr 23, 2017
1 parent 1bc9353 commit 3a591ed
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type ValidatorResult = null | Errors;
| pattern | `pattern: RegExp` |
| email | - |
| equalToControl | `controlName: string` |
| callback | `(value: any, control: Control) => boolean` |

Options are passed via props
```jsx
Expand Down
35 changes: 35 additions & 0 deletions __tests__/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,38 @@ test('onChange callback', () => {
bar: 'qux'
});
});

test('callback validator', () => {
const wrapper = mount(
<Form>
{ _ => (
<span>
<Control name="foo" callback={(value: string) => value !== 'qux'}>
{ control => (
<input
type="text" value={control.value}
onChange={event => control.onChange(event.target.value)}
/>
) }
</Control>
</span>
) }
</Form>
);

const instance = wrapper.instance() as Form;

instance.setValue('foo', 'foobar');

expect(instance.errors)
.toEqual({
foo: {},
});

instance.setValue('foo', 'qux');

expect(instance.errors)
.toEqual({
foo: { callback: true },
});
});
16 changes: 16 additions & 0 deletions __tests__/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ test('equalToControl', () => {
expect(Validators.get('equalToControl')('bar', 'foo', mockControl as any))
.toBeNull();
});

test('callback', () => {
const mockControl = {
get form() {
return {
getValue() { return 'bar'; }
}
}
};

expect(Validators.get('callback')('bar', (value: any) => value !== 'bar', mockControl as any))
.toEqual({ callback: true });

expect(Validators.get('callback')('baz', (value: any) => value !== 'bar', mockControl as any))
.toBeNull();
});
2 changes: 1 addition & 1 deletion src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function isValidationRulesChanged(
}

for (const rule of oldRules) {
if (currentProps[rule] !== nextProps[rule]) {
if ('function' !== typeof currentProps[rule] && currentProps[rule] !== nextProps[rule]) {
return true;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ validators.set('equalToControl', (val: any, controlName: string, control: Contro
return val === control.form.getValue(controlName) ? null : { equalToControl: true };
});

validators.set('callback', (val: any, fn: (value: any, control: Control) => boolean, control: Control) => {
if (isEmpty(val)) {
return null;
}

return fn(val, control) ? null : { callback: true };
});

export default {
delete(v: string) { return validators.delete(v); },
add(v: string, fn: Validator) { return validators.set(v, fn); },
Expand Down

0 comments on commit 3a591ed

Please sign in to comment.