Skip to content

Commit c1deeeb

Browse files
committed
feat(validation): universal validateSchema for TypeBox + Zod
Adds `validateSchema` / `parseSchema` at `@socketsecurity/lib/validation/validate-schema` — a single structurally-dispatched entry point that accepts TypeBox schemas, Zod v3/v4 schemas, or any `safeParse`-shaped duck type, and returns a tagged `{ ok, value | errors }` result with normalized `{ path, message }` issues across all validator backends. Type inference flows through: Zod users get `z.infer<…>`, TypeBox users get `Static<…>`, no casts. Migrates the bundled external from `zod` to `@sinclair/typebox` since TypeBox is the only backend whose runtime we eagerly need (`Value.Check` / `Value.Errors`); Zod is detected purely via `.safeParse`, so it stays a pinned devDep used by tests that verify the Zod path. `ipc.ts`'s stub schema switches from `z.object(...)` to `Type.Object(...)` + `parseSchema` to prove the new path end-to-end. Drops `./zod` subpath export + `src/zod.ts` + `src/external/zod.*` + `test/unit/zod.test.mts` (the wrapper has no consumers now that validation goes through the universal helper). TypeBox is bundled as `dist/external/@sinclair/typebox.js` (+ `/value.js` subpath) so consumers of `@socketsecurity/lib` do not need to install `@sinclair/typebox` themselves.
1 parent 94c67b5 commit c1deeeb

15 files changed

Lines changed: 474 additions & 163 deletions

File tree

docs/api-index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Each entry links to the source module and shows the first sentence of its `@file
5858
| [`@socketsecurity/lib/url`](../src/url.ts) | URL parsing and validation utilities. |
5959
| [`@socketsecurity/lib/versions`](../src/versions.ts) | Version comparison and validation utilities for Socket ecosystem. |
6060
| [`@socketsecurity/lib/words`](../src/words.ts) | Word manipulation utilities for capitalization and formatting. |
61-
| [`@socketsecurity/lib/zod`](../src/zod.ts) | Zod schema validation library wrapper for type-safe runtime validation. |
6261

6362
## argv/
6463

@@ -218,7 +217,8 @@ Each entry links to the source module and shows the first sentence of its `@file
218217

219218
## validation/
220219

221-
| Subpath | Description |
222-
| -------------------------------------------------------------------------------- | -------------------------------------------------------- |
223-
| [`@socketsecurity/lib/validation/json-parser`](../src/validation/json-parser.ts) | Safe JSON parsing with validation and security controls. |
224-
| [`@socketsecurity/lib/validation/types`](../src/validation/types.ts) | Validation type definitions. |
220+
| Subpath | Description |
221+
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
222+
| [`@socketsecurity/lib/validation/json-parser`](../src/validation/json-parser.ts) | Safe JSON parsing with validation and security controls. |
223+
| [`@socketsecurity/lib/validation/types`](../src/validation/types.ts) | Validation type definitions. |
224+
| [`@socketsecurity/lib/validation/validate-schema`](../src/validation/validate-schema.ts) | Universal schema validation — works with TypeBox, Zod (v3 and v4), and any Zod-shaped `Schema<T>` duck type. |

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@
663663
"types": "./dist/validation/types.d.ts",
664664
"default": "./dist/validation/types.js"
665665
},
666+
"./validation/validate-schema": {
667+
"types": "./dist/validation/validate-schema.d.ts",
668+
"default": "./dist/validation/validate-schema.js"
669+
},
666670
"./versions": {
667671
"types": "./dist/versions.d.ts",
668672
"default": "./dist/versions.js"
@@ -671,10 +675,6 @@
671675
"types": "./dist/words.d.ts",
672676
"default": "./dist/words.js"
673677
},
674-
"./zod": {
675-
"types": "./dist/zod.d.ts",
676-
"default": "./dist/zod.js"
677-
},
678678
"./data/extensions.json": "./data/extensions.json",
679679
"./package.json": "./package.json",
680680
"./tsconfig.dts.json": "./tsconfig.dts.json",
@@ -720,6 +720,7 @@
720720
"@npmcli/arborist": "9.1.4",
721721
"@npmcli/package-json": "7.0.0",
722722
"@npmcli/promise-spawn": "8.0.3",
723+
"@sinclair/typebox": "0.34.49",
723724
"@socketregistry/is-unicode-supported": "1.0.5",
724725
"@socketregistry/packageurl-js": "1.4.2",
725726
"@socketregistry/yocto-spinner": "1.0.25",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/build-externals/config.mts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ export const externalPackages = [
4949
{ name: 'which', bundle: true },
5050
{ name: 'yargs-parser', bundle: true },
5151
{ name: 'yoctocolors-cjs', bundle: false },
52-
// Used by socket-cli (dist/cli.js has minified zod).
53-
{ name: 'zod', bundle: true },
5452
]
5553

5654
// Scoped packages need special handling.
@@ -86,5 +84,18 @@ export const scopedPackages = [
8684
packages: ['packageurl-js', 'is-unicode-supported', 'yocto-spinner'],
8785
optional: true,
8886
},
87+
// @sinclair/typebox powers validateSchema()'s TypeBox path. Bundle
88+
// so consumers don't need to install typebox separately — they just
89+
// import from @socketsecurity/lib/validation/validate-schema and
90+
// pass in TypeBox schemas built with our vendored copy of Type.*.
91+
//
92+
// Bundles both the core entry (for Type.* builders) and the /value
93+
// runtime (for Value.Check + Value.Errors used internally).
94+
{
95+
scope: '@sinclair',
96+
name: 'typebox',
97+
bundle: true,
98+
subpaths: ['typebox/value'],
99+
},
89100
{ scope: '@yarnpkg', name: 'extensions', bundle: true },
90101
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Re-export types from @sinclair/typebox. The runtime side is provided
2+
// by the bundled ./dist/external/@sinclair/typebox.js.
3+
export * from '@sinclair/typebox'

src/external/@sinclair/typebox.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
// Direct re-export of @sinclair/typebox (core Type.* builders).
4+
// Bundled by esbuild into dist/external/@sinclair/typebox.js.
5+
module.exports = require('@sinclair/typebox')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@sinclair/typebox/value'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
// Direct re-export of @sinclair/typebox/value (Value.Check / Value.Errors).
4+
// Bundled by esbuild into dist/external/@sinclair/typebox/value.js.
5+
module.exports = require('@sinclair/typebox/value')

src/external/zod.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/external/zod.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)