sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.8k stars 105 forks source link

Method ow.object.exactShape does not work in ow versions higher than 0.19.0 #196

Closed sergeyblohin closed 3 years ago

sergeyblohin commented 3 years ago

Justification Method ow.object.exactShape does not work in ow versions higher than 0.19.0

Steps for reproduce Install ow version 0.20.0 or 0.21.0

Code for reproduce

const ow = require('ow')

function main(foo) {
  ow(foo, ow.any(ow.string, ow.object.exactShape({
    main: {
      key: ow.string
    }
  })));
}

main('foo');

main(
  {
    main: {
      key: 'foo'
    }
  }
);
$ node index.js
/ow_test/index.js:4
  ow(foo, ow.any(ow.string, ow.object.exactShape({
                                      ^

TypeError: Cannot read property 'exactShape' of undefined
    at main (/ow_test/index.js:4:39)
    at Object.<anonymous> (/ow_test/index.js:11:1)
    at Module._compile (internal/modules/cjs/loader.js:1201:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
    at Module.load (internal/modules/cjs/loader.js:1050:32)
    at Function.Module._load (internal/modules/cjs/loader.js:938:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

But, if I use ow version 0.19.0, then all work correctly. What I do wrong?

sindresorhus commented 3 years ago

// @mmkal

mmkal commented 3 years ago

My changes in #194 went in as v0.21.0 (i.e. after this started happening).

@sergeyblohin Likely you just need to update for the breaking change in the import style: https://github.com/sindresorhus/ow/releases/tag/v0.20.0

CommonJS users needs to change their import:

-const ow = require('ow')
+const {default: ow} = require('ow')
sergeyblohin commented 3 years ago

@mmkal, Thank you! After changing require method from const ow to const {default: ow}, everything worked.

nnmrts commented 3 years ago

@sindresorhus This is also the case when using import ow from "ow", at least for me. I use Node.js 15, the newest version of ow and have a "type": "module" in my package.json. The imported package still is an object with default on it instead of the ow function directly. Please reopen this.

EDIT: All this when running my test code through node itself by node index.js though. This issue probably disappears when using rollup or something else for custom module resolution.

nnmrts commented 3 years ago

I made a fork here btw, converting the whole project to js: https://github.com/pumpncode/ow

With this I can import normally again. I also created a PR: #207

Thoughts @sindresorhus?