Validator.register: TypeError: Cannot set properties of undefined
What version of this package are you using?
"validatorjs": "^3.22.1",
What operating system, Node.js, and npm version?
MacOs 12.4 Node v18.13.0 Yarn 1.22.19
I'm using Vite for the build.
What happened?
I have following code:
import * as Validator from 'validatorjs'
Validator.register('amount', value => value.match(/^-?[0-9.,]*$/), 'The :attribute must be a number.');
This gives me error on the Validator.register line above:
Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'amount')
at Object._setRuleMessage (lang.js:36:34)
at Validator.register (validator.js:604:8)
at policy-validation.js:40:11
_
In the source code it errors on line:
this.messages[lang][attribute] = message;
So probably means this.messages[lang] was never set.
What did you expect to happen?
No errors.
Are you willing to submit a pull request to fix this bug?
Maybe if it's simple.
This edit fixes my issue:
// lang.js
...
_setRuleMessage: function(lang, attribute, message) {
this._load(lang);
if (message === undefined) {
message = this.messages[lang].def;
}
// Added this line
if (typeof this.messages[lang] === 'undefined') {
this.messages[lang] = {}
}
this.messages[lang][attribute] = message;
},
....
I have this issue as well. seems your fix does the trick! hopefully it can be patched in soon
I'm facing the same issue, this workaround works fine for me:
import { defineConfig } from 'vite';
export default defineConfig({
resolve: {
alias: {
validatorjs: 'validatorjs/dist/validator.js',
},
},
});
Same issue here,
undefined ERROR Uncaught Exception {"errorType":"TypeError","errorMessage":"Cannot set properties of undefined (setting 'notEmptyString')","stack":["TypeError: Cannot set properties of undefined (setting 'notEmptyString')"," at Object._setRuleMessage (file:REDACTED**)"," at Validator.registerImplicit (file:REDACTED**)"," at file:REDACTED**" at ModuleJob.run (file:REDACTED**)"]}
This is when I build this using esbuild and try to run it,
I have this piece of code that fails
import ValidatorJS from "validatorjs"; ValidatorJS.registerImplicit("notEmptyString", validateEmptyString, "The :attribute can not be empty string");
version - "3.22.1"
debugged the code and this this.messages[lang][attribute] = message;fails atthis.messages[lang] being undefined;
undefined ERROR Error: Cannot find module './lang/en' at
_load: function(lang) { if (!this.messages[lang]) { try { var rawMessages = require_method("./lang/" + lang); this._set(lang, rawMessages); } catch (e) { console.error(e, 'some error in load') - // this is where the error comes from } } },
this problem still persists in this library.
same here, issue still persists