I have a class Car with some info of a car include plate, now I want validate the property plate, so I use a property decoratorin this way:
class Car{
@validate
public plate: string;
public model: string;
// extra info
constructor(plate: string, model: string){
this.plate= plate;
this.model = model;
}
toString(): string{
return `Car: ${this.plate} - ${this.model}`;
}
}
Then I have the following property decorator function:
function validate(target: any, propertyKey: string){
let value = target[propertyKey];
Object.defineProperty(target, propertyKey, {
get: () => value,
set: (newValue) => {
const pattern = /^[A-Z]{2}\s?[0-9]{3}\s?[A-Z]{2}$/;
if(pattern.test(newValue)){
value = newValue;
} else {
console.error('Non valid plate: ', newValue);
//value = undefined;
}
}
})
}
Now, if I test my code in this way:
const car1 = new Car('IT123UE', 'Car1Model');
console.log('1 - ', car1.toString());
const car2 = new Car('IT000000UE', 'Car2Model');
console.log('2 - ', car2.toString());
I get:
1 - Car: IT123UE - Car1Model
Non valid plate: IT000000UE
2 - Car: IT123UE - Car2Model <-- why print car1.plate if car2.plate is not valid and this is car2 object?
I have solved using value = undefined; in the else inside my validation function but I don't figure out why in my car2.plate I get car1.plate value. Another test, if I change the order:
const car2 = new Car('IT000000UE', 'Car2Model');
console.log('2 - ', car2.toString());
const car1 = new Car('IT123UE', 'Car1Model');
console.log('1 - ', car1.toString());
I get:
Non valid plate: IT000000UE
2 - Car: undefined - Car2Model <- now is undefinied
1 - Car: IT123UE - Car1Model
what I expected, but why didn't it work before?
I'm using TS 3.4.5 with VS Code, and in tsconfig I have
"target": "es6",
"experimentalDecorators": true,