Skip to content

Commit e53031c

Browse files
author
Larry Botha
authored
Merge pull request #46 from tiaanduplessis/master
2 parents b363be7 + 2954edf commit e53031c

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

lib/create-form.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ export const createForm = (config) => {
8383
isValidating.set(true);
8484
return Promise.resolve()
8585
.then(() => validateFn({[field]: value}))
86-
.then((errs) => util.update(errors, field, errs[field]))
86+
.then((errs) =>
87+
util.update(errors, field, !util.isNullish(errs) ? errs[field] : ''),
88+
)
8789
.finally(() => {
8890
isValidating.set(false);
8991
});

lib/util.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ function cloneDeep(object) {
1717
return JSON.parse(JSON.stringify(object));
1818
}
1919

20+
function isNullish(value) {
21+
return value === undefined || value === null;
22+
}
23+
2024
function isEmpty(object) {
21-
return Object.keys(object).length <= 0;
25+
return isNullish(object) || Object.keys(object).length <= 0;
2226
}
2327

2428
function getValues(object) {
@@ -127,4 +131,5 @@ export const util = {
127131
reach,
128132
subscribeOnce,
129133
update,
134+
isNullish,
130135
};

test/lib.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,29 @@ describe("createForm", () => {
257257
.then(errors => expect(errors.email).toBe("this email is invalid"))
258258
.then(done);
259259
});
260+
261+
it("assigns empty string to field if validateFn returns undefined", done => {
262+
const value = "[email protected]";
263+
const event = {
264+
target: {
265+
name: "email",
266+
value
267+
}
268+
};
269+
const instance = createForm({
270+
initialValues: {
271+
email: ""
272+
},
273+
validate: values => undefined,
274+
onSubmit: values => console.log(values)
275+
});
276+
277+
instance
278+
.handleChange(event)
279+
.then(() => subscribeOnce(instance.errors))
280+
.then(errors => expect(errors.email).toBe(""))
281+
.then(done);
282+
})
260283
});
261284

262285
describe("handleSubmit", () => {

0 commit comments

Comments
 (0)