How to use the ember-changeset/utils/is-promise function in ember-changeset

To help you get started, we’ve selected a few ember-changeset examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github poteto / ember-changeset / addon / index.js View on Github external
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;
    },
github poteto / ember-changeset / addon / helpers / changeset.ts View on Github external
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);
}
github nucleartide / ember-changeset-hofs / addon / utils / or.js View on Github external
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
  }
github nucleartide / ember-changeset-hofs / addon / utils / and.js View on Github external
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
  }
github poteto / ember-changeset-validations / addon / index.js View on Github external
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];
  };
}