@decs/typeschema
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -7,4 +7,5 @@ import type { InferSchema } from './resolver'; | ||
}[keyof TypeSchemaRegistry]; | ||
export declare const adapters: Array<(<T>(schema: Schema<T>) => Promise<TypeSchema<T> | null>)>; | ||
export type Adapter = <T>(schema: Schema<T>) => Promise<TypeSchema<T> | null>; | ||
export declare const adapters: Array<Adapter>; | ||
export declare function register<TKey extends keyof TypeSchemaRegistry>(coerce: <T>(schema: Schema<T>) => Promise<InferSchema<TypeSchemaRegistry[TKey], T> | null>, wrap: <T>(schema: InferSchema<TypeSchemaRegistry[TKey], T>) => TypeSchema<T>): void; | ||
import './adapters'; |
@@ -14,5 +14,17 @@ "use strict"; | ||
const registry_1 = require("./registry"); | ||
let lastUsedAdapter = null; | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const results = (yield Promise.all(registry_1.adapters.map(wrapper => wrapper(schema)))).filter(Boolean); | ||
if (lastUsedAdapter != null) { | ||
const wrappedSchema = yield lastUsedAdapter(schema); | ||
if (wrappedSchema != null) { | ||
return wrappedSchema; | ||
} | ||
} | ||
const results = (yield Promise.all(registry_1.adapters.map((adapter) => __awaiter(this, void 0, void 0, function* () { | ||
return ({ | ||
adapter, | ||
wrappedSchema: yield adapter(schema), | ||
}); | ||
})))).filter((result) => result.wrappedSchema != null); | ||
if (results.length === 0) { | ||
@@ -24,5 +36,6 @@ throw new Error('Missing adapters for schema: ' + schema); | ||
} | ||
return results[0]; | ||
lastUsedAdapter = results[0].adapter; | ||
return results[0].wrappedSchema; | ||
}); | ||
} | ||
exports.wrap = wrap; |
{ | ||
"name": "@decs/typeschema", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Universal adapter for schema validation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -73,4 +73,4 @@ <div id="header"> | ||
- `Schema<T>`<br />Generic interface for schemas<br />An union of the schema types of all supported validation libraries | ||
- `Infer<TSchema>`<br />Extracts the equivalent TypeScript type of a schema | ||
- `ValidationIssue`<br />Generic interface for validation issues<br />Includes a `message: string` and an optional `path?: Array<string | number | symbol>` | ||
- `Infer<TSchema>`<br />Extracts the equivalent TypeScript type of a schema | ||
@@ -117,1 +117,3 @@ #### Functions | ||
- Inspired by [tRPC](https://trpc.io/)'s [input & output validators](https://trpc.io/docs/server/validators) | ||
- Adapter architecture inspired by [@ecyrbe](https://github.com/ecyrbe)'s [suggestions](https://github.com/decs/typeschema/issues/1) | ||
- API definition inspired by [@colinhacks](https://github.com/colinhacks)'s [proposal](https://twitter.com/colinhacks/status/1634284724796661761) |
42488
749
118