Giter Site home page Giter Site logo

smikhalevski / doubter Goto Github PK

View Code? Open in Web Editor NEW
63.0 63.0 1.0 6.45 MB

🤔 Runtime validation and transformation library.

Home Page: https://smikhalevski.github.io/doubter/latest/modules/core.html

License: MIT License

JavaScript 7.17% TypeScript 92.83%
assert parsing transformation validation

doubter's Introduction

doubter's People

Contributors

smikhalevski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

icenine457

doubter's Issues

How to perform an async validation? it's missing from docs

The README did mention that it's possible to create async validations but it doesn't include an example, neither the tests include one.

I tried to use check method

import * as d from 'doubter';

async function isOdd(value: number): Promise<boolean> {
  return value % 2 === 0;
}

const oddNumber = d.number()
  .coerce()
  .check(async (value) => {
    if (await isOdd(value)) {
      return null;
    }
    return {
      code: 'odd_number',
      message: 'Number must be odd',
    } satisfies d.Issue;
  });

await oddNumber.parseAsync(4);
⚠️ Typescript error
error TS2345: Argument of type '(value: number) => Promise<{ code: string; message: string; } | null>' is not assignable to parameter of type 'CheckCallback'.
Type 'Promise<{ code: string; message: string; } | null>' is not assignable to type 'void | Issue | Issue[] | null | undefined'.
💥 Runtime error
ValidationError: [
  {}
]
    at new ValidationError (/Users/developer/Projects/express-mssql-example/node_modules/.pnpm/[email protected]/node_modules/doubter/lib/index.js:370:28) {
  issues: [
    Promise {
      undefined,
      [Symbol(async_id_symbol)]: 909,
      [Symbol(trigger_async_id_symbol)]: 6
    }
  ]
}

Or using refine method

import * as d from 'doubter';

async function isOdd(value: number): Promise<boolean> {
  return value % 2 === 0;
}

const oddNumber = d.number()
  .coerce()
  .refine(isOdd);
  
await oddNumber.parseAsync(3)
⚠️ Typescript error
error TS2769: No overload matches this call.
Overload 1 of 2, '(cb: (output: number, options: Readonly) => output is number, options?: RefineOptions | Message | undefined): Shape', gave the following error.
  Argument of type '(value: number) => Promise' is not assignable to parameter of type '(output: number, options: Readonly) => output is number'.
    Signature '(value: number): Promise' must be a type predicate.
Overload 2 of 2, '(cb: (output: number, options: Readonly) => boolean, options?: RefineOptions | Message | undefined): NumberShape', gave the following error.
  Argument of type '(value: number) => Promise' is not assignable to parameter of type '(output: number, options: Readonly) => boolean'.
    Type 'Promise' is not assignable to type 'boolean'.
🤷 no error but it always pass
Promise {
  ,
  [Symbol(async_id_symbol)]: 1254,
  [Symbol(trigger_async_id_symbol)]: 1253
}
3

Doubter version: 2.1.0
Node.js version: 20.3.1

PS: my actual use case is that I want to check whether a value exist in the DB

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.