Skip to content

Commit 7693e92

Browse files
committed
feat: add number parser and validator
1 parent d58ef32 commit 7693e92

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TypeValidator } from './interfaces.validator';
2+
import { PropertyValidationError } from '../PropertyValidationError';
3+
4+
export const number = Symbol('number');
5+
type NumberValidator = TypeValidator<number, typeof number>;
6+
7+
export const NumberValidator: NumberValidator = {
8+
type: number,
9+
validateAndParse: function(value) {
10+
const result = +value;
11+
12+
if (isNaN(result)) {
13+
throw new PropertyValidationError({ value, type: this.type });
14+
} else {
15+
return result;
16+
}
17+
}
18+
};

0 commit comments

Comments
 (0)