smikhalevski / doubter Goto Github PK
View Code? Open in Web Editor NEW🤔 Runtime validation and transformation library.
Home Page: https://smikhalevski.github.io/doubter/latest/modules/core.html
License: MIT License
🤔 Runtime validation and transformation library.
Home Page: https://smikhalevski.github.io/doubter/latest/modules/core.html
License: MIT License
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);
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'.
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)
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'.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.