# JavaScript: How to Validate an ES6 Class Instance at Runtime
NicolasBrondinBernard
When TypeScript is no longer enough!

Article published on 18/11/2020, last updated on 10/08/2026
Data integrity is very important in JavaScript, since the language is dynamically typed, data that doesn't have the right type or that is missing can cause the program to stop, or worse, the program will run but not as intended.
And there, I know what you're thinking: "But that's exactly why we use TypeScript!"
And I would answer you:
Yes, but that doesn't solve all the problems, far from it.
If you want to understand in detail why, I invite you to read my introductory article on TypeScript.
But in summary, TypeScript "simply" allows you to avoid typing errors during the development phase of the program, but for all data that only exists in the program at runtime, such as file imports (csv, etc.), localStorage, or even the content of http requests, then TypeScript is no longer enough!
For more information, I invite you to read this article on the Oreilly blog.
typestack/class-validator
After some research and several tests, I finally found the "class-validator" library, which simply allows you to validate the content of an object, instantiated from a class.
For those who might wonder about the reliability of this library, "class-validator" is used internally by NestJS for its DTO mechanism.
To use it, you just need to run "npm install --save class-validator"
How it works
To prepare a class for validation, "class-validator" provides a set of decorators to add above each attribute of the class, as in the following example:
Note that all the examples in this article are written in TypeScript, but of course the library can be imported in plain JavaScript.
//index.ts
import { IsString, IsInt, IsOptional, validate } from 'class-validator';
class Person {
@IsString()
firstname: string
@IsString()
lastname:string
@IsInt()
@IsOptional()
age: number
constructor(csv_arr: string[]){
this.firstname = csv_arr[0];
this.lastname = csv_arr[1];
this.age = csv_arr[2] ? parseInt(csv_arr[2]) : undefined;
}
}
Here I took the example of a csv file that we would import, so we initialize the constructor with a simple array of strings (a line from the .csv file).
Then, all we need to do is instantiate all our people from the data coming from the file and call the validation method.
Note that to simplify the example, the file has been replaced by a static array, if you want to know how to import a csv file, I wrote a detailed article on the subject.
//index.ts
let csv : any[][] = [
["Marc", "Devier", 40],
["Alice", "Boran",],
["Jean",,52]
]
let persons: Person[] = []; // Contains only valid persons
for(let i:number = 0 ; i<csv.length ; i++){
let person: Person = new Person(csv[i]);
validate(person).then(errors => {
if(errors.length === 0){
persons.push(person);
console.log(person);
} else {
console.error(errors);
}
});
}
When this program runs, our "persons" array will therefore only contain valid instances of "Person", so data integrity is preserved.
Here is the information logged during the execution of the program:
Person { firstname: 'Marc', lastname: 'Devier', age: 40 }
Person { firstname: 'Alice', lastname: 'Boran', age: undefined }
[
ValidationError {
target: Person { firstname: 'Jean', lastname: undefined, age: 52 },
value: undefined,
property: 'lastname',
children: [],
constraints: { isString: 'lastname must be a string' }
}
]
We can clearly see that our second object was validated, even though its age is undefined since it was declared as optional, but on the contrary our third object is not valid because its last name is missing.
There are a huge number of different decorators, not just for the data type but also for its content (you can validate the length of a string, or even check that it's an email address, for example).
For more information, I invite you to check out the documentation directly on Github: https://github.com/typestack/class-validator
Alternative
Of course, there are other alternatives, such as JOI for example, which will allow you to validate not just a class instance, but any JSON object!
I hope you enjoyed this article, and see you soon on the blog!
No spam. Only free content, news, and ever more resources to level up your skills!
Join +1500 developers
No comments yet