import { Try, failure } from '@oats-ts/try' import { IssueTypes, ValidatorConfig } from '@oats-ts/validators' import { Primitive, ValueParser } from '../types' import { identityParser } from './identityParser' type NumberParserFn = { (parser: ValueParser): ValueParser (): ValueParser } export const numberParser: NumberParserFn = (parser: ValueParser = identityParser): ValueParser => (value: string, name: string, path: string, config: ValidatorConfig): Try => { if (typeof value !== 'string') { return failure([ { message: `should not be ${value}`, path, severity: 'error', type: IssueTypes.type, }, ]) } const numValue = Number(value) if (value.length === 0 || isNaN(numValue)) { return failure([ { message: `should be a number, but was ${value}`, path, severity: 'error', type: IssueTypes.value, }, ]) } return parser(numValue, name, path, config) }