Skip to content

Commit

Permalink
Merge pull request #46 from tiaanduplessis/master
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybotha authored Aug 6, 2020
2 parents b363be7 + 2954edf commit e53031c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/create-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export const createForm = (config) => {
isValidating.set(true);
return Promise.resolve()
.then(() => validateFn({[field]: value}))
.then((errs) => util.update(errors, field, errs[field]))
.then((errs) =>
util.update(errors, field, !util.isNullish(errs) ? errs[field] : ''),
)
.finally(() => {
isValidating.set(false);
});
Expand Down
7 changes: 6 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ function cloneDeep(object) {
return JSON.parse(JSON.stringify(object));
}

function isNullish(value) {
return value === undefined || value === null;
}

function isEmpty(object) {
return Object.keys(object).length <= 0;
return isNullish(object) || Object.keys(object).length <= 0;
}

function getValues(object) {
Expand Down Expand Up @@ -127,4 +131,5 @@ export const util = {
reach,
subscribeOnce,
update,
isNullish,
};
23 changes: 23 additions & 0 deletions test/lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ describe("createForm", () => {
.then(errors => expect(errors.email).toBe("this email is invalid"))
.then(done);
});

it("assigns empty string to field if validateFn returns undefined", done => {
const value = "[email protected]";
const event = {
target: {
name: "email",
value
}
};
const instance = createForm({
initialValues: {
email: ""
},
validate: values => undefined,
onSubmit: values => console.log(values)
});

instance
.handleChange(event)
.then(() => subscribeOnce(instance.errors))
.then(errors => expect(errors.email).toBe(""))
.then(done);
})
});

describe("handleSubmit", () => {
Expand Down

0 comments on commit e53031c

Please sign in to comment.