Skip to content

Vite + React - leads to undefined error #462

Description

@gregfenton

What version of this package are you using?
v 3.22.1

What operating system, Node.js, and npm version?

  • MacOS: Ventura 13.0.1 (22A400)
  • node: v16.16.0
  • npm: 8.11.0

What happened?
This is likely exactly the same issue as #455

REPRO STEPS (approx. 5 minutes or less):

  1. npm create vite@latest my-vite-react-app -- --template react
  2. cd my-vite-react-app
  3. npm install
  4. npm install validatorjs
  5. replace the contents of src/App.jsx with the following:
    import { useState } from 'react';
    import './App.css';
    import Validator from 'validatorjs';
    
    const rules = { city: 'required', province: 'required' };
    const goodData = { city: 'Toronto', province: 'Ontario' };
    const badData = { city: undefined, province: 'Ontario' };
    
    export const App = () => {
      const [isValid, setIsValid] = useState();
    
      const doValid = () => setIsValid(new Validator(goodData, rules).passes());
      const doInvalid = () => setIsValid(new Validator(badData, rules).passes());
    
      return (
        <div className='App'>
          <h2>Is valid: {isValid ? 'true' : 'false'} </h2>
          <div>
            <button onClick={doValid}> VALID </button>{' '}
            <button onClick={doInvalid}> INVALID </button>
          </div>
        </div>
      );
    };
  6. npm run dev
  7. in your browser, click the VALID button and see that "Is valid: true` displays
  8. click INVALID button and check the browser's JS console. You will see the same exception as reported initially.

What did you expect to happen?

EXPECTED: Both calls to Validator.passes() run without exceptions being thrown, and that the INVALID case returns an appropriate validation message.

ACTUAL: an exception is thrown in the INVALID case and no validation message is available.

Are you willing to submit a pull request to fix this bug?

Yes if someone suggests a valid solution.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions