Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
},
export function changeset(
[obj, validations]: [object | Promise, ValidatorFunc],
options: Config = {}
): Changeset | Promise {
if (isChangeset(obj)) {
return obj;
}
if (isPromise(obj)) {
return Promise.resolve(obj).then((resolved: any) => new Changeset(resolved, validations, {}, options));
}
return new Changeset(obj, validations, {}, options);
}
return (key, newValue, oldValue, changes, object) => {
let validation
for (let i = 0; i < validators.length; i++) {
validation = validators[i](key, newValue, oldValue, changes, object)
if (isPromise(validation)) {
let promise = validation.then(handleResult)
for (let j = i+1; j < validators.length; j++) {
promise = promise
.then(() => validators[j](key, newValue, oldValue, changes, object))
.then(handleResult)
}
return promise.catch(err => err)
}
if (isTrue(validation)) return true
}
return validation
}
return (key, newValue, oldValue, changes, object) => {
for (let i = 0; i < validators.length; i++) {
const validation = validators[i](key, newValue, oldValue, changes, object)
if (isPromise(validation)) {
let promise = validation.then(handleResult)
for (let j = i+1; j < validators.length; j++) {
promise = promise
.then(() => validators[j](key, newValue, oldValue, changes, object))
.then(handleResult)
}
return promise.catch(err => err)
}
if (notTrue(validation)) return validation
}
return true
}
return ({ key, newValue, oldValue, changes, content }) => {
let validator = validationMap[key];
if (isEmpty(validator)) {
return true;
}
if (isArray(validator)) {
return handleMultipleValidations(validator, { key, newValue, oldValue, changes, content });
}
let validation = validator(key, newValue, oldValue, changes, content);
return isPromise(validation) ? validation.then(wrapInArray) : [validation];
};
}