How to use ember-changeset - 10 common examples

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 CenterForOpenScience / ember-osf-web / lib / osf-components / addon / components / validated-model-form / component.ts View on Github external
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;
    }
github poteto / ember-changeset / addon / index.js View on Github external
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);
github poteto / ember-changeset / addon / index.js View on Github external
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;
    },
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 / index.js View on Github external
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);
github hummingbird-me / hummingbird-client / app / components / library-entry / state.js View on Github external
* 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(),
github poteto / ember-changeset / addon / index.js View on Github external
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;
    },
github poteto / ember-changeset / addon / index.js View on Github external
) /*: 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;
github kaliber5 / ember-bootstrap-changeset-validations / tests / dummy / app / controllers / application.js View on Github external
init() {
    this._super(...arguments);
    this.changeset = new Changeset(model, lookupValidator(validation), validation);
  },
github poteto / ember-changeset / addon / utils / computed / inflate.ts View on Github external
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]));
          }
        });
      });