Giter VIP home page Giter VIP logo

Comments (9)

sz-piotr avatar sz-piotr commented on August 16, 2024

Yes that is correct. Currently there is no notion of an async sanitizer in the library, but a PR would be greatly appreciated. In the meantime all you really need to do is await the result of asCertificate and then unwrap it like so: https://github.com/EthWorks/restless/blob/master/sanitizers/src/cast.ts#L11-L16

from restless.

getlarge avatar getlarge commented on August 16, 2024

I'm ok to make a PR. Would that be fine to add an asyncCast function that takes a Promise<Sanitizer<T>> as input and returns Promise<T> ?

from restless.

sz-piotr avatar sz-piotr commented on August 16, 2024

That could also work, but it does not cover your use case. Your use case is a sanitizer that returns a promise, not a promise with a sanitizer.

from restless.

sz-piotr avatar sz-piotr commented on August 16, 2024

Hey @getlarge. I thought about your issue over the weekend and here is what I came up with:

import { Result, SanitizerFailure, CastError } from "@restless/sanitizers";

export type AsyncSanitizer<T> = (value: unknown, path: string) => Promise<Result<SanitizerFailure[], T>>

export async function asyncCast<T>(value: unknown, sanitizer: AsyncSanitizer<T>, message?: string): Promise<T> {
  const result = await sanitizer(value, '')
  if (Result.isOk(result)) {
    return result.ok
  } else {
    throw new CastError(result.error, message)
  }
}

You should be able to drop this code in your project and use it without issues.

If however you wanted a function that takes a Promise<Sanitizer<T> it becomes even easier:

import { Sanitizer, cast } from "@restless/sanitizers";

export async function asyncCast<T>(value: unknown, sanitizer: Promise<Sanitizer<T>>, message?: string): Promise<T> {
  return cast(value, await sanitizer, message)
}

from restless.

getlarge avatar getlarge commented on August 16, 2024

Hey @sz-piotr, sorry for the delay. You were right, the use case is a sanitizer that returns a promise.

Thanks a lot for your snippet and your feedback.
Do you intend to add the AsyncSanitizer type and asyncCast function in restless ?

from restless.

getlarge avatar getlarge commented on August 16, 2024

In fact my question was more to know if you would prefer a cast function that could take an async | sync sanitizer as 2nd argument or to separate async and sync sanitizer with another function and type.

from restless.

sz-piotr avatar sz-piotr commented on August 16, 2024

Here is my line of thinking. If async sanitizers were to be supported by default by the library this should be a bigger change, where all the other higher-order sanitizers like asArray would take either a sync or async sanitizer. In this scenario it makes sense that cast serves a dual purpose being sync or async depending on what it receives.

However since there are currently no async sanitizers in the library code I believe keeping the asyncCast function as separate from both the cast and the library itself makes sense.

from restless.

getlarge avatar getlarge commented on August 16, 2024

But since a user can create an async sanitizer maybe we can add an example or test to illustrate that case ?

from restless.

sz-piotr avatar sz-piotr commented on August 16, 2024

Hmm might be indeed useful once we have the notion of AsyncSanitizers somewhere in the library. If in the future we add this it will definitely be described in the readme.

from restless.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.