Skip to content

Commit

Permalink
Add test to ensure fields are not validation until touched
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Jan 8, 2020
1 parent 295de5c commit a263d31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-validify",
"version": "5.7.8",
"version": "5.7.9",
"description": "Form validation made easy",
"main": "dist/index.js",
"module": "lib/index.js",
Expand Down
6 changes: 5 additions & 1 deletion src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ type Props = {
valuesBlurred?: { [key: string]: boolean };
};

export default ({ values, rules, setErrors }: Props) => {
export default ({ values, rules, setErrors, valuesBlurred }: Props) => {
let newErrors = Object.keys(rules)
.filter(rule => {
if (valuesBlurred) return valuesBlurred[rule];
return true;
})
.map(field =>
rules[field].map(rule => {
let error = rule(get(values, field) || '', values);
Expand Down
11 changes: 11 additions & 0 deletions tests/form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,14 @@ test('Field validation runs on change, after submitting', async () => {
//ensure the validation shows up
expect(getByText('This field is required')).toBeInTheDocument();
});

test(`Untouched fields shouldn't validate unless submitted first`, () => {
let { queryByPlaceholderText, queryByText } = render(<TestForm />);
const email = queryByPlaceholderText('email');

fireEvent.focus(email);
fireEvent.change(email, { target: { value: '[email protected]' } });
fireEvent.blur(email);

expect(queryByText('This field is required')).toBeNull();
});

0 comments on commit a263d31

Please sign in to comment.