adopted-ember-addons / ember-cp-validations

Ember computed property based validations

Home Page:https://adopted-ember-addons.github.io/ember-cp-validations/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`Error: "EmberObject.create no longer supports.."` on Ember-4.12 addon - ONLY during embroider-safe scenario

Pixelik opened this issue · comments

Environment

  • Ember Version: ~4.12.0
  • Ember CLI Version: ~4.12.2
  • Ember CP Validations Version: ^5.0.0

Steps to Reproduce

I updated my Ember Addon (v1) from Ember 4.8 to Ember 4.12.

The addon is using ember-cp-validations v5.

This is what my package.json looks like:

  "dependencies": {
    ...,
    "ember-cp-validations": "^5.0.0",
    ...
  },
  "devDependencies": {
    ...,
    "@babel/core": "^7.22.1",
    "@babel/eslint-parser": "^7.21.8",
    "@babel/plugin-proposal-decorators": "^7.21.0",
    ...,
    "@ember-intl/cp-validations": "^5.0.0",
    ...,
    "@embroider/test-setup": "^3.0.0",
    ...,
    "ember-auto-import": "^2.6.3",
    ...,
    "ember-qunit": "^6.2.0",
    "ember-resolver": "^10.0.0",
    "ember-source": "~4.12.0",
    ...,
    "qunit": "^2.19.4",
    ...,
    "webpack": "^5.0.0
  },
  "peerDependencies": {
    "ember-source": "^4.8.0"
  },
  "resolutions": {
    "@embroider/macros": "^1.12.2"
  }

Everything works and all tests are passing, except when running the embroider-safe scenario where this error is thrown:

Error: Assertion Failed: EmberObject.create no longer supports defining computed properties. Define computed properties using extend() or reopen() before calling create().
    at assert (http://localhost:4200/assets/test-support.js:233:15)
    at initialize (http://localhost:4200/assets/vendor.js:30952:81)
    at Class.create (http://localhost:4200/assets/vendor.js:31314:9)
    at new Options (webpack://dummy/../rewritten-packages/ember-cp-validations.126a452a/-private/options.js?:39:26)
    at Class.buildOptions (webpack://dummy/../rewritten-packages/ember-cp-validations.126a452a/validators/base.js?:130:12)
    at Class.init (webpack://dummy/../rewritten-packages/ember-cp-validations.126a452a/validators/base.js?:110:78)
    at Class.superWrapper [as init] (http://localhost:4200/assets/vendor.js:23897:22)
    at initialize (http://localhost:4200/assets/vendor.js:30988:9)
    at Class.create (http://localhost:4200/assets/vendor.js:31312:9)
    at InternalFactoryManager.create (http://localhost:4200/assets/vendor.js:13726:25)

I understand that the Error itself EmberObject.create no longer supports defining computed properties. is a known issue, but I'm only getting it during the embroider-safe test scenario.
In the default scenario everything is working fine and all tests are passing.

Any help would be much appreciated 🙏

👀 If I change ember-cp-validations/addon/-private/options.js from:

export default class Options {
  constructor({ model, attribute, options = {} }) {
    const optionKeys = keys(options);
    const createParams = { [OPTION_KEYS]: optionKeys, model, attribute };

    // If any of the options is a CP, we need to create a custom class for it
    if (optionKeys.some((key) => isDescriptor(options[key]))) {
      return OptionsObject.extend(options).create(createParams);
    }

    return OptionsObject.create(createParams, options);
  }
}

to:

export default class Options {
  constructor({ model, attribute, options = {} }) {
    const optionKeys = keys(options);
    const createParams = { [OPTION_KEYS]: optionKeys, model, attribute };
    const someOptionsAreCPs = optionKeys.some((key) => {
      return isDescriptor(options[key]) || options[key]?.constructor?.name === 'AliasDecoratorImpl'
    });
    
    // If any of the options is a CP, we need to create a custom class for it
    if (someOptionsAreCPs) {
      return OptionsObject.extend(options).create(createParams);
    }

    return OptionsObject.create(createParams, options);
  }
}

the error is gone and everything works.

Is this a valid fix ? Should I open a PR ?

Bear in mind that I'm only having the Error when running the embroider-safe scenario and not the default scenario on Ember 4.12.

🤔