Reputation: 11
I Have done read doc yup validation, searching all SO about yup event gogling, but i'have not clue. Here my code
const upload = [
{
"kode": "filelamaran",
"desc": "Surat Lamaran",
"id": 7
},
{
"kode": "filefoto",
"desc": "File Foto",
"id": 10
}
];
const shapesDua = {};
const shapes = Object.entries(upload).map((([key,value]) => { //console.log(value.kode);
let val = value.kode;
return shapesDua[val] = yup.string().required('Diperlukan');
}));
const schemaDua = yup.object().shape(shapes);
const schema = yup.object().shape({
nohp: yup.string().required('Nomor HP Wajib diisi bro..').matches(phoneRegExp, 'Nomor Handphone tidak valid bro..').min(9, "kependekan bro..").max(14, "kepanjangan bro.."), // source: https://stackoverflow.com/a/63317462
email: yup.string().required('Email Wajib diisi bro..'),
nomorktp: yup.string().required('KTP Wajib diisi bro..').test('len', 'Nomor KTP tidak
});
const gabungSchema = schema.concat(schemaDua);
console.log('skema: ', schema, 'skema gabungan:', gabungSchema);
with thats code i have it's produce object with number (not value.kode)
any ideas and answers are greatly appreciated.
THANKS SO!
Upvotes: 0
Views: 3286
Reputation: 21
you can use array reduce.
const shapes = upload.reduce((acc, curr) => {
acc[curr.kode] = yup.string().required('Diperlukan');
return acc
}, {})
Upvotes: 2