import { Try, failure } from '@oats-ts/try' import { IssueTypes, ValidatorConfig } from '@oats-ts/validators' import { Primitive, ValueParser } from '../types' import { identityParser } from './identityParser' type BooleanParserFn = { (parser: ValueParser): ValueParser (): ValueParser } export const booleanParser: BooleanParserFn = (parser: ValueParser = identityParser): ValueParser => (value: string, name: string, path: string, config: ValidatorConfig): Try => { if (value !== 'true' && value !== 'false') { return failure([ { message: `should be a boolean ("true" or "false")`, path, severity: 'error', type: IssueTypes.value, }, ]) } const boolValue = value === 'true' return parser(boolValue, name, path, config) }