@queryweave/standard-schema 0.1.0-alpha.1 → 0.1.0-beta.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/README.md +13 -2
- package/dist/index.d.ts +21 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -9
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,15 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
Standard Schema interoperability for QueryWeave parameters and models.
|
|
4
4
|
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @queryweave/standard-schema
|
|
7
|
+
```
|
|
8
|
+
|
|
5
9
|
```ts
|
|
6
10
|
import { fromStandardSchema } from "@queryweave/standard-schema";
|
|
7
11
|
|
|
8
12
|
param.text().refine(fromStandardSchema(schema)).optional();
|
|
13
|
+
param.text().refine(fromStandardSchema(digitsToNumber, { encode: (value) => String(value) }));
|
|
14
|
+
param.text().refine(fromStandardSchema(slugIsFree, { async: true }));
|
|
9
15
|
defineQueryModel(definitions, { refine: [fromStandardSchema(crossFieldSchema)] });
|
|
10
16
|
```
|
|
11
17
|
|
|
12
18
|
The package depends on `@standard-schema/spec` and nothing else. Zod, Valibot, and ArkType are
|
|
13
19
|
development-only dependencies of the test suite, and one shared compatibility contract runs against
|
|
14
|
-
all three. Vendor errors are normalized into QueryWeave
|
|
20
|
+
all three. Vendor errors — and vendor exceptions — are normalized into QueryWeave
|
|
21
|
+
`validation_failed` issues.
|
|
22
|
+
|
|
23
|
+
A schema that changes the value's type must be given `encode`, the inverse used to write the value
|
|
24
|
+
back to a URL. Asynchronous schemas resolve through `model.decodeAsync` and the runtime's pending
|
|
25
|
+
state; declare them with `async: true` so the synchronous decode does not start them.
|
|
15
26
|
|
|
16
|
-
|
|
27
|
+
Documentation: https://queryweave-docs.vercel.app/validation/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { QueryRefinement } from "@queryweave/core";
|
|
1
|
+
import { QueryRefinement, QueryTransform } from "@queryweave/core";
|
|
2
2
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
//#region src/index.d.ts
|
|
4
4
|
/**
|
|
@@ -12,16 +12,32 @@ import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
|
12
12
|
interface StandardSchemaRefinementOptions {
|
|
13
13
|
/** Label used for diagnostics. Defaults to the schema's own vendor name. */
|
|
14
14
|
readonly name?: string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Declare that the schema validates asynchronously. The synchronous decode then reports
|
|
17
|
+
* `async_required` without starting the validation.
|
|
18
|
+
*/
|
|
19
|
+
readonly async?: boolean | undefined;
|
|
15
20
|
}
|
|
16
|
-
/**
|
|
21
|
+
/** Options for a schema whose output type differs from its input. */
|
|
22
|
+
interface StandardSchemaTransformOptions<TSchema extends StandardSchemaV1> extends StandardSchemaRefinementOptions {
|
|
23
|
+
/** Map a validated output back to the input the schema accepts, for writing to a URL. */
|
|
24
|
+
encode(value: StandardSchemaV1.InferOutput<TSchema>): StandardSchemaV1.InferInput<TSchema>;
|
|
25
|
+
}
|
|
26
|
+
/** The refinement produced from a validating or narrowing Standard Schema. */
|
|
17
27
|
type StandardSchemaRefinement<TSchema extends StandardSchemaV1> = QueryRefinement<StandardSchemaV1.InferInput<TSchema>, StandardSchemaV1.InferOutput<TSchema>>;
|
|
28
|
+
/** The refinement produced from a transforming Standard Schema together with its inverse. */
|
|
29
|
+
type StandardSchemaTransform<TSchema extends StandardSchemaV1> = QueryTransform<StandardSchemaV1.InferInput<TSchema>, StandardSchemaV1.InferOutput<TSchema>>;
|
|
18
30
|
/**
|
|
19
31
|
* Adapt any Standard Schema into a QueryWeave refinement.
|
|
20
32
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
33
|
+
* A schema that only validates, or narrows its type, needs no options. A schema that transforms
|
|
34
|
+
* its input into another type must be given `encode`, so the transformed value can be written
|
|
35
|
+
* back to a URL; the returned transform is what `refine()` accepts for such a schema.
|
|
36
|
+
* Synchronous schemas keep synchronous decoding available; asynchronous schemas resolve through
|
|
37
|
+
* the asynchronous decode path.
|
|
23
38
|
*/
|
|
39
|
+
declare function fromStandardSchema<TSchema extends StandardSchemaV1>(schema: TSchema, options: StandardSchemaTransformOptions<TSchema>): StandardSchemaTransform<TSchema>;
|
|
24
40
|
declare function fromStandardSchema<TSchema extends StandardSchemaV1>(schema: TSchema, options?: StandardSchemaRefinementOptions): StandardSchemaRefinement<TSchema>;
|
|
25
41
|
//#endregion
|
|
26
|
-
export { StandardSchemaRefinement, StandardSchemaRefinementOptions, fromStandardSchema };
|
|
42
|
+
export { StandardSchemaRefinement, StandardSchemaRefinementOptions, StandardSchemaTransform, StandardSchemaTransformOptions, fromStandardSchema };
|
|
27
43
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;;UAiBiB;;WAEN;;;;;WAKA;;;UAIM,+BACf,gBAAgB,0BACR;;EAER,OAAO,OAAO,iBAAiB,YAAY,WAAW,iBAAiB,WAAW;;;KAIxE,yBAAyB,gBAAgB,oBAAoB,gBACvE,iBAAiB,WAAW,UAC5B,iBAAiB,YAAY;;KAInB,wBAAwB,gBAAgB,oBAAoB,eACtE,iBAAiB,WAAW,UAC5B,iBAAiB,YAAY;;;;;;;;;;iBAgDf,mBAAmB,gBAAgB,kBACjD,QAAQ,SACR,SAAS,+BAA+B,WACvC,wBAAwB;iBACX,mBAAmB,gBAAgB,kBACjD,QAAQ,SACR,UAAU,kCACT,yBAAyB"}
|
package/dist/index.js
CHANGED
|
@@ -19,22 +19,25 @@ function toRefinementResult(result) {
|
|
|
19
19
|
}))
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* Synchronous schemas keep synchronous decoding available. Asynchronous schemas resolve through
|
|
26
|
-
* `model.decodeAsync`, and transformed outputs flow through to the parameter's value type.
|
|
27
|
-
*/
|
|
22
|
+
function isThenable(value) {
|
|
23
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
24
|
+
}
|
|
28
25
|
function fromStandardSchema(schema, options = {}) {
|
|
29
26
|
const standard = schema["~standard"];
|
|
30
|
-
|
|
27
|
+
const settle = (result) => toRefinementResult(result);
|
|
28
|
+
const refinement = {
|
|
31
29
|
name: options.name ?? standard.vendor,
|
|
30
|
+
async: options.async,
|
|
32
31
|
refine: (value) => {
|
|
33
32
|
const outcome = standard.validate(value);
|
|
34
|
-
|
|
35
|
-
return toRefinementResult(outcome);
|
|
33
|
+
return isThenable(outcome) ? outcome.then(settle) : settle(outcome);
|
|
36
34
|
}
|
|
37
35
|
};
|
|
36
|
+
if ("encode" in options) return {
|
|
37
|
+
...refinement,
|
|
38
|
+
encode: (value) => options.encode(value)
|
|
39
|
+
};
|
|
40
|
+
return refinement;
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
//#endregion
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type {\n QueryRefinement,\n QueryRefinementIssue,\n QueryRefinementResult,\n} from \"@queryweave/core\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\n/**\n * Standard Schema interoperability.\n *\n * The validator stays a development-time choice: this package depends on the specification alone,\n * never on Zod, Valibot, ArkType, or any other runtime. Vendor errors are normalized so consumers\n * only ever see QueryWeave issues.\n */\n\n/** Options accepted by {@link fromStandardSchema}. */\nexport interface StandardSchemaRefinementOptions {\n /** Label used for diagnostics. Defaults to the schema's own vendor name. */\n readonly name?: string | undefined;\n}\n\n/** The refinement produced from a Standard Schema. */\nexport type StandardSchemaRefinement<TSchema extends StandardSchemaV1> = QueryRefinement<\n StandardSchemaV1.InferInput<TSchema>,\n StandardSchemaV1.InferOutput<TSchema>\n>;\n\nfunction toPath(segments: StandardSchemaV1.Issue[\"path\"]): readonly PropertyKey[] | undefined {\n if (segments === undefined) {\n return undefined;\n }\n const path: PropertyKey[] = [];\n for (const segment of segments) {\n if (typeof segment === \"object\") {\n path.push(segment.key);\n } else {\n path.push(segment);\n }\n }\n return path;\n}\n\nfunction toRefinementResult<TOutput>(\n result: StandardSchemaV1.Result<TOutput>,\n): QueryRefinementResult<TOutput> {\n if (result.issues === undefined) {\n return { ok: true, value: result.value };\n }\n const issues: QueryRefinementIssue[] = result.issues.map((issue) => ({\n message: issue.message,\n path: toPath(issue.path),\n }));\n return { ok: false, issues };\n}\n\n/**\n * Adapt any Standard Schema into a QueryWeave refinement.\n *\n * Synchronous schemas keep synchronous decoding available
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type {\n QueryRefinement,\n QueryRefinementIssue,\n QueryRefinementResult,\n QueryTransform,\n} from \"@queryweave/core\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\n/**\n * Standard Schema interoperability.\n *\n * The validator stays a development-time choice: this package depends on the specification alone,\n * never on Zod, Valibot, ArkType, or any other runtime. Vendor errors are normalized so consumers\n * only ever see QueryWeave issues.\n */\n\n/** Options accepted by {@link fromStandardSchema}. */\nexport interface StandardSchemaRefinementOptions {\n /** Label used for diagnostics. Defaults to the schema's own vendor name. */\n readonly name?: string | undefined;\n /**\n * Declare that the schema validates asynchronously. The synchronous decode then reports\n * `async_required` without starting the validation.\n */\n readonly async?: boolean | undefined;\n}\n\n/** Options for a schema whose output type differs from its input. */\nexport interface StandardSchemaTransformOptions<\n TSchema extends StandardSchemaV1,\n> extends StandardSchemaRefinementOptions {\n /** Map a validated output back to the input the schema accepts, for writing to a URL. */\n encode(value: StandardSchemaV1.InferOutput<TSchema>): StandardSchemaV1.InferInput<TSchema>;\n}\n\n/** The refinement produced from a validating or narrowing Standard Schema. */\nexport type StandardSchemaRefinement<TSchema extends StandardSchemaV1> = QueryRefinement<\n StandardSchemaV1.InferInput<TSchema>,\n StandardSchemaV1.InferOutput<TSchema>\n>;\n\n/** The refinement produced from a transforming Standard Schema together with its inverse. */\nexport type StandardSchemaTransform<TSchema extends StandardSchemaV1> = QueryTransform<\n StandardSchemaV1.InferInput<TSchema>,\n StandardSchemaV1.InferOutput<TSchema>\n>;\n\nfunction toPath(segments: StandardSchemaV1.Issue[\"path\"]): readonly PropertyKey[] | undefined {\n if (segments === undefined) {\n return undefined;\n }\n const path: PropertyKey[] = [];\n for (const segment of segments) {\n if (typeof segment === \"object\") {\n path.push(segment.key);\n } else {\n path.push(segment);\n }\n }\n return path;\n}\n\nfunction toRefinementResult<TOutput>(\n result: StandardSchemaV1.Result<TOutput>,\n): QueryRefinementResult<TOutput> {\n if (result.issues === undefined) {\n return { ok: true, value: result.value };\n }\n const issues: QueryRefinementIssue[] = result.issues.map((issue) => ({\n message: issue.message,\n path: toPath(issue.path),\n }));\n return { ok: false, issues };\n}\n\nfunction isThenable(value: unknown): value is PromiseLike<unknown> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n typeof (value as { then?: unknown }).then === \"function\"\n );\n}\n\n/**\n * Adapt any Standard Schema into a QueryWeave refinement.\n *\n * A schema that only validates, or narrows its type, needs no options. A schema that transforms\n * its input into another type must be given `encode`, so the transformed value can be written\n * back to a URL; the returned transform is what `refine()` accepts for such a schema.\n * Synchronous schemas keep synchronous decoding available; asynchronous schemas resolve through\n * the asynchronous decode path.\n */\nexport function fromStandardSchema<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n options: StandardSchemaTransformOptions<TSchema>,\n): StandardSchemaTransform<TSchema>;\nexport function fromStandardSchema<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n options?: StandardSchemaRefinementOptions,\n): StandardSchemaRefinement<TSchema>;\nexport function fromStandardSchema<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n options: StandardSchemaRefinementOptions | StandardSchemaTransformOptions<TSchema> = {},\n): StandardSchemaRefinement<TSchema> {\n type Output = StandardSchemaV1.InferOutput<TSchema>;\n const standard = schema[\"~standard\"];\n const settle = (result: unknown): QueryRefinementResult<Output> =>\n toRefinementResult(result as StandardSchemaV1.Result<Output>);\n\n const refinement: StandardSchemaRefinement<TSchema> = {\n name: options.name ?? standard.vendor,\n async: options.async,\n refine: (value) => {\n const outcome = standard.validate(value);\n return isThenable(outcome) ? outcome.then(settle) : settle(outcome);\n },\n };\n if (\"encode\" in options) {\n return { ...refinement, encode: (value) => options.encode(value) };\n }\n return refinement;\n}\n"],"mappings":";AA+CA,SAAS,OAAO,UAA8E;CAC5F,IAAI,aAAa,QACf;CAEF,MAAM,OAAsB,CAAC;CAC7B,KAAK,MAAM,WAAW,UACpB,IAAI,OAAO,YAAY,UACrB,KAAK,KAAK,QAAQ,GAAG;MAErB,KAAK,KAAK,OAAO;CAGrB,OAAO;AACT;AAEA,SAAS,mBACP,QACgC;CAChC,IAAI,OAAO,WAAW,QACpB,OAAO;EAAE,IAAI;EAAM,OAAO,OAAO;CAAM;CAMzC,OAAO;EAAE,IAAI;EAAO,QAJmB,OAAO,OAAO,KAAK,WAAW;GACnE,SAAS,MAAM;GACf,MAAM,OAAO,MAAM,IAAI;EACzB,EACyB;CAAE;AAC7B;AAEA,SAAS,WAAW,OAA+C;CACjE,OACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAQ,MAA6B,SAAS;AAElD;AAmBA,SAAgB,mBACd,QACA,UAAqF,CAAC,GACnD;CAEnC,MAAM,WAAW,OAAO;CACxB,MAAM,UAAU,WACd,mBAAmB,MAAyC;CAE9D,MAAM,aAAgD;EACpD,MAAM,QAAQ,QAAQ,SAAS;EAC/B,OAAO,QAAQ;EACf,SAAS,UAAU;GACjB,MAAM,UAAU,SAAS,SAAS,KAAK;GACvC,OAAO,WAAW,OAAO,IAAI,QAAQ,KAAK,MAAM,IAAI,OAAO,OAAO;EACpE;CACF;CACA,IAAI,YAAY,SACd,OAAO;EAAE,GAAG;EAAY,SAAS,UAAU,QAAQ,OAAO,KAAK;CAAE;CAEnE,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@queryweave/standard-schema",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
4
|
"description": "Standard Schema interoperability contracts for QueryWeave.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"query",
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@standard-schema/spec": "1.1.0",
|
|
42
|
-
"@queryweave/core": "0.1.0-
|
|
42
|
+
"@queryweave/core": "0.1.0-beta.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@queryweave/typescript-config": "0.0.0"
|
|
46
46
|
},
|
|
47
47
|
"engines": {
|
|
48
|
-
"node": ">=
|
|
48
|
+
"node": ">=22.12.0"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsdown",
|