sindresorhus / ow

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

Using ow across npm modules #168

Closed drobnikj closed 4 years ago

drobnikj commented 4 years ago

Hi, thank you for this amazing tool!

I found an issue that I can not use ow across npm modules. This is the code in my utils module

const ow = require('ow')
const checkWithtFriendlyError(value, argName, pattern) => {
    try {
        ow(value, argName, pattern);
    } catch (err) {
        // do some stuff
    }
    return true;
}
module.exports = { checkWithFriendlyError };

The other npm module

const ow = require('ow')
const { checkWithtFriendlyError } = require('my_utils_module');

const test = 'string';
checkWithtFriendlyError(test, 'test', ow.string)

The code ends with error TypeError: n[a.testSymbol] is not a function

Any idea how to avoid this issue?

drobnikj commented 4 years ago

I got it now. Because you can globally register validator using ow.create(), it doesn't make sense to mix two instances of ow.

I solve it to export ow from utils modules. Updated code: This is the code in utils module

const ow = require('ow')
const checkWithtFriendlyError(value, argName, pattern) => {
    try {
        ow(value, argName, pattern);
    } catch (err) {
        // do some stuff
    }
    return true;
}
module.exports = { 
  checkWithFriendlyError,
  check: ow,
};

The other npm module

const { checkWithtFriendlyError, check } = require('my_utils_module');

const test = 'string';
checkWithtFriendlyError(test, 'test', check.string)