question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

issue: "Illegal constructor" error when using the classValidatorResolver with a file input

See original GitHub issue

Version Number

7.15.4

Codesandbox/Expo snack

https://codesandbox.io/s/flamboyant-breeze-trm2p?file=/src/App.tsx

Steps to reproduce

  1. Create a simple form with a file input e.g.

<input type="file" {...register('test')} />

  1. Create a class for the validation schema e.g.
class ValidationSchema {
  test: any;
}
  1. Give the validation class to the useForm hook e.g.

useForm< ValidationSchema >({ resolver: classValidatorResolver(ValidationSchema), });

  1. Submit the form after selecting a .jpg file. You will see a “Illegal constructor” error

Expected behaviour

We shouldn’t get an error.

What browsers are you seeing the problem on?

Chrome, Safari

Relevant log output

Unhandled Runtime Error
TypeError: Illegal constructor

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
willdspdcommented, Jan 17, 2022

It works with the @williamdespard 's custom resolver because he is using Object.assign, we can’t use it in our case. It’s not compatible with nested data.

I’ve found a possible solution, I’m a beginner with class-validator, you may adapt the code below:


export class ValidationSchema {

  @Type(() => Object)

  @Transform(({ obj }: TransformFnParams) => {

    return obj.test;

  })

  test: File | null = null;

}

👉🏻 https://codesandbox.io/s/restless-wood-mw9mz

Thanks for this but it appears your solution doesn’t work in the sandbox provided. I get the error “Not enough arguments”

1reaction
jorisrecommented, Jan 17, 2022

It works with the @williamdespard 's custom resolver because he is using Object.assign, we can’t use it in our case. It’s not compatible with nested data.

I’ve found a possible solution, I’m a beginner with class-validator, you may adapt the code below:

export class ValidationSchema {
  @Type(() => Object)
  @Transform(({ obj }: TransformFnParams) => {
    return obj.test;
  })
  test: File | null = null;
}

👉🏻 https://codesandbox.io/s/restless-wood-mw9mz

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Illegal constructor when trying to create a new ...
If I now want to instantiate a ui component (defined and imported in /src/index.js) in view-matrix.js, I'm getting the following error. 1) ...
Read more >
Uncaught TypeError: Illegal constructor Error #1181 - GitHub
Hi, I am getting this error: Uncaught TypeError: Illegal constructor at n (prism.js:1) at prism.js:1 at prism.js:1 at Array.
Read more >
true` results in `Uncaught TypeError: Illegal constructor` - Reddit
I am trying to get my app to work as a web component. However, I keep getting this error: Uncaught TypeError: Illegal constructor....
Read more >
How to solve "Uncaught TypeError: Illegal constructor"
The Illegal constructor error is thrown when a class extends HTMLElement, but is instantiated like a regular class. Using classes that inherits from ......
Read more >
390457 - Please implement a FileList constructor - Monorail
since we can't add or remove files from a FileList in <input type=file> the only way to ... it throws a TypeError: Illegal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found