@cratestack/validator-yup 0.5.2
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 +30 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stephane Segning
|
|
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,30 @@
|
|
|
1
|
+
# @cratestack/validator-yup
|
|
2
|
+
|
|
3
|
+
An [`RpcLink`](https://github.com/cratestack/cratestack/tree/main/packages/cratestack-ts-types)
|
|
4
|
+
([issue #182](https://github.com/cratestack/cratestack/issues/182)) for CrateStack's generated
|
|
5
|
+
TypeScript RPC client (`transport rpc` schemas) that validates `request.input` against a per-op
|
|
6
|
+
[yup](https://github.com/jquense/yup) schema before the call reaches the network — client-side
|
|
7
|
+
validation with the exact same schema shape your server-side procedure/model input already has,
|
|
8
|
+
instead of hoping the generated types alone catch a bad call at compile time.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createYupValidatorLink } from "@cratestack/validator-yup";
|
|
14
|
+
import { CratestackRpcRuntime } from "./generated/runtime"; // your project's generated client
|
|
15
|
+
import * as yup from "yup";
|
|
16
|
+
|
|
17
|
+
const runtime = new CratestackRpcRuntime("https://api.example.com", {
|
|
18
|
+
links: [
|
|
19
|
+
createYupValidatorLink({
|
|
20
|
+
"model.Order.create": yup.object({ total: yup.number().positive().required() }),
|
|
21
|
+
}),
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Ops with no configured schema pass through unvalidated — this is opt-in per op, not a blanket
|
|
27
|
+
gate. On success, the schema's *cast* output (defaults, type coercion) becomes the actual request
|
|
28
|
+
`input`, not just a type-level assertion. On failure, the chain short-circuits — the real network
|
|
29
|
+
call never happens — and the call rejects the same way a server-side validation failure would,
|
|
30
|
+
with an `RpcErrorBody` carrying `code: "invalid_argument"`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RpcLink } from "@cratestack/ts-types";
|
|
2
|
+
import type { AnySchema } from "yup";
|
|
3
|
+
/** Maps a `request.opId` to the schema its `input` must satisfy. Ops
|
|
4
|
+
* with no entry pass through unvalidated — this link is opt-in per op,
|
|
5
|
+
* not a blanket gate. */
|
|
6
|
+
export type YupValidatorSchemas = Record<string, AnySchema>;
|
|
7
|
+
/** Validates `request.input` against a per-op `yup` schema before
|
|
8
|
+
* calling `next()`, shipped as an
|
|
9
|
+
* [`RpcLink`](https://github.com/cratestack/cratestack/tree/main/packages/cratestack-ts-types)
|
|
10
|
+
* ([issue #182](https://github.com/cratestack/cratestack/issues/182)) so it composes with
|
|
11
|
+
* `@cratestack/link-batch`/`@cratestack/link-logger` instead of requiring its own call site.
|
|
12
|
+
*
|
|
13
|
+
* On success, `next()` receives the schema's *cast* output as `input`
|
|
14
|
+
* — so defaults/type coercion actually take effect on the wire, not
|
|
15
|
+
* just at the type level. On failure, the chain short-circuits:
|
|
16
|
+
* `next()` is never called and the link resolves its own `Response`
|
|
17
|
+
* carrying an `RpcErrorBody` with `code: "invalid_argument"`,
|
|
18
|
+
* mirroring what the server itself would return for a rejected input
|
|
19
|
+
* (see `docs/design/rpc-transport.md`). */
|
|
20
|
+
export declare function createYupValidatorLink(schemas: YupValidatorSchemas): RpcLink;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAGrC;;0BAE0B;AAC1B,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAE5D;;;;;;;;;;;;4CAY4C;AAC5C,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAsB5E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ValidationError } from "yup";
|
|
2
|
+
/** Validates `request.input` against a per-op `yup` schema before
|
|
3
|
+
* calling `next()`, shipped as an
|
|
4
|
+
* [`RpcLink`](https://github.com/cratestack/cratestack/tree/main/packages/cratestack-ts-types)
|
|
5
|
+
* ([issue #182](https://github.com/cratestack/cratestack/issues/182)) so it composes with
|
|
6
|
+
* `@cratestack/link-batch`/`@cratestack/link-logger` instead of requiring its own call site.
|
|
7
|
+
*
|
|
8
|
+
* On success, `next()` receives the schema's *cast* output as `input`
|
|
9
|
+
* — so defaults/type coercion actually take effect on the wire, not
|
|
10
|
+
* just at the type level. On failure, the chain short-circuits:
|
|
11
|
+
* `next()` is never called and the link resolves its own `Response`
|
|
12
|
+
* carrying an `RpcErrorBody` with `code: "invalid_argument"`,
|
|
13
|
+
* mirroring what the server itself would return for a rejected input
|
|
14
|
+
* (see `docs/design/rpc-transport.md`). */
|
|
15
|
+
export function createYupValidatorLink(schemas) {
|
|
16
|
+
return async (request, next) => {
|
|
17
|
+
const schema = schemas[request.opId];
|
|
18
|
+
if (!schema) {
|
|
19
|
+
return next(request);
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const value = await schema.validate(request.input, { abortEarly: false, stripUnknown: true });
|
|
23
|
+
return next({ ...request, input: value });
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (!(error instanceof ValidationError)) {
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
const body = {
|
|
30
|
+
code: "invalid_argument",
|
|
31
|
+
message: `input for "${request.opId}" failed validation`,
|
|
32
|
+
details: error.errors,
|
|
33
|
+
};
|
|
34
|
+
return { response: new Response(request.codec.encode(body), { status: 400 }) };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,KAAK,CAAC;AAOtC;;;;;;;;;;;;4CAY4C;AAC5C,MAAM,UAAU,sBAAsB,CAAC,OAA4B;IACjE,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;QAED,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;YAC9F,OAAO,IAAI,CAAC,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,CAAC,KAAK,YAAY,eAAe,CAAC,EAAE,CAAC;gBACxC,MAAM,KAAK,CAAC;YACd,CAAC;YACD,MAAM,IAAI,GAAiB;gBACzB,IAAI,EAAE,kBAAkB;gBACxB,OAAO,EAAE,cAAc,OAAO,CAAC,IAAI,qBAAqB;gBACxD,OAAO,EAAE,KAAK,CAAC,MAAM;aACtB,CAAC;YACF,OAAO,EAAE,QAAQ,EAAE,IAAI,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;QACjF,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cratestack/validator-yup",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"description": "An RpcLink for CrateStack's generated TypeScript RPC client that validates request input against per-op yup schemas.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/cratestack/cratestack.git",
|
|
9
|
+
"directory": "packages/cratestack-validator-yup"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://cratestack.dev",
|
|
12
|
+
"bugs": "https://github.com/cratestack/cratestack/issues",
|
|
13
|
+
"keywords": ["cratestack", "cstack", "rpc", "yup", "validation", "trpc-links"],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.json",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"lint": "biome check ."
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@cratestack/ts-types": "0.5.2"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"yup": "^1.4.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"yup": { "optional": true }
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"typescript": "^5.7.0",
|
|
41
|
+
"vitest": "^3.0.0",
|
|
42
|
+
"yup": "^1.4.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
}
|
|
47
|
+
}
|