@vtaits/react-hook-form-schema
Version:
Integration of react-hook-form and @vtaits/form-schema
53 lines • 1.19 kB
JavaScript
// src/utils/mapObjectToSchema.ts
function mapFiledToSchema(name, value) {
switch (typeof value) {
case "string":
return {
type: "input",
label: name
};
case "number":
return {
type: "input",
isNumber: true,
label: name
};
case "boolean":
return {
type: "checkbox",
checkboxLabel: name
};
case "object":
if (!value) {
throw new Error("Value can not be empty");
}
if (Array.isArray(value)) {
return {
type: "list",
label: name,
getBlockLabel: (index) => `${name} #${index + 1}`,
itemSchema: mapFiledToSchema(name, value[0])
};
}
return {
type: "set",
nested: true,
schemas: mapObjectToSchema(value)
};
default:
throw new Error(
"Only string, number, boolean, array and object are supported"
);
}
}
function mapObjectToSchema(obj) {
const res = {};
for (const [name, value] of Object.entries(obj)) {
res[name] = mapFiledToSchema(name, value);
}
return res;
}
export {
mapObjectToSchema
};
//# sourceMappingURL=utils.js.map