Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
toastManager: { addToast },
updateItem,
item: initialData,
} = this.props;
const fieldsObject = this.getFieldsObject();
const initialValues = getInitialValues(fieldsObject, initialData);
const currentValues = getCurrentValues(fieldsObject, item);
// Don't try to update anything that hasn't changed.
// This is particularly important for access control where a field
// may be `read: true, update: false`, so will appear in the item
// details, but is not editable, and would cause an error if a value
// was sent as part of the update query.
const data = omitBy(
currentValues,
path => !fieldsObject[path].hasChanged(initialValues, currentValues)
);
const fields = Object.values(omitBy(fieldsObject, path => !data.hasOwnProperty(path)));
// On the first pass through, there wont be any warnings, so we go ahead
// and check.
// On the second pass through, there _may_ be warnings, and by this point
// we know there are no errors (see the `validationErrors` check above),
// if so, we let the user force the update through anyway and hence skip
// this check.
// Later, on every change, we reset the warnings, so we know if things
// have changed since last time we checked.
if (!countArrays(validationWarnings)) {
const { errors, warnings } = await validateFields(fields, item, data);
// We then filter out the `undefined` results (they should return `null` or
// a value)
resolvedData = omitBy(resolvedData, key => typeof resolvedData[key] === 'undefined');
// Run the schema-level field hooks, passing in the results from the field
// type hooks
resolvedData = {
...resolvedData,
...(await this._mapToFields(this.fields.filter(field => field.hooks.resolveInput), field =>
field.hooks.resolveInput({ ...args, resolvedData })
)),
};
// And filter out the `undefined`s again.
resolvedData = omitBy(resolvedData, key => typeof resolvedData[key] === 'undefined');
if (this.hooks.resolveInput) {
// And run any list-level hook
resolvedData = await this.hooks.resolveInput({ ...args, resolvedData });
if (typeof resolvedData !== 'object') {
throw new Error(
`Expected ${
this.key
}.hooks.resolveInput() to return an object, but got a ${typeof resolvedData}: ${resolvedData}`
);
}
}
// Finally returning the amalgamated result of all the hooks.
return resolvedData;
}
const fieldsObject = this.getFieldsObject();
const initialValues = getInitialValues(fieldsObject, initialData);
const currentValues = getCurrentValues(fieldsObject, item);
// Don't try to update anything that hasn't changed.
// This is particularly important for access control where a field
// may be `read: true, update: false`, so will appear in the item
// details, but is not editable, and would cause an error if a value
// was sent as part of the update query.
const data = omitBy(
currentValues,
path => !fieldsObject[path].hasChanged(initialValues, currentValues)
);
const fields = Object.values(omitBy(fieldsObject, path => !data.hasOwnProperty(path)));
// On the first pass through, there wont be any warnings, so we go ahead
// and check.
// On the second pass through, there _may_ be warnings, and by this point
// we know there are no errors (see the `validationErrors` check above),
// if so, we let the user force the update through anyway and hence skip
// this check.
// Later, on every change, we reset the warnings, so we know if things
// have changed since last time we checked.
if (!countArrays(validationWarnings)) {
const { errors, warnings } = await validateFields(fields, item, data);
const totalErrors = countArrays(errors);
const totalWarnings = countArrays(warnings);
if (totalErrors + totalWarnings > 0) {
resolvedData,
existingItem,
context,
originalInput,
actions: mapKeys(this.hooksActions, hook => hook(context)),
operation,
};
// First we run the field type hooks
// NOTE: resolveInput is run on _every_ field, regardless if it has a value
// passed in or not
resolvedData = await this._mapToFields(this.fields, field => field.resolveInput(args));
// We then filter out the `undefined` results (they should return `null` or
// a value)
resolvedData = omitBy(resolvedData, key => typeof resolvedData[key] === 'undefined');
// Run the schema-level field hooks, passing in the results from the field
// type hooks
resolvedData = {
...resolvedData,
...(await this._mapToFields(this.fields.filter(field => field.hooks.resolveInput), field =>
field.hooks.resolveInput({ ...args, resolvedData })
)),
};
// And filter out the `undefined`s again.
resolvedData = omitBy(resolvedData, key => typeof resolvedData[key] === 'undefined');
if (this.hooks.resolveInput) {
// And run any list-level hook
resolvedData = await this.hooks.resolveInput({ ...args, resolvedData });
existingItem,
context,
originalInput,
actions: mapKeys(this.hooksActions, hook => hook(context)),
};
const fieldsWithoutValues = this.fields.filter(
field => typeof originalInput[field.path] === 'undefined'
);
const defaultValues = await this._mapToFields(fieldsWithoutValues, field =>
field.getDefaultValue(args)
);
return {
...omitBy(defaultValues, path => typeof defaultValues[path] === 'undefined'),
...originalInput,
};
}