@gambonny/valext 0.1.0
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 +105 -0
- package/dist/extract-BX7JgYbE.d.ts +22 -0
- package/dist/extract-BX7JgYbE.d.ts.map +1 -0
- package/dist/extract-COvJXyrv.js +2 -0
- package/dist/extract-COvJXyrv.js.map +1 -0
- package/dist/extract.d.ts +2 -0
- package/dist/extract.js +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Jonny Gamba 2025
|
|
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,105 @@
|
|
|
1
|
+
# valext
|
|
2
|
+
|
|
3
|
+
> Minimal helper for extracting typed, validated values from Valibot schemas.
|
|
4
|
+
|
|
5
|
+
<br />
|
|
6
|
+
|
|
7
|
+
Cloudflare Workers, Hono, and other runtime environments benefit from **explicit contracts** and **clear validation flows**.
|
|
8
|
+
But `valibot.safeParse()` gives you nested boilerplate, and parsing logic often gets duplicated or abstracted too deeply.
|
|
9
|
+
|
|
10
|
+
`valext` embraces a simple idea:
|
|
11
|
+
|
|
12
|
+
### ✨ Extract values with structure and intent, without hiding what matters.
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
const result = extract(schema).from(unknownValue, issues => console.warn(issues))
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @gambonny/valext
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Why `valext`?
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## Example in Hono
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { validator } from 'hono/validator'
|
|
37
|
+
import { extract } from '@gambonny/valext'
|
|
38
|
+
import { signupSchema } from './schemas'
|
|
39
|
+
|
|
40
|
+
app.post(
|
|
41
|
+
'/signup',
|
|
42
|
+
validator('json', async (body, c) => {
|
|
43
|
+
const { success, output } = extract(signupSchema).from(body, issues =>
|
|
44
|
+
c.var.logger.warn('validation failed', { issues }),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return success ? output : c.text('Invalid input')
|
|
48
|
+
}),
|
|
49
|
+
)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `extract(schema).from(input, onError?)`
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
{
|
|
62
|
+
success: true;
|
|
63
|
+
output: T;
|
|
64
|
+
issues: undefined;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
or:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
{
|
|
72
|
+
success: false;
|
|
73
|
+
output: undefined;
|
|
74
|
+
issues: ValibotFlattenedIssues;
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
### `extract(schema).safe(input, onError?)`
|
|
81
|
+
|
|
82
|
+
Exactly like `valibot.safeParse()`, but lets you pass a callback for flattened errors.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
### `extract(schema).parse(input)`
|
|
87
|
+
|
|
88
|
+
Exactly like `valibot.parse()`. Will throw if input is invalid.
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Design Notes
|
|
93
|
+
|
|
94
|
+
- ✅ `valext` uses `valibot.safeParse()` under the hood.
|
|
95
|
+
- ✅ Errors are **flattened** with `valibot.flatten().nested` for easy rendering/logging.
|
|
96
|
+
- ✅ If you need raw nested issues, just use `valibot.safeParse()` directly — you’re not locked in.
|
|
97
|
+
|
|
98
|
+
> **Reminder:** `valext` is not an abstraction layer — it’s a tool for making your intent clear at the point of use.
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
[MIT](./LICENSE)
|
|
105
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
|
|
3
|
+
//#region src/extract.d.ts
|
|
4
|
+
type TypedSchema = v.BaseSchema<any, any, v.BaseIssue<unknown>>;
|
|
5
|
+
type OnValidationError = (issues: ReturnType<typeof v.flatten>["nested"]) => void;
|
|
6
|
+
type ExtractResult<TSchema extends TypedSchema> = {
|
|
7
|
+
success: true;
|
|
8
|
+
output: v.InferOutput<TSchema>;
|
|
9
|
+
issues: undefined;
|
|
10
|
+
} | {
|
|
11
|
+
success: false;
|
|
12
|
+
output: undefined;
|
|
13
|
+
issues: ReturnType<typeof v.flatten>["nested"];
|
|
14
|
+
};
|
|
15
|
+
declare function extract<TSchema extends TypedSchema>(schema: TSchema): {
|
|
16
|
+
from(input: unknown, onValidationError?: OnValidationError): ExtractResult<TSchema>;
|
|
17
|
+
safe(input: unknown, onValidationError?: OnValidationError): ReturnType<typeof v.safeParse>;
|
|
18
|
+
parse(input: unknown): v.InferOutput<TSchema>;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ExtractResult, OnValidationError, extract };
|
|
22
|
+
//# sourceMappingURL=extract-BX7JgYbE.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-BX7JgYbE.d.ts","names":[],"sources":["../src/extract.ts"],"sourcesContent":[],"mappings":";;;KAGK,WAAA,GAAc,CAAA,CAAE,qBAAqB,CAAA,CAAE;KAGhC,iBAAA,YACF,kBAAkB,CAAA,CAAE;AAJzB,KAQO,aARI,CAAA,gBAQ0B,WAR1B,CAAA,GAAA;EAAA,OAAA,EAAA,IAAA;EAAA,MAA4B,EASf,CAAA,CAAE,WATa,CASD,OATC,CAAA;EAAS,MAAhC,EAAA,SAAA;AAAU,CAAA,GAAA;EAGnB,OAAA,EAAA,KAAA;EAAiB,MAAA,EAAA,SAAA;EAAA,MACC,EAShB,UATgB,CAAA,OASE,CAAA,CAAE,OATJ,CAAA,CAAA,QAAA,CAAA;CAAO;AAAjB,iBAaJ,OAbI,CAAA,gBAaoB,WAbpB,CAAA,CAAA,MAAA,EAayC,OAbzC,CAAA,EAAA;EAIR,IAAA,CAAA,KAAA,EAAA,OAAa,EAAA,iBAAA,CAAA,EAcC,iBAdD,CAAA,EAelB,aAfkB,CAeJ,OAfI,CAAA;EAAA,IAAA,CAAA,KAAA,EAAA,OAAA,EAAA,iBAAA,CAAA,EAoCC,iBApCD,CAAA,EAqClB,UArCkB,CAAA,OAqCA,CAAA,CAAE,SArCF,CAAA;EAAA,KAAiB,CAAA,KAAA,EAAA,OAAA,CAAA,EAgDf,CAAA,CAAE,WAhDa,CAgDD,OAhDC,CAAA;CAAW"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import*as e from"valibot";function t(t){return{from(n,r){let i=e.safeParse(t,n);if(i.success)return{success:!0,output:i.output,issues:void 0};let a=e.flatten(i.issues).nested;return r?.(a),{success:!1,output:void 0,issues:a}},safe(n,r){let i=e.safeParse(t,n);return i.success||r?.(e.flatten(i.issues).nested),i},parse(n){return e.parse(t,n)}}}export{t as extract};
|
|
2
|
+
//# sourceMappingURL=extract-COvJXyrv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-COvJXyrv.js","names":["schema: TSchema","input: unknown","onValidationError?: OnValidationError"],"sources":["../src/extract.ts"],"sourcesContent":["import * as v from \"valibot\"\n\n// Base schema type used across all extract functions\ntype TypedSchema = v.BaseSchema<any, any, v.BaseIssue<unknown>>\n\n// Optional error handler used when validation fails\nexport type OnValidationError = (\n issues: ReturnType<typeof v.flatten>[\"nested\"],\n) => void\n\n// Unified result shape for `from()` method\nexport type ExtractResult<TSchema extends TypedSchema> =\n | { success: true; output: v.InferOutput<TSchema>; issues: undefined }\n | {\n success: false\n output: undefined\n issues: ReturnType<typeof v.flatten>[\"nested\"]\n }\n\n// Factory function to wrap Valibot schema with safe utilities\nexport function extract<TSchema extends TypedSchema>(schema: TSchema) {\n return {\n // Runs safeParse() and returns a normalized result shape\n from(\n input: unknown,\n onValidationError?: OnValidationError,\n ): ExtractResult<TSchema> {\n const result = v.safeParse(schema, input)\n\n if (result.success) {\n return { success: true, output: result.output, issues: undefined }\n }\n\n // Flatten issues for cleaner consumption\n const flattened = v.flatten(result.issues).nested\n onValidationError?.(flattened)\n\n return {\n success: false,\n output: undefined,\n issues: flattened,\n }\n },\n\n // Same as Valibot's safeParse, but optionally hooks into error handling\n safe(\n input: unknown,\n onValidationError?: OnValidationError,\n ): ReturnType<typeof v.safeParse> {\n const result = v.safeParse(schema, input)\n\n if (!result.success) {\n onValidationError?.(v.flatten(result.issues).nested)\n }\n\n return result\n },\n\n // Raw parse that throws on failure — same as Valibot’s parse\n parse(input: unknown): v.InferOutput<TSchema> {\n return v.parse(schema, input)\n },\n }\n}\n"],"mappings":"0BAoBA,SAAgB,EAAqCA,EAAiB,CACpE,MAAO,CAEL,KACEC,EACAC,EACwB,CACxB,IAAM,EAAS,EAAE,UAAU,EAAQ,EAAM,CAEzC,GAAI,EAAO,QACT,MAAO,CAAE,QAAS,GAAM,OAAQ,EAAO,OAAQ,OAAQ,IAAA,EAAW,EAIpE,IAAM,EAAY,EAAE,QAAQ,EAAO,OAAO,CAAC,OAG3C,OAFA,IAAoB,EAAU,CAEvB,CACL,QAAS,GACT,OAAQ,IAAA,GACR,OAAQ,CACT,CACF,EAGD,KACED,EACAC,EACgC,CAChC,IAAM,EAAS,EAAE,UAAU,EAAQ,EAAM,CAMzC,OAJK,EAAO,SACV,IAAoB,EAAE,QAAQ,EAAO,OAAO,CAAC,OAAO,CAG/C,CACR,EAGD,MAAMD,EAAwC,CAC5C,OAAO,EAAE,MAAM,EAAQ,EAAM,AAC9B,CACF,CACF"}
|
package/dist/extract.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{extract as e}from"./extract-COvJXyrv.js";export{e as extract};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{extract as e}from"./extract-COvJXyrv.js";export{e as extract};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gambonny/valext",
|
|
3
|
+
"author": "Jonny Gamba",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"sideEffects": true,
|
|
6
|
+
"description": "Minimal extraction and validation helper for Valibot.",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"format": "pnpm exec biome format --write",
|
|
18
|
+
"lint": "pnpm exec biome lint",
|
|
19
|
+
"build": "tsdown",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"test": "vitest"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"valibot",
|
|
25
|
+
"extract",
|
|
26
|
+
"fluent-api",
|
|
27
|
+
"runtime-validation"
|
|
28
|
+
],
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"packageManager": "pnpm@10.13.1",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@biomejs/biome": "2.1.2",
|
|
33
|
+
"lefthook": "^1.12.2",
|
|
34
|
+
"tsdown": "^0.13.0",
|
|
35
|
+
"typescript": "^5.8.3",
|
|
36
|
+
"valibot": "^1.1.0",
|
|
37
|
+
"vitest": "^3.2.4"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"valibot": "^1.1.0"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist"
|
|
44
|
+
]
|
|
45
|
+
}
|