import { Try, fluent, fromRecord } from '@oats-ts/try' import { DefaultConfig, ValidatorConfig } from '@oats-ts/validators' import { ParameterObject, PathValueDeserializers, ParameterValue, PathDeserializer } from '../types' import { parseRawPath } from './parseRawPath' export const createPathDeserializer = ( parameterNames: string[], regex: RegExp, deserializers: PathValueDeserializers, ): PathDeserializer => { return (input: string, path: string = 'path', config: ValidatorConfig = DefaultConfig): Try => { return fluent(parseRawPath(parameterNames, regex, input, path)) .flatMap((raw) => { const deserialized = Object.keys(deserializers).reduce( (acc: Record>, key: string) => { const deserializer = deserializers[key] acc[key] = deserializer(raw, key, config.append(path, key), config) return acc }, {}, ) return fromRecord(deserialized) as Try }) .toTry() } }