import { fold } from 'fp-ts/lib/Either' ; import { pipe } from 'fp-ts/lib/pipeable' ; import * as rt from 'io-ts' ; import { formatErrors , exactCheck } from '@kbn/securitysolution-io-ts-utils' ; import { RouteValidationFunction , RouteValidationResultFactory , RouteValidationError , } from '../../../../../../src/core/server' ; import { excess , GenericIntersectionC } from '../runtime_types' ; type RequestValidationResult < T > = { value : T ; error ? : undefined ; } | { value ? : undefined ; error : RouteValidationError ; } ; export const buildRouteValidation = < T extends rt . Mixed , A = rt . TypeOf < T > > ( schema ) : RouteValidationFunction < A > => ( inputValue , validationResult ) => pipe ( schema . decode ( inputValue ) , ( decoded ) => exactCheck ( inputValue , decoded ) , fold < rt . Errors , A , RequestValidationResult < A > > ( ( errors ) => validationResult . badRequest ( formatErrors ( errors ) . join ( ) ) , ( validatedInput ) => validationResult . ok ( validatedInput ) ) ) ; export const buildRouteValidationWithExcess = < T extends rt . InterfaceType < rt . Props > | GenericIntersectionC | rt . PartialType < rt . Props > , A = rt . TypeOf < T > > ( schema ) : RouteValidationFunction < A > => ( inputValue , validationResult ) => pipe ( excess ( schema ) . decode ( inputValue ) , fold < rt . Errors , A , RequestValidationResult < A > > ( ( errors ) => validationResult . badRequest ( formatErrors ( errors ) . join ( ) ) , ( validatedInput ) => validationResult . ok ( validatedInput ) ) ) ;