Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator.register: TypeError: Cannot set properties of undefined #467

Open
vedmant opened this issue Jun 25, 2023 · 5 comments
Open

Validator.register: TypeError: Cannot set properties of undefined #467

vedmant opened this issue Jun 25, 2023 · 5 comments

Comments

@vedmant
Copy link

vedmant commented Jun 25, 2023

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.

@vedmant
Copy link
Author

vedmant commented Jun 25, 2023

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;
  },
....

@AlonBruQuali
Copy link

I have this issue as well. seems your fix does the trick!
hopefully it can be patched in soon

@ashalfarhan
Copy link

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',
    },
  },
});

@suvam-jaiswal
Copy link

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 } } },

@zzDave
Copy link

zzDave commented Dec 28, 2023

this problem still persists in this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants