Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
useOptions = options;
}
const validationMap = model.validations.validatableAttributes.reduce(
(o: any, attr: string) => {
o[attr] = true; // eslint-disable-line no-param-reassign
return o;
},
{},
);
const validateFn: ValidatorFunc = async params => {
const { validations } = await model.validateAttribute(params.key, params.newValue);
return validations.isValid ? true : validations.message;
};
return new Changeset(model, validateFn, validationMap, useOptions) as ChangesetDef;
}
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;
},
key /*: string */,
value /*: T */
) /*: Promise | Promise> | T | ErrLike */ {
let c /*: ChangesetDef */ = this;
let content /*: Object */ = get(this, CONTENT);
let oldValue /*: mixed */ = get(content, key);
let validation /*: ValidationResult | Promise */ =
c._validate(key, value, oldValue);
let v /*: ValidationResult */ = (validation /*: any */);
c.trigger(BEFORE_VALIDATION_EVENT, key);
let result = c._setProperty(v, { key, value, oldValue });
// TODO: Address case when Promise is rejected.
if (isPromise(validation)) {
c._setIsValidating(key, true);
let v /*: Promise */ = (validation /*: any */);
return v.then(resolvedValidation => {
c._setIsValidating(key, false);
c.trigger(AFTER_VALIDATION_EVENT, key);
return c._setProperty(resolvedValidation, { key, value, oldValue });
});
}
c.trigger(AFTER_VALIDATION_EVENT, key);
return result;
},
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);
* perform(content) {
// Warning: semi-private e-c API
const owner = get(this, 'owner');
const libraryEntry = content || get(owner, 'libraryEntry');
// Send the task to the onUpdate function
get(owner, 'onUpdate')(libraryEntry, this);
try {
yield libraryEntry.save();
get(owner, 'queryCache').invalidateType('library-entry');
} catch (error) {
if (isChangeset(libraryEntry)) {
libraryEntry.rollback();
} else {
libraryEntry.rollbackAttributes();
}
throw error;
}
}
}).enqueue(),
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;
},
) /*: T | ErrLike */ {
let changes /*: Changes */ = get(this, CHANGES);
let isValid /*: boolean */ = validation === true
|| isArray(validation)
&& validation.length === 1
&& (validation /*: any */)[0] === true;
// Shorthand for `this`.
let c /*: ChangesetDef */ = this;
// Happy path: remove `key` from error map.
c._deleteKey(ERRORS, key);
// Happy path: update change map.
if (!isEqual(oldValue, value)) {
setNestedProperty(changes, key, new Change(value));
} else if (key in changes) {
c._deleteKey(CHANGES, key);
}
// Happy path: notify that `key` was added.
c.notifyPropertyChange(CHANGES);
c.notifyPropertyChange(key);
// Error case.
if (!isValid) {
let v /*: ValidationErr */ = (validation /*: any */);
return c.addError(key, { value, validation: v });
}
// Return new value.
return value;
init() {
this._super(...arguments);
this.changeset = new Changeset(model, lookupValidator(validation), validation);
},
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]));
}
});
});