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);
// Notify that `key` has changed.
c.notifyPropertyChange(key);
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);
// Notify that `key` has changed.
c.notifyPropertyChange(key);
// Return passed-in `error`.
return error;
},
let newErrors /*: Errors */ = keys(errors).reduce((newObj, key) => {
let e /*: ErrLike<*> */ = errors[key];
newObj[key] = new Err(e.value, e.validation);
return newObj;
}, {});
pushErrors(
key /*: string */,
...newErrors /*: Array */
) /*: ErrLike */ {
let errors /*: Errors */ = get(this, ERRORS);
let existingError /*: Err */ = errors[key] || new Err(null, []);
let validation /*: ValidationErr */ = existingError.validation;
let value /*: mixed */ = get(this, key);
if (!isArray(validation) && isPresent(validation)) {
let v /*: string */ = (existingError.validation /*: any */);
existingError.validation = [v];
}
let v /*: Array */ = (existingError.validation /*: any */);
validation = [...v, ...newErrors];
let c = (this /*: ChangesetDef */)
c.notifyPropertyChange(ERRORS);
c.notifyPropertyChange(key);
errors[key] = new Err(value, validation);
let validation /*: ValidationErr */ = existingError.validation;
let value /*: mixed */ = get(this, key);
if (!isArray(validation) && isPresent(validation)) {
let v /*: string */ = (existingError.validation /*: any */);
existingError.validation = [v];
}
let v /*: Array */ = (existingError.validation /*: any */);
validation = [...v, ...newErrors];
let c = (this /*: ChangesetDef */)
c.notifyPropertyChange(ERRORS);
c.notifyPropertyChange(key);
errors[key] = new Err(value, validation);
return { value, validation };
},