toolcraft-schema 0.0.93 → 0.0.95
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 +19 -5
- package/dist/index.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
# toolcraft-schema
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Zero-dependency schema builder for typed command inputs and JSON Schema generation.
|
|
3
|
+
Zero-dependency schema builder for typed command inputs, runtime validation,
|
|
4
|
+
and JSON Schema generation.
|
|
6
5
|
|
|
7
6
|
## Features
|
|
8
7
|
|
|
9
8
|
- Zero runtime dependencies
|
|
10
9
|
- Typed schema descriptors
|
|
11
10
|
- `Static<typeof schema>` type inference
|
|
11
|
+
- Runtime validation with `validateValue()`
|
|
12
12
|
- JSON Schema serialization via `toJsonSchema()`
|
|
13
13
|
- JSON Schema document serialization via `toJsonSchemaDocument()`
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
import { S, toJsonSchema, toJsonSchemaDocument } from "toolcraft-schema";
|
|
18
|
+
import { S, toJsonSchema, toJsonSchemaDocument, validateValue } from "toolcraft-schema";
|
|
19
19
|
import type { Static } from "toolcraft-schema";
|
|
20
20
|
|
|
21
21
|
const schema = S.Object({
|
|
@@ -38,6 +38,11 @@ const document = toJsonSchemaDocument(schema, {
|
|
|
38
38
|
id: "https://example.test/schema.json",
|
|
39
39
|
title: "Example schema"
|
|
40
40
|
});
|
|
41
|
+
const validation = validateValue(schema, {
|
|
42
|
+
name: "Ada",
|
|
43
|
+
mode: "safe",
|
|
44
|
+
tags: []
|
|
45
|
+
});
|
|
41
46
|
```
|
|
42
47
|
|
|
43
48
|
## API
|
|
@@ -49,6 +54,9 @@ const document = toJsonSchemaDocument(schema, {
|
|
|
49
54
|
- `S.Boolean({ description?, default?, short?, cliAliases? })`
|
|
50
55
|
- `S.Enum(values, { description?, default?, short?, cliAliases? })`
|
|
51
56
|
- `S.Array(itemSchema, { description?, default?, short?, cliAliases? })`
|
|
57
|
+
- `S.Record(valueSchema, { description?, default? })`
|
|
58
|
+
- `S.Union([schemaA, schemaB], { description?, default? })`
|
|
59
|
+
- `S.OneOf([schemaA, schemaB], { description?, default? })`
|
|
52
60
|
- `S.Object({ [key]: schema })`
|
|
53
61
|
- `S.Optional(schema)`
|
|
54
62
|
|
|
@@ -66,7 +74,13 @@ const document = toJsonSchemaDocument(schema, {
|
|
|
66
74
|
- Nested `S.Object(...)` schemas produce nested JSON Schema objects.
|
|
67
75
|
- `S.Enum(...)` rejects empty or duplicate values at runtime for JavaScript callers.
|
|
68
76
|
|
|
69
|
-
|
|
77
|
+
### Runtime validation
|
|
78
|
+
|
|
79
|
+
- `validateValue(schema, value)` returns `{ ok: true, value }` for valid input.
|
|
80
|
+
- Invalid input returns `{ ok: false, issues }` with path-aware diagnostics.
|
|
81
|
+
- Validation applies defaults from schema descriptors.
|
|
82
|
+
|
|
83
|
+
## Environment Variables
|
|
70
84
|
|
|
71
85
|
This package exposes no environment variables.
|
|
72
86
|
|
package/dist/index.d.ts
CHANGED
|
@@ -49,6 +49,7 @@ type InferObject<TShape extends ObjectShape> = {
|
|
|
49
49
|
};
|
|
50
50
|
type SchemaOptions<TDefault> = {
|
|
51
51
|
description?: string;
|
|
52
|
+
cliDescription?: string;
|
|
52
53
|
cliAliases?: readonly string[];
|
|
53
54
|
default?: TDefault;
|
|
54
55
|
nullable?: boolean;
|
|
@@ -67,6 +68,7 @@ type WithNullable<TSchema extends AnySchema, TOptions extends {
|
|
|
67
68
|
export interface SchemaBase<TKind extends SchemaKind, TStatic> {
|
|
68
69
|
readonly kind: TKind;
|
|
69
70
|
readonly description?: string;
|
|
71
|
+
readonly cliDescription?: string;
|
|
70
72
|
readonly cliAliases?: readonly string[];
|
|
71
73
|
readonly default?: TStatic;
|
|
72
74
|
readonly nullable?: boolean;
|