mdestafadilah
mdestafadilah

Reputation: 11

Added 2 Schema in Yup Validation React-Hook-Form

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)

enter image description here

any ideas and answers are greatly appreciated.

THANKS SO!

Upvotes: 0

Views: 3286

Answers (1)

Denis Lee
Denis Lee

Reputation: 21

you can use array reduce.

const shapes = upload.reduce((acc, curr) => {
    acc[curr.kode] = yup.string().required('Diperlukan');
    return acc
}, {})

Upvotes: 2

Related Questions