Skip to content

Commit aa24ca2

Browse files
committed
FIX: Improve type checking for Date and Number
- Added a check to `isOfType` to correctly validate `Date` and `Number` types when the value is `NaN`. This ensures that `NaN` values are not incorrectly accepted as valid `Date` or `Number` types during validation. - Updated the version and description in `package.json`.
1 parent 497ecb0 commit aa24ca2

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎base.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ export default class Base {
465465
}
466466
}
467467
const isOfType = () => {
468-
return (unionTypes && unionTypes.some((t) => value.constructor === t)) || value.constructor === conf.type;
468+
return (unionTypes && unionTypes.some((t) => value.constructor === t)) ||
469+
value.constructor === conf.type ||
470+
((conf.type === Date || conf.type === Number) && !isNaN(value));
469471
};
470472
if (!isOfType()) {
471473
// Attempt to coerce the value to the correct type if possible. Valuable for date strings from a json for example
@@ -482,7 +484,7 @@ export default class Base {
482484
return coerced;
483485
}
484486
})();
485-
if (!isOfType() || isNaN(value))
487+
if (!isOfType())
486488
throw buildError(TypeValidationError, `Invalid type at '${path}', expected ${conf.type.name}, got ${value.constructor.name}`, this.constructor.name, path, conf.type, value, "INVALID_TYPE");
487489
}
488490
if ((conf.max !== undefined) && (value.length ? value.length > conf.max : value > conf.max))

‎base.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,9 @@ export default class Base {
591591
}
592592

593593
const isOfType = () => {
594-
return (unionTypes && unionTypes.some((t: any) => value.constructor === t)) || value.constructor === conf.type
594+
return (unionTypes && unionTypes.some((t: any) => value.constructor === t)) ||
595+
value.constructor === conf.type ||
596+
((conf.type === Date || conf.type === Number) && !isNaN(value))
595597
};
596598

597599
if (!isOfType()) {
@@ -608,7 +610,7 @@ export default class Base {
608610
}
609611
})();
610612

611-
if (!isOfType() || isNaN(value))
613+
if (!isOfType())
612614
throw buildError(TypeValidationError, `Invalid type at '${path}', expected ${conf.type.name}, got ${value.constructor.name}`, this.constructor.name, path, conf.type, value, "INVALID_TYPE");
613615
}
614616

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@bufferpunk/modelcore",
3-
"version": "1.6.0",
4-
"description": "A blazing fast, lightweight and Reactive Class-Based Object Modeling framework",
3+
"version": "1.6.1",
4+
"description": "A blazing fast, lightweight and Reactive Class-Based Object Modeling and data integrity framework",
55
"type": "module",
66
"main": "base.js",
77
"types": "base.d.ts",

0 commit comments

Comments
 (0)