Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async response validation #106

Open
xperiandri opened this issue Sep 30, 2022 · 0 comments
Open

Async response validation #106

xperiandri opened this issue Sep 30, 2022 · 0 comments

Comments

@xperiandri
Copy link
Contributor

If we need to read a content on validation like this

let validateResponse (content:HttpContent) = task {
    let! result =  content.ReadAsStringAsync()
    let! stream = content.ReadAsStreamAsync()
    stream.Seek(0L, SeekOrigin.Begin) |>ignore
    return not <| result.StartsWith("<!DOCTYPE html>")
}

then validator must be asynchronous like this

let validateAsync<'TContext, 'TSource>
    (predicate: 'TSource -> Task<bool>)
    (source: Pipeline<'TContext, 'TSource>)
    : Pipeline<'TContext, 'TSource> =
    fun next ->
        { new IAsyncNext<'TContext, 'TSource> with
            member _.OnSuccessAsync(ctx, value) =
                  task{
                      let! isValid = predicate value
                      if isValid then return! next.OnSuccessAsync(ctx, value)
                      else return! next.OnErrorAsync(ctx, SkipException "Validation failed")
                }

            member _.OnErrorAsync(ctx, exn) = next.OnErrorAsync(ctx, exn)
            member _.OnCancelAsync(ctx) = next.OnCancelAsync(ctx) }
        |> source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant