Skip to content

Commit

Permalink
feat: add number parser and validator
Browse files Browse the repository at this point in the history
  • Loading branch information
emyann committed Jun 22, 2019
1 parent d58ef32 commit 7693e92
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/validation/validators/number.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TypeValidator } from './interfaces.validator';
import { PropertyValidationError } from '../PropertyValidationError';

export const number = Symbol('number');
type NumberValidator = TypeValidator<number, typeof number>;

export const NumberValidator: NumberValidator = {
type: number,
validateAndParse: function(value) {
const result = +value;

if (isNaN(result)) {
throw new PropertyValidationError({ value, type: this.type });
} else {
return result;
}
}
};

0 comments on commit 7693e92

Please sign in to comment.