Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
addError /*:: > */ (
key /*: string */,
error /*: T */
) /*: T */ {
// Construct new `Err` instance.
let newError /*: Err */;
if (isObject(error)) {
let errorLike /*: ErrLike<*> */ = (error /*: any */);
assert('Error must have value.', errorLike.hasOwnProperty('value'));
assert('Error must have validation.', errorLike.hasOwnProperty('validation'));
newError = new Err(errorLike.value, errorLike.validation);
} else {
let validation /*: ValidationErr */ = (error /*: any */);
newError = new Err(get(this, key), validation);
}
// Remove `key` from changes map.
let c = (this /*: ChangesetDef */);
// Add `key` to errors map.
let errors /*: Errors */ = get(this, ERRORS);
setNestedProperty(errors, key, newError);
c.notifyPropertyChange(ERRORS);
key.split('.').forEach((_part, i, allParts) => {
if (i < allParts.length - 1) {
let path = allParts.slice(0, i+1).join('.');
let msg = `Path ${path} leading up to ${key} must be an Object if specified.`;
assert(msg, isObject(obj[path]) || isBlank(obj[path]));
}
});
});
return keys(obj).map(key => {
let value = transform(obj[key]);
if (flattenObjects && isObject(value)) {
return assign({ key }, value);
}
return { key, value };
})
}).readOnly();
prepare(
prepareChangesFn /*: ({ [string]: mixed }) => ({ [string]: mixed }) */
) /*: ChangesetDef */ {
let changes /*: { [string]: mixed } */ = get(this, '_bareChanges');
let preparedChanges = prepareChangesFn(changes);
assert('Callback to `changeset.prepare` must return an object', isObject(preparedChanges));
validateNestedObj('preparedChanges', preparedChanges);
let newChanges /*: Changes */ = keys(preparedChanges).reduce((newObj, key) => {
newObj[key] = new Change(preparedChanges[key]);
return newObj;
}, {});
set(this, CHANGES, newChanges);
return this;
},