Skip to content

Commit

Permalink
fix(isvalid store): fix isValid returning 'false' for valid nested ar…
Browse files Browse the repository at this point in the history
…rays

fix #115
  • Loading branch information
larrybotha committed May 1, 2021
1 parent 22f948a commit 9420ce7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ function isEmpty(object) {
}

function getValues(object) {
let result = [];
let results = [];

for (const [, value] of Object.entries(object)) {
result = [...result, ...(typeof value === 'object' ? getValues(value) : [value])];
const values = typeof value === 'object' ? getValues(value) : [value];
results = [...results, ...values];
}
return result;

return results;
}

// TODO: refactor this so as not to rely directly on yup's API
Expand Down
60 changes: 60 additions & 0 deletions test/specs/library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,66 @@ describe('createForm', () => {
})
.then(done);
});

it('is false for invalid arrays', async done => {
const validationSchema = yup
.array()
.of(yup.object().shape({x: yup.string().required()}).required());
const initialValues = [{x: ''}];
const formInstance = getInstance({validationSchema, initialValues});

formInstance
.handleSubmit()
.then(() => subscribeOnce(formInstance.isValid))
.then(isValid => expect(isValid).toBe(false))
.then(done);
});

it('is true for valid arrays', async done => {
const validationSchema = yup
.array()
.of(yup.object().shape({x: yup.string().required()}).required());
const initialValues = [{x: 'foo'}];
const formInstance = getInstance({validationSchema, initialValues});

formInstance
.handleSubmit()
.then(() => subscribeOnce(formInstance.isValid))
.then(isValid => expect(isValid).toBe(true))
.then(done);
});

it('is false for invalid nested arrays', async done => {
const validationSchema = yup.object().shape({
xs: yup
.array()
.of(yup.object().shape({x: yup.string().required()}).required()),
});
const initialValues = {xs: [{x: ''}]};
const formInstance = getInstance({validationSchema, initialValues});

formInstance
.handleSubmit()
.then(() => subscribeOnce(formInstance.isValid))
.then(isValid => expect(isValid).toBe(false))
.then(done);
});

it('is true for valid nested arrays', async done => {
const validationSchema = yup.object().shape({
xs: yup
.array()
.of(yup.object().shape({x: yup.string().required()}).required()),
});
const initialValues = {xs: [{x: 'bar'}]};
const formInstance = getInstance({validationSchema, initialValues});

formInstance
.handleSubmit()
.then(() => subscribeOnce(formInstance.isValid))
.then(isValid => expect(isValid).toBe(true))
.then(done);
});
});

describe('handleReset', () => {
Expand Down

0 comments on commit 9420ce7

Please sign in to comment.