Skip to content

Commit

Permalink
fix: base auth / upload fields no longer cause validation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Mar 29, 2021
1 parent 3b0f3ec commit 23e1fc3
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions src/admin/components/forms/Form/buildStateFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,87 +36,91 @@ const buildStateFromSchema = async (fieldSchema: FieldSchema[], fullData: Data =
const iterateFields = (fields: FieldSchema[], data: Data, path = '') => fields.reduce((state, field) => {
let initialData = data;

if (field.name && field.defaultValue && typeof initialData?.[field.name] === 'undefined') {
initialData = { [field.name]: field.defaultValue };
}

if (field.name) {
if (field.type === 'relationship' && initialData?.[field.name] === null) {
initialData[field.name] = 'null';
if (!field?.admin?.disabled) {
if (field.name && field.defaultValue && typeof initialData?.[field.name] === 'undefined') {
initialData = { [field.name]: field.defaultValue };
}

if (field.type === 'array' || field.type === 'blocks') {
if (Array.isArray(initialData?.[field.name])) {
const rows = initialData[field.name] as Data[];

if (field.type === 'array') {
return {
...state,
...rows.reduce((rowState, row, i) => ({
...rowState,
...iterateFields(field.fields, row, `${path}${field.name}.${i}.`),
}), {}),
};
}
if (field.name) {
if (field.type === 'relationship' && initialData?.[field.name] === null) {
initialData[field.name] = 'null';
}

if (field.type === 'blocks') {
return {
...state,
...rows.reduce((rowState, row, i) => {
const block = field.blocks.find((blockType) => blockType.slug === row.blockType);
const rowPath = `${path}${field.name}.${i}.`;
if (field.type === 'array' || field.type === 'blocks') {
if (Array.isArray(initialData?.[field.name])) {
const rows = initialData[field.name] as Data[];

return {
if (field.type === 'array') {
return {
...state,
...rows.reduce((rowState, row, i) => ({
...rowState,
[`${rowPath}blockType`]: {
value: row.blockType,
initialValue: row.blockType,
valid: true,
},
[`${rowPath}blockName`]: {
value: row.blockName,
initialValue: row.blockName,
valid: true,
},
...(block?.fields ? iterateFields(block.fields, row, rowPath) : {}),
};
}, {}),
};
...iterateFields(field.fields, row, `${path}${field.name}.${i}.`),
}), {}),
};
}

if (field.type === 'blocks') {
return {
...state,
...rows.reduce((rowState, row, i) => {
const block = field.blocks.find((blockType) => blockType.slug === row.blockType);
const rowPath = `${path}${field.name}.${i}.`;

return {
...rowState,
[`${rowPath}blockType`]: {
value: row.blockType,
initialValue: row.blockType,
valid: true,
},
[`${rowPath}blockName`]: {
value: row.blockName,
initialValue: row.blockName,
valid: true,
},
...(block?.fields ? iterateFields(block.fields, row, rowPath) : {}),
};
}, {}),
};
}
}

return state;
}

return state;
}
// Handle non-array-based nested fields (group, etc)
if (field.type === 'group') {
const subFieldData = initialData?.[field.name] as Data;

// Handle non-array-based nested fields (group, etc)
if (field.type === 'group') {
const subFieldData = initialData?.[field.name] as Data;
return {
...state,
...iterateFields(field.fields, subFieldData, `${path}${field.name}.`),
};
}

return {
...state,
...iterateFields(field.fields, subFieldData, `${path}${field.name}.`),
[`${path}${field.name}`]: structureFieldState(field, data),
};
}

return {
...state,
[`${path}${field.name}`]: structureFieldState(field, data),
};
}
// Handle field types that do not use names (row, etc)
if (field.type === 'row') {
return {
...state,
...iterateFields(field.fields, data, path),
};
}

// Handle field types that do not use names (row, etc)
if (field.type === 'row') {
// Handle normal fields
return {
...state,
...iterateFields(field.fields, data, path),
[`${path}${field.name}`]: structureFieldState(field, data),
};
}

// Handle normal fields
return {
...state,
[`${path}${field.name}`]: structureFieldState(field, data),
};
return state;
}, {});

const resultingState = iterateFields(fieldSchema, fullData);
Expand Down

0 comments on commit 23e1fc3

Please sign in to comment.