@unthrown/standard-schema 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -0
- package/dist/index.cjs +69 -0
- package/dist/index.d.cts +60 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +60 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +69 -0
- package/dist/index.mjs.map +1 -0
- package/docs/index.md +116 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benoit Travers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @unthrown/standard-schema
|
|
2
|
+
|
|
3
|
+
> [Standard Schema](https://standardschema.dev) interop for
|
|
4
|
+
> [unthrown](https://github.com/btravstack/unthrown)'s `Result`.
|
|
5
|
+
|
|
6
|
+
๐ **[Documentation](https://btravstack.github.io/unthrown/guide/interop)** ยท
|
|
7
|
+
[API Reference](https://btravstack.github.io/unthrown/api/standard-schema/)
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @unthrown/standard-schema
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Bridge any [Standard Schema](https://standardschema.dev) validator โ **Zod**,
|
|
14
|
+
**Valibot**, **ArkType**, and others โ to a `Result`. A schema's validation
|
|
15
|
+
issues become the modeled error `E`, because a failed validation is an
|
|
16
|
+
_anticipated_ outcome, not a defect.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { fromSchema, fromSchemaAsync } from "@unthrown/standard-schema";
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
|
|
22
|
+
const parseUser = fromSchema(z.object({ id: z.string() }));
|
|
23
|
+
|
|
24
|
+
parseUser({ id: "u_1" }).unwrap(); // { id: "u_1" }
|
|
25
|
+
parseUser({ id: 1 }).unwrapErr(); // readonly StandardSchemaV1.Issue[]
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- `fromSchema(schema)` โ returns a validator `(input) => Result<Output, Issues>`
|
|
29
|
+
for a **synchronous** schema. If the schema validates asynchronously it throws
|
|
30
|
+
a `TypeError` (use `fromSchemaAsync`).
|
|
31
|
+
- `fromSchemaAsync(schema)` โ returns a validator
|
|
32
|
+
`(input) => AsyncResult<Output, Issues>` that accepts **sync or async**
|
|
33
|
+
schemas. A validator that _throws_ (rather than returning issues) becomes a
|
|
34
|
+
`Defect`; the `AsyncResult` never rejects.
|
|
35
|
+
|
|
36
|
+
`@standard-schema/spec` is a tiny, types-only dependency โ your validator
|
|
37
|
+
library (Zod, Valibot, โฆ) provides the runtime.
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
[MIT](../../LICENSE) ยฉ Benoit TRAVERS
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let unthrown = require("unthrown");
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Turn a **synchronous** Standard Schema into a validator returning a
|
|
6
|
+
* `Result`.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Validation issues are the modeled error `E` โ `Result<Output, SchemaIssues>` โ
|
|
10
|
+
* because a failed validation is an *anticipated* outcome, not a Defect. Works
|
|
11
|
+
* with any Standard Schema implementation (Zod, Valibot, ArkType, โฆ).
|
|
12
|
+
*
|
|
13
|
+
* A validator that **throws** (rather than returning issues) becomes a `Defect`
|
|
14
|
+
* โ the same boundary behaviour as `fromThrowable`, so an unexpected crash never
|
|
15
|
+
* escapes as a raw exception. If the schema validates **asynchronously**
|
|
16
|
+
* (its `validate` returns a `Promise`), a synchronous `Result` cannot represent
|
|
17
|
+
* the pending work, so this throws a `TypeError` โ a deliberate usage error; use
|
|
18
|
+
* {@link fromSchemaAsync} instead.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam S - the schema type.
|
|
21
|
+
* @param schema - a Standard Schema validator.
|
|
22
|
+
* @returns a function mapping an input to `Result<Output, SchemaIssues>`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { fromSchema } from "@unthrown/standard-schema";
|
|
27
|
+
* const parse = fromSchema(z.string());
|
|
28
|
+
* parse("hi").unwrap(); // "hi"
|
|
29
|
+
* parse(42).unwrapErr(); // the issues array
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function fromSchema(schema) {
|
|
33
|
+
const validate = (0, unthrown.fromThrowable)((input) => schema["~standard"].validate(input), (cause) => (0, unthrown.Defect)(cause));
|
|
34
|
+
return (input) => {
|
|
35
|
+
const settled = validate(input);
|
|
36
|
+
if (settled.isOk() && settled.value instanceof Promise) throw new TypeError("@unthrown/standard-schema: this schema validates asynchronously โ use fromSchemaAsync instead.");
|
|
37
|
+
return settled.flatMap((result) => {
|
|
38
|
+
const sync = result;
|
|
39
|
+
return sync.issues ? (0, unthrown.Err)(sync.issues) : (0, unthrown.Ok)(sync.value);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Turn a Standard Schema (sync **or** async) into a validator returning an
|
|
45
|
+
* `AsyncResult`.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* The async counterpart of {@link fromSchema}: it awaits the schema's
|
|
49
|
+
* `validate`, so it accepts both synchronous and asynchronous schemas. As with
|
|
50
|
+
* every `AsyncResult`, the returned value never rejects โ a validator that
|
|
51
|
+
* *throws* (rather than returning issues) becomes a `Defect`.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam S - the schema type.
|
|
54
|
+
* @param schema - a Standard Schema validator.
|
|
55
|
+
* @returns a function mapping an input to `AsyncResult<Output, SchemaIssues>`.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { fromSchemaAsync } from "@unthrown/standard-schema";
|
|
60
|
+
* const parse = fromSchemaAsync(asyncSchema);
|
|
61
|
+
* (await parse(input)).match({ ok, err, defect });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function fromSchemaAsync(schema) {
|
|
65
|
+
return (input) => (0, unthrown.fromSafePromise)(async () => schema["~standard"].validate(input)).flatMap((result) => result.issues ? (0, unthrown.Err)(result.issues) : (0, unthrown.Ok)(result.value));
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
exports.fromSchema = fromSchema;
|
|
69
|
+
exports.fromSchemaAsync = fromSchemaAsync;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { AsyncResult, Result } from "unthrown";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/** The error channel both entry points produce: a schema's validation issues. */
|
|
6
|
+
type SchemaIssues = readonly StandardSchemaV1.Issue[];
|
|
7
|
+
/**
|
|
8
|
+
* Turn a **synchronous** Standard Schema into a validator returning a
|
|
9
|
+
* `Result`.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Validation issues are the modeled error `E` โ `Result<Output, SchemaIssues>` โ
|
|
13
|
+
* because a failed validation is an *anticipated* outcome, not a Defect. Works
|
|
14
|
+
* with any Standard Schema implementation (Zod, Valibot, ArkType, โฆ).
|
|
15
|
+
*
|
|
16
|
+
* A validator that **throws** (rather than returning issues) becomes a `Defect`
|
|
17
|
+
* โ the same boundary behaviour as `fromThrowable`, so an unexpected crash never
|
|
18
|
+
* escapes as a raw exception. If the schema validates **asynchronously**
|
|
19
|
+
* (its `validate` returns a `Promise`), a synchronous `Result` cannot represent
|
|
20
|
+
* the pending work, so this throws a `TypeError` โ a deliberate usage error; use
|
|
21
|
+
* {@link fromSchemaAsync} instead.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam S - the schema type.
|
|
24
|
+
* @param schema - a Standard Schema validator.
|
|
25
|
+
* @returns a function mapping an input to `Result<Output, SchemaIssues>`.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { fromSchema } from "@unthrown/standard-schema";
|
|
30
|
+
* const parse = fromSchema(z.string());
|
|
31
|
+
* parse("hi").unwrap(); // "hi"
|
|
32
|
+
* parse(42).unwrapErr(); // the issues array
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function fromSchema<S extends StandardSchemaV1>(schema: S): (input: unknown) => Result<StandardSchemaV1.InferOutput<S>, SchemaIssues>;
|
|
36
|
+
/**
|
|
37
|
+
* Turn a Standard Schema (sync **or** async) into a validator returning an
|
|
38
|
+
* `AsyncResult`.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* The async counterpart of {@link fromSchema}: it awaits the schema's
|
|
42
|
+
* `validate`, so it accepts both synchronous and asynchronous schemas. As with
|
|
43
|
+
* every `AsyncResult`, the returned value never rejects โ a validator that
|
|
44
|
+
* *throws* (rather than returning issues) becomes a `Defect`.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam S - the schema type.
|
|
47
|
+
* @param schema - a Standard Schema validator.
|
|
48
|
+
* @returns a function mapping an input to `AsyncResult<Output, SchemaIssues>`.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { fromSchemaAsync } from "@unthrown/standard-schema";
|
|
53
|
+
* const parse = fromSchemaAsync(asyncSchema);
|
|
54
|
+
* (await parse(input)).match({ ok, err, defect });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function fromSchemaAsync<S extends StandardSchemaV1>(schema: S): (input: unknown) => AsyncResult<StandardSchemaV1.InferOutput<S>, SchemaIssues>;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { SchemaIssues, fromSchema, fromSchemaAsync };
|
|
60
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;KAoBY,YAAA,YAAwB,gBAAA,CAAiB,KAAK;AAA1D;;;;AAA0D;AA8B1D;;;;;;;;;;;;;;;;;;;;;;AAE2E;AAhC3E,iBA8BgB,UAAA,WAAqB,gBAAA,EACnC,MAAA,EAAQ,CAAA,IACN,KAAA,cAAmB,MAAA,CAAO,gBAAA,CAAiB,WAAA,CAAY,CAAA,GAAI,YAAA;;;;;;;;;;;;;;;;;;;;;;iBA4C/C,eAAA,WAA0B,gBAAA,EACxC,MAAA,EAAQ,CAAA,IACN,KAAA,cAAmB,WAAA,CAAY,gBAAA,CAAiB,WAAA,CAAY,CAAA,GAAI,YAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { AsyncResult, Result } from "unthrown";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/** The error channel both entry points produce: a schema's validation issues. */
|
|
6
|
+
type SchemaIssues = readonly StandardSchemaV1.Issue[];
|
|
7
|
+
/**
|
|
8
|
+
* Turn a **synchronous** Standard Schema into a validator returning a
|
|
9
|
+
* `Result`.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Validation issues are the modeled error `E` โ `Result<Output, SchemaIssues>` โ
|
|
13
|
+
* because a failed validation is an *anticipated* outcome, not a Defect. Works
|
|
14
|
+
* with any Standard Schema implementation (Zod, Valibot, ArkType, โฆ).
|
|
15
|
+
*
|
|
16
|
+
* A validator that **throws** (rather than returning issues) becomes a `Defect`
|
|
17
|
+
* โ the same boundary behaviour as `fromThrowable`, so an unexpected crash never
|
|
18
|
+
* escapes as a raw exception. If the schema validates **asynchronously**
|
|
19
|
+
* (its `validate` returns a `Promise`), a synchronous `Result` cannot represent
|
|
20
|
+
* the pending work, so this throws a `TypeError` โ a deliberate usage error; use
|
|
21
|
+
* {@link fromSchemaAsync} instead.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam S - the schema type.
|
|
24
|
+
* @param schema - a Standard Schema validator.
|
|
25
|
+
* @returns a function mapping an input to `Result<Output, SchemaIssues>`.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { fromSchema } from "@unthrown/standard-schema";
|
|
30
|
+
* const parse = fromSchema(z.string());
|
|
31
|
+
* parse("hi").unwrap(); // "hi"
|
|
32
|
+
* parse(42).unwrapErr(); // the issues array
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function fromSchema<S extends StandardSchemaV1>(schema: S): (input: unknown) => Result<StandardSchemaV1.InferOutput<S>, SchemaIssues>;
|
|
36
|
+
/**
|
|
37
|
+
* Turn a Standard Schema (sync **or** async) into a validator returning an
|
|
38
|
+
* `AsyncResult`.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* The async counterpart of {@link fromSchema}: it awaits the schema's
|
|
42
|
+
* `validate`, so it accepts both synchronous and asynchronous schemas. As with
|
|
43
|
+
* every `AsyncResult`, the returned value never rejects โ a validator that
|
|
44
|
+
* *throws* (rather than returning issues) becomes a `Defect`.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam S - the schema type.
|
|
47
|
+
* @param schema - a Standard Schema validator.
|
|
48
|
+
* @returns a function mapping an input to `AsyncResult<Output, SchemaIssues>`.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { fromSchemaAsync } from "@unthrown/standard-schema";
|
|
53
|
+
* const parse = fromSchemaAsync(asyncSchema);
|
|
54
|
+
* (await parse(input)).match({ ok, err, defect });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function fromSchemaAsync<S extends StandardSchemaV1>(schema: S): (input: unknown) => AsyncResult<StandardSchemaV1.InferOutput<S>, SchemaIssues>;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { SchemaIssues, fromSchema, fromSchemaAsync };
|
|
60
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;KAoBY,YAAA,YAAwB,gBAAA,CAAiB,KAAK;AAA1D;;;;AAA0D;AA8B1D;;;;;;;;;;;;;;;;;;;;;;AAE2E;AAhC3E,iBA8BgB,UAAA,WAAqB,gBAAA,EACnC,MAAA,EAAQ,CAAA,IACN,KAAA,cAAmB,MAAA,CAAO,gBAAA,CAAiB,WAAA,CAAY,CAAA,GAAI,YAAA;;;;;;;;;;;;;;;;;;;;;;iBA4C/C,eAAA,WAA0B,gBAAA,EACxC,MAAA,EAAQ,CAAA,IACN,KAAA,cAAmB,WAAA,CAAY,gBAAA,CAAiB,WAAA,CAAY,CAAA,GAAI,YAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Defect, Err, Ok, fromSafePromise, fromThrowable } from "unthrown";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Turn a **synchronous** Standard Schema into a validator returning a
|
|
5
|
+
* `Result`.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Validation issues are the modeled error `E` โ `Result<Output, SchemaIssues>` โ
|
|
9
|
+
* because a failed validation is an *anticipated* outcome, not a Defect. Works
|
|
10
|
+
* with any Standard Schema implementation (Zod, Valibot, ArkType, โฆ).
|
|
11
|
+
*
|
|
12
|
+
* A validator that **throws** (rather than returning issues) becomes a `Defect`
|
|
13
|
+
* โ the same boundary behaviour as `fromThrowable`, so an unexpected crash never
|
|
14
|
+
* escapes as a raw exception. If the schema validates **asynchronously**
|
|
15
|
+
* (its `validate` returns a `Promise`), a synchronous `Result` cannot represent
|
|
16
|
+
* the pending work, so this throws a `TypeError` โ a deliberate usage error; use
|
|
17
|
+
* {@link fromSchemaAsync} instead.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam S - the schema type.
|
|
20
|
+
* @param schema - a Standard Schema validator.
|
|
21
|
+
* @returns a function mapping an input to `Result<Output, SchemaIssues>`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { fromSchema } from "@unthrown/standard-schema";
|
|
26
|
+
* const parse = fromSchema(z.string());
|
|
27
|
+
* parse("hi").unwrap(); // "hi"
|
|
28
|
+
* parse(42).unwrapErr(); // the issues array
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function fromSchema(schema) {
|
|
32
|
+
const validate = fromThrowable((input) => schema["~standard"].validate(input), (cause) => Defect(cause));
|
|
33
|
+
return (input) => {
|
|
34
|
+
const settled = validate(input);
|
|
35
|
+
if (settled.isOk() && settled.value instanceof Promise) throw new TypeError("@unthrown/standard-schema: this schema validates asynchronously โ use fromSchemaAsync instead.");
|
|
36
|
+
return settled.flatMap((result) => {
|
|
37
|
+
const sync = result;
|
|
38
|
+
return sync.issues ? Err(sync.issues) : Ok(sync.value);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Turn a Standard Schema (sync **or** async) into a validator returning an
|
|
44
|
+
* `AsyncResult`.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* The async counterpart of {@link fromSchema}: it awaits the schema's
|
|
48
|
+
* `validate`, so it accepts both synchronous and asynchronous schemas. As with
|
|
49
|
+
* every `AsyncResult`, the returned value never rejects โ a validator that
|
|
50
|
+
* *throws* (rather than returning issues) becomes a `Defect`.
|
|
51
|
+
*
|
|
52
|
+
* @typeParam S - the schema type.
|
|
53
|
+
* @param schema - a Standard Schema validator.
|
|
54
|
+
* @returns a function mapping an input to `AsyncResult<Output, SchemaIssues>`.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { fromSchemaAsync } from "@unthrown/standard-schema";
|
|
59
|
+
* const parse = fromSchemaAsync(asyncSchema);
|
|
60
|
+
* (await parse(input)).match({ ok, err, defect });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
function fromSchemaAsync(schema) {
|
|
64
|
+
return (input) => fromSafePromise(async () => schema["~standard"].validate(input)).flatMap((result) => result.issues ? Err(result.issues) : Ok(result.value));
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { fromSchema, fromSchemaAsync };
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// @unthrown/standard-schema โ bridge any Standard Schema validator (Zod,\n// Valibot, ArkType, โฆ) to a `Result` / `AsyncResult`.\n//\n// A Standard Schema's `validate` returns `{ value }` on success or `{ issues }`\n// on failure. `fromSchema` turns that into a validator returning\n// `Result<Output, readonly Issue[]>`; `fromSchemaAsync` is the async counterpart\n// (and also accepts synchronous schemas). The `issues` array is the modeled `E`\n// โ a failed validation is an anticipated outcome, never a Defect.\n//\n// import { fromSchema } from \"@unthrown/standard-schema\";\n// import { z } from \"zod\";\n//\n// const parseUser = fromSchema(z.object({ id: z.string() }));\n// parseUser(input); // Result<{ id: string }, readonly StandardSchemaV1.Issue[]>\n\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { Defect, Err, fromSafePromise, fromThrowable, Ok } from \"unthrown\";\nimport type { AsyncResult, Result } from \"unthrown\";\n\n/** The error channel both entry points produce: a schema's validation issues. */\nexport type SchemaIssues = readonly StandardSchemaV1.Issue[];\n\n/**\n * Turn a **synchronous** Standard Schema into a validator returning a\n * `Result`.\n *\n * @remarks\n * Validation issues are the modeled error `E` โ `Result<Output, SchemaIssues>` โ\n * because a failed validation is an *anticipated* outcome, not a Defect. Works\n * with any Standard Schema implementation (Zod, Valibot, ArkType, โฆ).\n *\n * A validator that **throws** (rather than returning issues) becomes a `Defect`\n * โ the same boundary behaviour as `fromThrowable`, so an unexpected crash never\n * escapes as a raw exception. If the schema validates **asynchronously**\n * (its `validate` returns a `Promise`), a synchronous `Result` cannot represent\n * the pending work, so this throws a `TypeError` โ a deliberate usage error; use\n * {@link fromSchemaAsync} instead.\n *\n * @typeParam S - the schema type.\n * @param schema - a Standard Schema validator.\n * @returns a function mapping an input to `Result<Output, SchemaIssues>`.\n *\n * @example\n * ```ts\n * import { fromSchema } from \"@unthrown/standard-schema\";\n * const parse = fromSchema(z.string());\n * parse(\"hi\").unwrap(); // \"hi\"\n * parse(42).unwrapErr(); // the issues array\n * ```\n */\nexport function fromSchema<S extends StandardSchemaV1>(\n schema: S,\n): (input: unknown) => Result<StandardSchemaV1.InferOutput<S>, SchemaIssues> {\n type Output = StandardSchemaV1.InferOutput<S>;\n // Run `validate` at a boundary so a *throwing* validator lands in the Defect\n // channel instead of escaping; `qualify` only ever mints a Defect, so E = never.\n const validate = fromThrowable(\n (input: unknown) => schema[\"~standard\"].validate(input),\n (cause) => Defect(cause),\n );\n return (input) => {\n const settled = validate(input);\n // An async schema can't be represented synchronously โ fail loud and early.\n if (settled.isOk() && settled.value instanceof Promise) {\n throw new TypeError(\n \"@unthrown/standard-schema: this schema validates asynchronously โ use fromSchemaAsync instead.\",\n );\n }\n return settled.flatMap((result) => {\n const sync = result as StandardSchemaV1.Result<Output>;\n return sync.issues ? Err(sync.issues) : Ok(sync.value);\n });\n };\n}\n\n/**\n * Turn a Standard Schema (sync **or** async) into a validator returning an\n * `AsyncResult`.\n *\n * @remarks\n * The async counterpart of {@link fromSchema}: it awaits the schema's\n * `validate`, so it accepts both synchronous and asynchronous schemas. As with\n * every `AsyncResult`, the returned value never rejects โ a validator that\n * *throws* (rather than returning issues) becomes a `Defect`.\n *\n * @typeParam S - the schema type.\n * @param schema - a Standard Schema validator.\n * @returns a function mapping an input to `AsyncResult<Output, SchemaIssues>`.\n *\n * @example\n * ```ts\n * import { fromSchemaAsync } from \"@unthrown/standard-schema\";\n * const parse = fromSchemaAsync(asyncSchema);\n * (await parse(input)).match({ ok, err, defect });\n * ```\n */\nexport function fromSchemaAsync<S extends StandardSchemaV1>(\n schema: S,\n): (input: unknown) => AsyncResult<StandardSchemaV1.InferOutput<S>, SchemaIssues> {\n return (input) =>\n fromSafePromise(async () => schema[\"~standard\"].validate(input)).flatMap((result) =>\n result.issues ? Err(result.issues) : Ok(result.value),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDA,SAAgB,WACd,QAC2E;CAI3E,MAAM,WAAW,eACd,UAAmB,OAAO,YAAY,CAAC,SAAS,KAAK,IACrD,UAAU,OAAO,KAAK,CACzB;CACA,QAAQ,UAAU;EAChB,MAAM,UAAU,SAAS,KAAK;EAE9B,IAAI,QAAQ,KAAK,KAAK,QAAQ,iBAAiB,SAC7C,MAAM,IAAI,UACR,gGACF;EAEF,OAAO,QAAQ,SAAS,WAAW;GACjC,MAAM,OAAO;GACb,OAAO,KAAK,SAAS,IAAI,KAAK,MAAM,IAAI,GAAG,KAAK,KAAK;EACvD,CAAC;CACH;AACF;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,gBACd,QACgF;CAChF,QAAQ,UACN,gBAAgB,YAAY,OAAO,YAAY,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC,SAAS,WACxE,OAAO,SAAS,IAAI,OAAO,MAAM,IAAI,GAAG,OAAO,KAAK,CACtD;AACJ"}
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
**@unthrown/standard-schema**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# @unthrown/standard-schema
|
|
6
|
+
|
|
7
|
+
## Type Aliases
|
|
8
|
+
|
|
9
|
+
### SchemaIssues
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
type SchemaIssues = readonly StandardSchemaV1.Issue[];
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Defined in: [index.ts:21](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/standard-schema/src/index.ts#L21)
|
|
16
|
+
|
|
17
|
+
The error channel both entry points produce: a schema's validation issues.
|
|
18
|
+
|
|
19
|
+
## Functions
|
|
20
|
+
|
|
21
|
+
### fromSchema()
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
function fromSchema<S>(schema): (input) => Result<InferOutput<S>, SchemaIssues>;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Defined in: [index.ts:51](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/standard-schema/src/index.ts#L51)
|
|
28
|
+
|
|
29
|
+
Turn a **synchronous** Standard Schema into a validator returning a
|
|
30
|
+
`Result`.
|
|
31
|
+
|
|
32
|
+
#### Type Parameters
|
|
33
|
+
|
|
34
|
+
| Type Parameter | Description |
|
|
35
|
+
| ------ | ------ |
|
|
36
|
+
| `S` *extends* `StandardSchemaV1`<`unknown`, `unknown`> | the schema type. |
|
|
37
|
+
|
|
38
|
+
#### Parameters
|
|
39
|
+
|
|
40
|
+
| Parameter | Type | Description |
|
|
41
|
+
| ------ | ------ | ------ |
|
|
42
|
+
| `schema` | `S` | a Standard Schema validator. |
|
|
43
|
+
|
|
44
|
+
#### Returns
|
|
45
|
+
|
|
46
|
+
a function mapping an input to `Result<Output, SchemaIssues>`.
|
|
47
|
+
|
|
48
|
+
(`input`) => `Result`<`InferOutput`<`S`>, [`SchemaIssues`](#schemaissues)>
|
|
49
|
+
|
|
50
|
+
#### Remarks
|
|
51
|
+
|
|
52
|
+
Validation issues are the modeled error `E` โ `Result<Output, SchemaIssues>` โ
|
|
53
|
+
because a failed validation is an *anticipated* outcome, not a Defect. Works
|
|
54
|
+
with any Standard Schema implementation (Zod, Valibot, ArkType, โฆ).
|
|
55
|
+
|
|
56
|
+
A validator that **throws** (rather than returning issues) becomes a `Defect`
|
|
57
|
+
โ the same boundary behaviour as `fromThrowable`, so an unexpected crash never
|
|
58
|
+
escapes as a raw exception. If the schema validates **asynchronously**
|
|
59
|
+
(its `validate` returns a `Promise`), a synchronous `Result` cannot represent
|
|
60
|
+
the pending work, so this throws a `TypeError` โ a deliberate usage error; use
|
|
61
|
+
[fromSchemaAsync](#fromschemaasync) instead.
|
|
62
|
+
|
|
63
|
+
#### Example
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { fromSchema } from "@unthrown/standard-schema";
|
|
67
|
+
const parse = fromSchema(z.string());
|
|
68
|
+
parse("hi").unwrap(); // "hi"
|
|
69
|
+
parse(42).unwrapErr(); // the issues array
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
***
|
|
73
|
+
|
|
74
|
+
### fromSchemaAsync()
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
function fromSchemaAsync<S>(schema): (input) => AsyncResult<InferOutput<S>, SchemaIssues>;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Defined in: [index.ts:97](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/standard-schema/src/index.ts#L97)
|
|
81
|
+
|
|
82
|
+
Turn a Standard Schema (sync **or** async) into a validator returning an
|
|
83
|
+
`AsyncResult`.
|
|
84
|
+
|
|
85
|
+
#### Type Parameters
|
|
86
|
+
|
|
87
|
+
| Type Parameter | Description |
|
|
88
|
+
| ------ | ------ |
|
|
89
|
+
| `S` *extends* `StandardSchemaV1`<`unknown`, `unknown`> | the schema type. |
|
|
90
|
+
|
|
91
|
+
#### Parameters
|
|
92
|
+
|
|
93
|
+
| Parameter | Type | Description |
|
|
94
|
+
| ------ | ------ | ------ |
|
|
95
|
+
| `schema` | `S` | a Standard Schema validator. |
|
|
96
|
+
|
|
97
|
+
#### Returns
|
|
98
|
+
|
|
99
|
+
a function mapping an input to `AsyncResult<Output, SchemaIssues>`.
|
|
100
|
+
|
|
101
|
+
(`input`) => `AsyncResult`<`InferOutput`<`S`>, [`SchemaIssues`](#schemaissues)>
|
|
102
|
+
|
|
103
|
+
#### Remarks
|
|
104
|
+
|
|
105
|
+
The async counterpart of [fromSchema](#fromschema): it awaits the schema's
|
|
106
|
+
`validate`, so it accepts both synchronous and asynchronous schemas. As with
|
|
107
|
+
every `AsyncResult`, the returned value never rejects โ a validator that
|
|
108
|
+
*throws* (rather than returning issues) becomes a `Defect`.
|
|
109
|
+
|
|
110
|
+
#### Example
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { fromSchemaAsync } from "@unthrown/standard-schema";
|
|
114
|
+
const parse = fromSchemaAsync(asyncSchema);
|
|
115
|
+
(await parse(input)).match({ ok, err, defect });
|
|
116
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unthrown/standard-schema",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Standard Schema (Zod, Valibot, ArkType, โฆ) interop for unthrown",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"arktype",
|
|
7
|
+
"errors-as-values",
|
|
8
|
+
"result",
|
|
9
|
+
"standard-schema",
|
|
10
|
+
"typescript",
|
|
11
|
+
"unthrown",
|
|
12
|
+
"valibot",
|
|
13
|
+
"validation",
|
|
14
|
+
"zod"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/btravstack/unthrown#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/btravstack/unthrown/issues"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/btravstack/unthrown.git",
|
|
25
|
+
"directory": "packages/standard-schema"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"docs"
|
|
30
|
+
],
|
|
31
|
+
"type": "module",
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.mjs",
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"default": "./dist/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@standard-schema/spec": "1.1.0",
|
|
50
|
+
"unthrown": "1.0.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/node": "24.13.2",
|
|
54
|
+
"@vitest/coverage-v8": "4.1.8",
|
|
55
|
+
"tsdown": "0.22.2",
|
|
56
|
+
"typedoc": "0.28.19",
|
|
57
|
+
"typedoc-plugin-markdown": "4.12.0",
|
|
58
|
+
"typescript": "6.0.3",
|
|
59
|
+
"vitest": "4.1.8",
|
|
60
|
+
"@unthrown/tsconfig": "0.1.0",
|
|
61
|
+
"@unthrown/typedoc": "0.1.0"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=22.19"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
68
|
+
"build:docs": "typedoc",
|
|
69
|
+
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"typecheck": "tsc --noEmit"
|
|
72
|
+
}
|
|
73
|
+
}
|