@appport/schema 1.0.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/README.md +32 -0
- package/dist/builder.d.ts +85 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +278 -0
- package/dist/builder.js.map +1 -0
- package/dist/from-json-schema.d.ts +12 -0
- package/dist/from-json-schema.d.ts.map +1 -0
- package/dist/from-json-schema.js +186 -0
- package/dist/from-json-schema.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/standard.d.ts +46 -0
- package/dist/standard.d.ts.map +1 -0
- package/dist/standard.js +42 -0
- package/dist/standard.js.map +1 -0
- package/dist/types.d.ts +40 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @appport/schema
|
|
2
|
+
|
|
3
|
+
Schema adapters and JSON Schema generation for AppPort capabilities.
|
|
4
|
+
|
|
5
|
+
AppPort publishes JSON Schema on the wire so the manifest is consumable from
|
|
6
|
+
TypeScript, Rust, Go, Python or Swift. In TypeScript you may author schemas three
|
|
7
|
+
ways, and all three publish the same thing.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { s, fromJSONSchema, fromStandardSchema, toSchema } from "@appport/schema";
|
|
11
|
+
|
|
12
|
+
// 1. the built-in builder: validation and JSON Schema from one definition
|
|
13
|
+
const input = s.object({ documentId: s.string(), title: s.optional(s.string()) });
|
|
14
|
+
|
|
15
|
+
// 2. any Standard Schema library (Zod, Valibot, ArkType)
|
|
16
|
+
const zodInput = fromStandardSchema(zodSchema, { jsonSchema: z.toJSONSchema(zodSchema) });
|
|
17
|
+
|
|
18
|
+
// 3. a raw JSON Schema document, validated at runtime
|
|
19
|
+
const published = fromJSONSchema({ type: "object", required: ["documentId"] });
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`fromJSONSchema` is what lets a generated client validate against a published
|
|
23
|
+
manifest without access to the original definitions — the same capability a
|
|
24
|
+
third-party implementation has.
|
|
25
|
+
|
|
26
|
+
The builder covers strings, numbers, integers, booleans, null, literals, enums,
|
|
27
|
+
arrays, objects, records, unions, optionals, nullables, timestamps and binary
|
|
28
|
+
references, with full type inference:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
type Input = Infer<typeof input>; // { documentId: string; title?: string }
|
|
32
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type BlobRef } from "@appport/protocol";
|
|
2
|
+
import type { Infer, Schema } from "./types.js";
|
|
3
|
+
export interface StringOptions {
|
|
4
|
+
minLength?: number;
|
|
5
|
+
maxLength?: number;
|
|
6
|
+
pattern?: RegExp | string;
|
|
7
|
+
format?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function string(options?: StringOptions): Schema<string>;
|
|
11
|
+
export interface NumberOptions {
|
|
12
|
+
minimum?: number;
|
|
13
|
+
maximum?: number;
|
|
14
|
+
description?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function number(options?: NumberOptions): Schema<number>;
|
|
17
|
+
export declare function integer(options?: NumberOptions): Schema<number>;
|
|
18
|
+
export declare function boolean(description?: string): Schema<boolean>;
|
|
19
|
+
export declare function nullSchema(): Schema<null>;
|
|
20
|
+
export declare function literal<const T extends string | number | boolean | null>(value: T): Schema<T>;
|
|
21
|
+
export declare function enumeration<const T extends readonly string[]>(values: T): Schema<T[number]>;
|
|
22
|
+
export declare function array<S extends Schema>(item: S, options?: {
|
|
23
|
+
minItems?: number;
|
|
24
|
+
maxItems?: number;
|
|
25
|
+
}): Schema<Array<Infer<S>>>;
|
|
26
|
+
type ShapeOf = Record<string, Schema>;
|
|
27
|
+
type OptionalKeys<P extends ShapeOf> = {
|
|
28
|
+
[K in keyof P]: P[K] extends {
|
|
29
|
+
isOptional: true;
|
|
30
|
+
} ? K : never;
|
|
31
|
+
}[keyof P];
|
|
32
|
+
type RequiredKeys<P extends ShapeOf> = Exclude<keyof P, OptionalKeys<P>>;
|
|
33
|
+
export type InferShape<P extends ShapeOf> = {
|
|
34
|
+
[K in RequiredKeys<P>]: Infer<P[K]>;
|
|
35
|
+
} & {
|
|
36
|
+
[K in OptionalKeys<P>]?: Infer<P[K]>;
|
|
37
|
+
};
|
|
38
|
+
export interface ObjectOptions {
|
|
39
|
+
/** Allow properties not described by the shape. Defaults to false. */
|
|
40
|
+
additionalProperties?: boolean;
|
|
41
|
+
description?: string;
|
|
42
|
+
title?: string;
|
|
43
|
+
}
|
|
44
|
+
export declare function object<P extends ShapeOf>(shape: P, options?: ObjectOptions): Schema<{
|
|
45
|
+
[K in keyof InferShape<P>]: InferShape<P>[K];
|
|
46
|
+
}>;
|
|
47
|
+
export declare function record<S extends Schema>(value: S): Schema<Record<string, Infer<S>>>;
|
|
48
|
+
export declare function union<S extends readonly Schema[]>(members: S): Schema<Infer<S[number]>>;
|
|
49
|
+
export declare function optional<S extends Schema>(schema: S): Schema<Infer<S> | undefined> & {
|
|
50
|
+
isOptional: true;
|
|
51
|
+
};
|
|
52
|
+
export declare function nullable<S extends Schema>(schema: S): Schema<Infer<S> | null>;
|
|
53
|
+
/** Accepts any JSON value. Used for capabilities without a meaningful shape. */
|
|
54
|
+
export declare function unknownSchema(): Schema<unknown>;
|
|
55
|
+
/** The canonical empty input/output (`{}`). */
|
|
56
|
+
export declare function empty(): Schema<Record<string, never>>;
|
|
57
|
+
/** A binary reference (§51). */
|
|
58
|
+
export declare function blob(): Schema<BlobRef>;
|
|
59
|
+
/** ISO-8601 timestamp string. */
|
|
60
|
+
export declare function timestamp(): Schema<string>;
|
|
61
|
+
/**
|
|
62
|
+
* The built-in schema builder. Applications may use it directly, or bring
|
|
63
|
+
* Zod/Valibot through the Standard Schema adapter.
|
|
64
|
+
*/
|
|
65
|
+
export declare const s: {
|
|
66
|
+
readonly string: typeof string;
|
|
67
|
+
readonly number: typeof number;
|
|
68
|
+
readonly integer: typeof integer;
|
|
69
|
+
readonly boolean: typeof boolean;
|
|
70
|
+
readonly null: typeof nullSchema;
|
|
71
|
+
readonly literal: typeof literal;
|
|
72
|
+
readonly enum: typeof enumeration;
|
|
73
|
+
readonly array: typeof array;
|
|
74
|
+
readonly object: typeof object;
|
|
75
|
+
readonly record: typeof record;
|
|
76
|
+
readonly union: typeof union;
|
|
77
|
+
readonly optional: typeof optional;
|
|
78
|
+
readonly nullable: typeof nullable;
|
|
79
|
+
readonly unknown: typeof unknownSchema;
|
|
80
|
+
readonly empty: typeof empty;
|
|
81
|
+
readonly blob: typeof blob;
|
|
82
|
+
readonly timestamp: typeof timestamp;
|
|
83
|
+
};
|
|
84
|
+
export {};
|
|
85
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,OAAO,EAAmB,MAAM,mBAAmB,CAAC;AACnF,OAAO,KAAK,EAAE,KAAK,EAAa,MAAM,EAAqC,MAAM,YAAY,CAAC;AAuB9F,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,CA4BlE;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,w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AK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAK7E;AAED,gFAAgF;AAChF,wBAAgB,aAAa,IAAI,MAAM,CAAC,OAAO,CAAC,CAE/C;AAED,+CAA+C;AAC/C,wBAAgB,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAErD;AAED,gCAAgC;AAChC,wBAAgB,IAAI,IAAI,MAAM,CAAC,OAAO,CAAC,CAetC;AAED,iCAAiC;AACjC,wBAAgB,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,CAE1C;AAED;;;GAGG;AACH,eAAO,MAAM,CAAC;;;;;;;;;;;;;;;;;;CAkBJ,CAAC"}
|
package/dist/builder.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { BLOB_REF_SCHEMA } from "@appport/protocol";
|
|
2
|
+
function ok(value) {
|
|
3
|
+
return { ok: true, value };
|
|
4
|
+
}
|
|
5
|
+
function fail(path, message, code) {
|
|
6
|
+
const issue = { path, message };
|
|
7
|
+
if (code)
|
|
8
|
+
issue.code = code;
|
|
9
|
+
return { ok: false, issues: [issue] };
|
|
10
|
+
}
|
|
11
|
+
function define(validate, toJSONSchema) {
|
|
12
|
+
return {
|
|
13
|
+
appport: "schema",
|
|
14
|
+
validate: (value, path = []) => validate(value, path),
|
|
15
|
+
toJSONSchema
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function string(options = {}) {
|
|
19
|
+
const pattern = options.pattern
|
|
20
|
+
? typeof options.pattern === "string"
|
|
21
|
+
? new RegExp(options.pattern)
|
|
22
|
+
: options.pattern
|
|
23
|
+
: undefined;
|
|
24
|
+
return define((value, path) => {
|
|
25
|
+
if (typeof value !== "string")
|
|
26
|
+
return fail(path, "expected a string", "type");
|
|
27
|
+
if (options.minLength !== undefined && value.length < options.minLength) {
|
|
28
|
+
return fail(path, `expected at least ${options.minLength} characters`, "minLength");
|
|
29
|
+
}
|
|
30
|
+
if (options.maxLength !== undefined && value.length > options.maxLength) {
|
|
31
|
+
return fail(path, `expected at most ${options.maxLength} characters`, "maxLength");
|
|
32
|
+
}
|
|
33
|
+
if (pattern && !pattern.test(value))
|
|
34
|
+
return fail(path, "does not match the required pattern", "pattern");
|
|
35
|
+
return ok(value);
|
|
36
|
+
}, () => {
|
|
37
|
+
const schema = { type: "string" };
|
|
38
|
+
if (options.description)
|
|
39
|
+
schema.description = options.description;
|
|
40
|
+
if (options.minLength !== undefined)
|
|
41
|
+
schema.minLength = options.minLength;
|
|
42
|
+
if (options.maxLength !== undefined)
|
|
43
|
+
schema.maxLength = options.maxLength;
|
|
44
|
+
if (options.pattern)
|
|
45
|
+
schema.pattern = typeof options.pattern === "string" ? options.pattern : options.pattern.source;
|
|
46
|
+
if (options.format)
|
|
47
|
+
schema.format = options.format;
|
|
48
|
+
return schema;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export function number(options = {}) {
|
|
52
|
+
return define((value, path) => {
|
|
53
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
54
|
+
return fail(path, "expected a finite number", "type");
|
|
55
|
+
if (options.minimum !== undefined && value < options.minimum) {
|
|
56
|
+
return fail(path, `expected a value >= ${options.minimum}`, "minimum");
|
|
57
|
+
}
|
|
58
|
+
if (options.maximum !== undefined && value > options.maximum) {
|
|
59
|
+
return fail(path, `expected a value <= ${options.maximum}`, "maximum");
|
|
60
|
+
}
|
|
61
|
+
return ok(value);
|
|
62
|
+
}, () => {
|
|
63
|
+
const schema = { type: "number" };
|
|
64
|
+
if (options.description)
|
|
65
|
+
schema.description = options.description;
|
|
66
|
+
if (options.minimum !== undefined)
|
|
67
|
+
schema.minimum = options.minimum;
|
|
68
|
+
if (options.maximum !== undefined)
|
|
69
|
+
schema.maximum = options.maximum;
|
|
70
|
+
return schema;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export function integer(options = {}) {
|
|
74
|
+
const base = number(options);
|
|
75
|
+
return define((value, path) => {
|
|
76
|
+
const result = base.validate(value, path);
|
|
77
|
+
if (!result.ok)
|
|
78
|
+
return result;
|
|
79
|
+
if (!Number.isInteger(result.value))
|
|
80
|
+
return fail(path, "expected an integer", "type");
|
|
81
|
+
return ok(result.value);
|
|
82
|
+
}, () => ({ ...base.toJSONSchema(), type: "integer" }));
|
|
83
|
+
}
|
|
84
|
+
export function boolean(description) {
|
|
85
|
+
return define((value, path) => (typeof value === "boolean" ? ok(value) : fail(path, "expected a boolean", "type")), () => (description ? { type: "boolean", description } : { type: "boolean" }));
|
|
86
|
+
}
|
|
87
|
+
export function nullSchema() {
|
|
88
|
+
return define((value, path) => (value === null ? ok(null) : fail(path, "expected null", "type")), () => ({ type: "null" }));
|
|
89
|
+
}
|
|
90
|
+
export function literal(value) {
|
|
91
|
+
return define((candidate, path) => candidate === value ? ok(value) : fail(path, `expected the literal ${JSON.stringify(value)}`, "const"), () => ({ const: value }));
|
|
92
|
+
}
|
|
93
|
+
export function enumeration(values) {
|
|
94
|
+
const allowed = new Set(values);
|
|
95
|
+
return define((value, path) => typeof value === "string" && allowed.has(value)
|
|
96
|
+
? ok(value)
|
|
97
|
+
: fail(path, `expected one of: ${values.join(", ")}`, "enum"), () => ({ type: "string", enum: [...values] }));
|
|
98
|
+
}
|
|
99
|
+
export function array(item, options = {}) {
|
|
100
|
+
return define((value, path) => {
|
|
101
|
+
if (!Array.isArray(value))
|
|
102
|
+
return fail(path, "expected an array", "type");
|
|
103
|
+
if (options.minItems !== undefined && value.length < options.minItems) {
|
|
104
|
+
return fail(path, `expected at least ${options.minItems} items`, "minItems");
|
|
105
|
+
}
|
|
106
|
+
if (options.maxItems !== undefined && value.length > options.maxItems) {
|
|
107
|
+
return fail(path, `expected at most ${options.maxItems} items`, "maxItems");
|
|
108
|
+
}
|
|
109
|
+
const out = [];
|
|
110
|
+
const issues = [];
|
|
111
|
+
value.forEach((entry, index) => {
|
|
112
|
+
const result = item.validate(entry, [...path, index]);
|
|
113
|
+
if (result.ok)
|
|
114
|
+
out.push(result.value);
|
|
115
|
+
else
|
|
116
|
+
issues.push(...result.issues);
|
|
117
|
+
});
|
|
118
|
+
return issues.length ? { ok: false, issues } : ok(out);
|
|
119
|
+
}, () => {
|
|
120
|
+
const schema = { type: "array", items: item.toJSONSchema() };
|
|
121
|
+
if (options.minItems !== undefined)
|
|
122
|
+
schema.minItems = options.minItems;
|
|
123
|
+
if (options.maxItems !== undefined)
|
|
124
|
+
schema.maxItems = options.maxItems;
|
|
125
|
+
return schema;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
export function object(shape, options = {}) {
|
|
129
|
+
const entries = Object.entries(shape);
|
|
130
|
+
const additional = options.additionalProperties ?? false;
|
|
131
|
+
return define((value, path) => {
|
|
132
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
133
|
+
return fail(path, "expected an object", "type");
|
|
134
|
+
}
|
|
135
|
+
const source = value;
|
|
136
|
+
const out = {};
|
|
137
|
+
const issues = [];
|
|
138
|
+
for (const [key, schema] of entries) {
|
|
139
|
+
const present = Object.prototype.hasOwnProperty.call(source, key) && source[key] !== undefined;
|
|
140
|
+
if (!present) {
|
|
141
|
+
if (schema.isOptional)
|
|
142
|
+
continue;
|
|
143
|
+
issues.push({ path: [...path, key], message: "is required", code: "required" });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const result = schema.validate(source[key], [...path, key]);
|
|
147
|
+
if (result.ok) {
|
|
148
|
+
if (result.value !== undefined)
|
|
149
|
+
out[key] = result.value;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
issues.push(...result.issues);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!additional) {
|
|
156
|
+
for (const key of Object.keys(source)) {
|
|
157
|
+
if (!Object.prototype.hasOwnProperty.call(shape, key)) {
|
|
158
|
+
issues.push({ path: [...path, key], message: "is not an accepted property", code: "additionalProperties" });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
for (const [key, entry] of Object.entries(source)) {
|
|
164
|
+
if (!Object.prototype.hasOwnProperty.call(shape, key))
|
|
165
|
+
out[key] = entry;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return issues.length ? { ok: false, issues } : ok(out);
|
|
169
|
+
}, () => {
|
|
170
|
+
const properties = {};
|
|
171
|
+
const required = [];
|
|
172
|
+
for (const [key, schema] of entries) {
|
|
173
|
+
properties[key] = schema.toJSONSchema();
|
|
174
|
+
if (!schema.isOptional)
|
|
175
|
+
required.push(key);
|
|
176
|
+
}
|
|
177
|
+
const schema = { type: "object", properties, additionalProperties: additional };
|
|
178
|
+
if (required.length)
|
|
179
|
+
schema.required = required;
|
|
180
|
+
if (options.title)
|
|
181
|
+
schema.title = options.title;
|
|
182
|
+
if (options.description)
|
|
183
|
+
schema.description = options.description;
|
|
184
|
+
return schema;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
export function record(value) {
|
|
188
|
+
return define((candidate, path) => {
|
|
189
|
+
if (typeof candidate !== "object" || candidate === null || Array.isArray(candidate)) {
|
|
190
|
+
return fail(path, "expected an object", "type");
|
|
191
|
+
}
|
|
192
|
+
const out = {};
|
|
193
|
+
const issues = [];
|
|
194
|
+
for (const [key, entry] of Object.entries(candidate)) {
|
|
195
|
+
const result = value.validate(entry, [...path, key]);
|
|
196
|
+
if (result.ok)
|
|
197
|
+
out[key] = result.value;
|
|
198
|
+
else
|
|
199
|
+
issues.push(...result.issues);
|
|
200
|
+
}
|
|
201
|
+
return issues.length ? { ok: false, issues } : ok(out);
|
|
202
|
+
}, () => ({ type: "object", additionalProperties: value.toJSONSchema() }));
|
|
203
|
+
}
|
|
204
|
+
export function union(members) {
|
|
205
|
+
return define((value, path) => {
|
|
206
|
+
for (const member of members) {
|
|
207
|
+
const result = member.validate(value, path);
|
|
208
|
+
if (result.ok)
|
|
209
|
+
return ok(result.value);
|
|
210
|
+
}
|
|
211
|
+
return fail(path, "does not match any accepted variant", "anyOf");
|
|
212
|
+
}, () => ({ anyOf: members.map((member) => member.toJSONSchema()) }));
|
|
213
|
+
}
|
|
214
|
+
export function optional(schema) {
|
|
215
|
+
return {
|
|
216
|
+
appport: "schema",
|
|
217
|
+
isOptional: true,
|
|
218
|
+
validate: (value, path = []) => value === undefined ? ok(undefined) : schema.validate(value, path),
|
|
219
|
+
toJSONSchema: () => schema.toJSONSchema()
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
export function nullable(schema) {
|
|
223
|
+
return define((value, path) => (value === null ? ok(null) : schema.validate(value, path)), () => ({ anyOf: [schema.toJSONSchema(), { type: "null" }] }));
|
|
224
|
+
}
|
|
225
|
+
/** Accepts any JSON value. Used for capabilities without a meaningful shape. */
|
|
226
|
+
export function unknownSchema() {
|
|
227
|
+
return define((value) => ok(value), () => ({}));
|
|
228
|
+
}
|
|
229
|
+
/** The canonical empty input/output (`{}`). */
|
|
230
|
+
export function empty() {
|
|
231
|
+
return object({});
|
|
232
|
+
}
|
|
233
|
+
/** A binary reference (§51). */
|
|
234
|
+
export function blob() {
|
|
235
|
+
return define((value, path) => {
|
|
236
|
+
if (typeof value !== "object" || value === null)
|
|
237
|
+
return fail(path, "expected a blob reference", "type");
|
|
238
|
+
const candidate = value;
|
|
239
|
+
if (candidate.type !== "blob")
|
|
240
|
+
return fail(path, 'expected "type" to be "blob"', "const");
|
|
241
|
+
if (typeof candidate.id !== "string")
|
|
242
|
+
return fail([...path, "id"], "expected a string", "type");
|
|
243
|
+
if (typeof candidate.size !== "number")
|
|
244
|
+
return fail([...path, "size"], "expected a number", "type");
|
|
245
|
+
if (typeof candidate.contentType !== "string") {
|
|
246
|
+
return fail([...path, "contentType"], "expected a string", "type");
|
|
247
|
+
}
|
|
248
|
+
return ok(value);
|
|
249
|
+
}, () => JSON.parse(JSON.stringify(BLOB_REF_SCHEMA)));
|
|
250
|
+
}
|
|
251
|
+
/** ISO-8601 timestamp string. */
|
|
252
|
+
export function timestamp() {
|
|
253
|
+
return string({ format: "date-time", description: "ISO-8601 timestamp" });
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The built-in schema builder. Applications may use it directly, or bring
|
|
257
|
+
* Zod/Valibot through the Standard Schema adapter.
|
|
258
|
+
*/
|
|
259
|
+
export const s = {
|
|
260
|
+
string,
|
|
261
|
+
number,
|
|
262
|
+
integer,
|
|
263
|
+
boolean,
|
|
264
|
+
null: nullSchema,
|
|
265
|
+
literal,
|
|
266
|
+
enum: enumeration,
|
|
267
|
+
array,
|
|
268
|
+
object,
|
|
269
|
+
record,
|
|
270
|
+
union,
|
|
271
|
+
optional,
|
|
272
|
+
nullable,
|
|
273
|
+
unknown: unknownSchema,
|
|
274
|
+
empty,
|
|
275
|
+
blob,
|
|
276
|
+
timestamp
|
|
277
|
+
};
|
|
278
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAiC,MAAM,mBAAmB,CAAC;AAGnF,SAAS,EAAE,CAAI,KAAQ;IACrB,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC;AAED,SAAS,IAAI,CAAC,IAAe,EAAE,OAAe,EAAE,IAAa;IAC3D,MAAM,KAAK,GAAoB,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IACjD,IAAI,IAAI;QAAE,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC;IAC5B,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC;AACxC,CAAC;AAED,SAAS,MAAM,CACb,QAAkE,EAClE,YAA8B;IAE9B,OAAO;QACL,OAAO,EAAE,QAAQ;QACjB,QAAQ,EAAE,CAAC,KAAc,EAAE,OAAkB,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,E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AC5E,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,CAAC,GAAG;IACf,MAAM;IACN,MAAM;IACN,OAAO;IACP,OAAO;IACP,IAAI,EAAE,UAAU;IAChB,OAAO;IACP,IAAI,EAAE,WAAW;IACjB,KAAK;IACL,MAAM;IACN,MAAM;IACN,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,OAAO,EAAE,aAAa;IACtB,KAAK;IACL,IAAI;IACJ,SAAS;CACD,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JSONSchema } from "@appport/protocol";
|
|
2
|
+
import type { Schema } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* A validator for the JSON Schema subset AppPort publishes in manifests
|
|
5
|
+
* (specification §32).
|
|
6
|
+
*
|
|
7
|
+
* This is what makes a manifest independently consumable: a third-party
|
|
8
|
+
* implementation can validate inputs and outputs from the published schema
|
|
9
|
+
* without access to the original TypeScript definitions.
|
|
10
|
+
*/
|
|
11
|
+
export declare function fromJSONSchema<T = unknown>(schema: JSONSchema, root?: JSONSchema): Schema<T>;
|
|
12
|
+
//# sourceMappingURL=from-json-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"from-json-schema.d.ts","sourceRoot":"","sources":["../src/from-json-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAkB,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAa,MAAM,EAAqC,MAAM,YAAY,CAAC;AAEvF;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,GAAE,UAAmB,GAAG,MAAM,CAAC,CAAC,CAAC,CASpG"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A validator for the JSON Schema subset AppPort publishes in manifests
|
|
3
|
+
* (specification §32).
|
|
4
|
+
*
|
|
5
|
+
* This is what makes a manifest independently consumable: a third-party
|
|
6
|
+
* implementation can validate inputs and outputs from the published schema
|
|
7
|
+
* without access to the original TypeScript definitions.
|
|
8
|
+
*/
|
|
9
|
+
export function fromJSONSchema(schema, root = schema) {
|
|
10
|
+
return {
|
|
11
|
+
appport: "schema",
|
|
12
|
+
validate(value, path = []) {
|
|
13
|
+
const issues = validateAgainst(value, schema, root, path);
|
|
14
|
+
return issues.length ? { ok: false, issues } : { ok: true, value: value };
|
|
15
|
+
},
|
|
16
|
+
toJSONSchema: () => schema
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function resolve(schema, root) {
|
|
20
|
+
let current = schema;
|
|
21
|
+
let guard = 0;
|
|
22
|
+
while (current.$ref && guard < 32) {
|
|
23
|
+
guard += 1;
|
|
24
|
+
const ref = current.$ref;
|
|
25
|
+
if (!ref.startsWith("#/$defs/"))
|
|
26
|
+
return current;
|
|
27
|
+
const key = ref.slice("#/$defs/".length);
|
|
28
|
+
const target = root.$defs?.[key];
|
|
29
|
+
if (!target)
|
|
30
|
+
return current;
|
|
31
|
+
current = target;
|
|
32
|
+
}
|
|
33
|
+
return current;
|
|
34
|
+
}
|
|
35
|
+
function typeOfValue(value) {
|
|
36
|
+
if (value === null)
|
|
37
|
+
return "null";
|
|
38
|
+
if (Array.isArray(value))
|
|
39
|
+
return "array";
|
|
40
|
+
if (typeof value === "number")
|
|
41
|
+
return Number.isInteger(value) ? "integer" : "number";
|
|
42
|
+
if (typeof value === "boolean")
|
|
43
|
+
return "boolean";
|
|
44
|
+
if (typeof value === "string")
|
|
45
|
+
return "string";
|
|
46
|
+
return "object";
|
|
47
|
+
}
|
|
48
|
+
function matchesType(value, type) {
|
|
49
|
+
const actual = typeOfValue(value);
|
|
50
|
+
if (type === "number")
|
|
51
|
+
return actual === "number" || actual === "integer";
|
|
52
|
+
return actual === type;
|
|
53
|
+
}
|
|
54
|
+
function deepEqual(a, b) {
|
|
55
|
+
if (a === b)
|
|
56
|
+
return true;
|
|
57
|
+
if (typeof a !== typeof b || a === null || b === null)
|
|
58
|
+
return false;
|
|
59
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
60
|
+
return a.length === b.length && a.every((entry, index) => deepEqual(entry, b[index]));
|
|
61
|
+
}
|
|
62
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
63
|
+
const ka = Object.keys(a);
|
|
64
|
+
const kb = Object.keys(b);
|
|
65
|
+
if (ka.length !== kb.length)
|
|
66
|
+
return false;
|
|
67
|
+
return ka.every((key) => deepEqual(a[key], b[key]));
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
function issue(path, message, code) {
|
|
72
|
+
return { path, message, code };
|
|
73
|
+
}
|
|
74
|
+
function validateAgainst(value, rawSchema, root, path) {
|
|
75
|
+
const schema = resolve(rawSchema, root);
|
|
76
|
+
const issues = [];
|
|
77
|
+
if (schema.const !== undefined && !deepEqual(value, schema.const)) {
|
|
78
|
+
return [issue(path, `expected the literal ${JSON.stringify(schema.const)}`, "const")];
|
|
79
|
+
}
|
|
80
|
+
if (schema.enum && !schema.enum.some((candidate) => deepEqual(candidate, value))) {
|
|
81
|
+
return [issue(path, `expected one of: ${schema.enum.map((v) => JSON.stringify(v)).join(", ")}`, "enum")];
|
|
82
|
+
}
|
|
83
|
+
if (schema.type) {
|
|
84
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
85
|
+
if (!types.some((type) => matchesType(value, type))) {
|
|
86
|
+
return [issue(path, `expected ${types.join(" or ")}`, "type")];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (typeof value === "string") {
|
|
90
|
+
if (schema.minLength !== undefined && value.length < schema.minLength) {
|
|
91
|
+
issues.push(issue(path, `expected at least ${schema.minLength} characters`, "minLength"));
|
|
92
|
+
}
|
|
93
|
+
if (schema.maxLength !== undefined && value.length > schema.maxLength) {
|
|
94
|
+
issues.push(issue(path, `expected at most ${schema.maxLength} characters`, "maxLength"));
|
|
95
|
+
}
|
|
96
|
+
if (schema.pattern !== undefined && !new RegExp(schema.pattern).test(value)) {
|
|
97
|
+
issues.push(issue(path, "does not match the required pattern", "pattern"));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (typeof value === "number") {
|
|
101
|
+
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
102
|
+
issues.push(issue(path, `expected a value >= ${schema.minimum}`, "minimum"));
|
|
103
|
+
}
|
|
104
|
+
if (schema.maximum !== undefined && value > schema.maximum) {
|
|
105
|
+
issues.push(issue(path, `expected a value <= ${schema.maximum}`, "maximum"));
|
|
106
|
+
}
|
|
107
|
+
if (schema.exclusiveMinimum !== undefined && value <= schema.exclusiveMinimum) {
|
|
108
|
+
issues.push(issue(path, `expected a value > ${schema.exclusiveMinimum}`, "exclusiveMinimum"));
|
|
109
|
+
}
|
|
110
|
+
if (schema.exclusiveMaximum !== undefined && value >= schema.exclusiveMaximum) {
|
|
111
|
+
issues.push(issue(path, `expected a value < ${schema.exclusiveMaximum}`, "exclusiveMaximum"));
|
|
112
|
+
}
|
|
113
|
+
if (schema.multipleOf !== undefined && schema.multipleOf > 0 && value % schema.multipleOf !== 0) {
|
|
114
|
+
issues.push(issue(path, `expected a multiple of ${schema.multipleOf}`, "multipleOf"));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (Array.isArray(value)) {
|
|
118
|
+
if (schema.minItems !== undefined && value.length < schema.minItems) {
|
|
119
|
+
issues.push(issue(path, `expected at least ${schema.minItems} items`, "minItems"));
|
|
120
|
+
}
|
|
121
|
+
if (schema.maxItems !== undefined && value.length > schema.maxItems) {
|
|
122
|
+
issues.push(issue(path, `expected at most ${schema.maxItems} items`, "maxItems"));
|
|
123
|
+
}
|
|
124
|
+
if (schema.uniqueItems && value.some((entry, i) => value.findIndex((other) => deepEqual(entry, other)) !== i)) {
|
|
125
|
+
issues.push(issue(path, "expected unique items", "uniqueItems"));
|
|
126
|
+
}
|
|
127
|
+
if (schema.items) {
|
|
128
|
+
value.forEach((entry, index) => {
|
|
129
|
+
issues.push(...validateAgainst(entry, schema.items, root, [...path, index]));
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (typeOfValue(value) === "object") {
|
|
134
|
+
const source = value;
|
|
135
|
+
for (const key of schema.required ?? []) {
|
|
136
|
+
if (!Object.prototype.hasOwnProperty.call(source, key) || source[key] === undefined) {
|
|
137
|
+
issues.push(issue([...path, key], "is required", "required"));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const properties = schema.properties ?? {};
|
|
141
|
+
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
142
|
+
if (Object.prototype.hasOwnProperty.call(source, key) && source[key] !== undefined) {
|
|
143
|
+
issues.push(...validateAgainst(source[key], propertySchema, root, [...path, key]));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (schema.additionalProperties === false) {
|
|
147
|
+
for (const key of Object.keys(source)) {
|
|
148
|
+
if (!Object.prototype.hasOwnProperty.call(properties, key)) {
|
|
149
|
+
issues.push(issue([...path, key], "is not an accepted property", "additionalProperties"));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (typeof schema.additionalProperties === "object") {
|
|
154
|
+
for (const [key, entry] of Object.entries(source)) {
|
|
155
|
+
if (!Object.prototype.hasOwnProperty.call(properties, key)) {
|
|
156
|
+
issues.push(...validateAgainst(entry, schema.additionalProperties, root, [...path, key]));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (schema.propertyNames) {
|
|
161
|
+
for (const key of Object.keys(source)) {
|
|
162
|
+
issues.push(...validateAgainst(key, schema.propertyNames, root, [...path, key]));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (schema.allOf) {
|
|
167
|
+
for (const member of schema.allOf)
|
|
168
|
+
issues.push(...validateAgainst(value, member, root, path));
|
|
169
|
+
}
|
|
170
|
+
if (schema.anyOf) {
|
|
171
|
+
const matched = schema.anyOf.some((member) => validateAgainst(value, member, root, path).length === 0);
|
|
172
|
+
if (!matched)
|
|
173
|
+
issues.push(issue(path, "does not match any accepted variant", "anyOf"));
|
|
174
|
+
}
|
|
175
|
+
if (schema.oneOf) {
|
|
176
|
+
const matches = schema.oneOf.filter((member) => validateAgainst(value, member, root, path).length === 0).length;
|
|
177
|
+
if (matches !== 1) {
|
|
178
|
+
issues.push(issue(path, `expected exactly one matching variant, matched ${matches}`, "oneOf"));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (schema.not && validateAgainst(value, schema.not, root, path).length === 0) {
|
|
182
|
+
issues.push(issue(path, "matches an excluded schema", "not"));
|
|
183
|
+
}
|
|
184
|
+
return issues;
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=from-json-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"from-json-schema.js","sourceRoot":"","sources":["../src/from-json-schema.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAc,MAAkB,EAAE,OAAmB,MAAM;IACvF,OAAO;QACL,OAAO,EAAE,QAAQ;QACjB,QAAQ,CAAC,KAAc,EAAE,OAAkB,EAAE;YAC3C,MAAM,MAAM,GAAG,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAC1D,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAU,EAAE,CAAC;QACjF,CAAC;QACD,YAAY,EAAE,GAAG,EAAE,CAAC,MAAM;KAC3B,CAAC;AACJ,CAAC;AAED,SAAS,OAAO,CAAC,MAAkB,EAAE,IAAgB;IACnD,IAAI,OAAO,GAAG,MAAM,CAAC;IACrB,IAAI,KAAK,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C,CAAC,CAAC;QACjG,CAAC;IACH,CAAC;IACD,IAAI,MAAM,CAAC,GAAG,IAAI,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9E,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,4BAA4B,EAAE,KAAK,CAAC,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@appport/schema` — schema adapters and JSON Schema generation.
|
|
3
|
+
*
|
|
4
|
+
* AppPort publishes JSON Schema on the wire (§32) but accepts several authoring
|
|
5
|
+
* formats in TypeScript: the built-in builder, any Standard Schema library, or
|
|
6
|
+
* a raw JSON Schema document.
|
|
7
|
+
*/
|
|
8
|
+
import type { JSONSchema } from "@appport/protocol";
|
|
9
|
+
import { type StandardSchemaV1 } from "./standard.js";
|
|
10
|
+
import { type Schema } from "./types.js";
|
|
11
|
+
export * from "./types.js";
|
|
12
|
+
export * from "./builder.js";
|
|
13
|
+
export * from "./from-json-schema.js";
|
|
14
|
+
export * from "./standard.js";
|
|
15
|
+
/** Anything AppPort accepts where a schema is expected. */
|
|
16
|
+
export type SchemaInput<T = unknown> = Schema<T> | StandardSchemaV1<unknown, T> | JSONSchema;
|
|
17
|
+
/**
|
|
18
|
+
* Normalizes any accepted schema representation into an AppPort {@link Schema}.
|
|
19
|
+
*/
|
|
20
|
+
export declare function toSchema<T = unknown>(input: SchemaInput<T>): Schema<T>;
|
|
21
|
+
/** Renders any accepted schema representation as JSON Schema. */
|
|
22
|
+
export declare function toJSONSchema(input: SchemaInput): JSONSchema;
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAwC,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAC5F,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,YAAY,CAAC;AAEnD,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAE9B,2DAA2D;AAC3D,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,UAAU,CAAC;AAE7F;;GAEG;AACH,wBAAgB,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAItE;AAED,iEAAiE;AACjE,wBAAgB,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,UAAU,CAE3D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { fromJSONSchema } from "./from-json-schema.js";
|
|
2
|
+
import { fromStandardSchema, isStandardSchema } from "./standard.js";
|
|
3
|
+
import { isSchema } from "./types.js";
|
|
4
|
+
export * from "./types.js";
|
|
5
|
+
export * from "./builder.js";
|
|
6
|
+
export * from "./from-json-schema.js";
|
|
7
|
+
export * from "./standard.js";
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes any accepted schema representation into an AppPort {@link Schema}.
|
|
10
|
+
*/
|
|
11
|
+
export function toSchema(input) {
|
|
12
|
+
if (isSchema(input))
|
|
13
|
+
return input;
|
|
14
|
+
if (isStandardSchema(input))
|
|
15
|
+
return fromStandardSchema(input);
|
|
16
|
+
return fromJSONSchema(input);
|
|
17
|
+
}
|
|
18
|
+
/** Renders any accepted schema representation as JSON Schema. */
|
|
19
|
+
export function toJSONSchema(input) {
|
|
20
|
+
return toSchema(input).toJSONSchema();
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAyB,MAAM,eAAe,CAAC;AAC5F,OAAO,EAAE,QAAQ,EAAe,MAAM,YAAY,CAAC;AAEnD,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAK9B;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAc,KAAqB;IACzD,IAAI,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAkB,CAAC;IAC/C,IAAI,gBAAgB,CAAC,KAAK,CAAC;QAAE,OAAO,kBAAkB,CAAC,KAAqC,CAAC,CAAC;IAC9F,OAAO,cAAc,CAAI,KAAmB,CAAC,CAAC;AAChD,CAAC;AAED,iEAAiE;AACjE,MAAM,UAAU,YAAY,CAAC,KAAkB;IAC7C,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { JSONSchema } from "@appport/protocol";
|
|
2
|
+
import type { Schema } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Minimal structural typing for the Standard Schema v1 interface implemented by
|
|
5
|
+
* Zod, Valibot, ArkType and others. Bringing one of those libraries requires no
|
|
6
|
+
* dependency here — only the shared `~standard` property.
|
|
7
|
+
*/
|
|
8
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
9
|
+
readonly "~standard": {
|
|
10
|
+
readonly version: 1;
|
|
11
|
+
readonly vendor: string;
|
|
12
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
13
|
+
readonly types?: {
|
|
14
|
+
readonly input: Input;
|
|
15
|
+
readonly output: Output;
|
|
16
|
+
} | undefined;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
interface StandardResult<Output> {
|
|
20
|
+
value?: Output;
|
|
21
|
+
issues?: ReadonlyArray<{
|
|
22
|
+
message: string;
|
|
23
|
+
path?: ReadonlyArray<PropertyKey | {
|
|
24
|
+
key: PropertyKey;
|
|
25
|
+
}>;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
export declare function isStandardSchema(value: unknown): value is StandardSchemaV1;
|
|
29
|
+
export interface StandardAdapterOptions {
|
|
30
|
+
/**
|
|
31
|
+
* JSON Schema for the manifest. Standard Schema does not expose one, so pass
|
|
32
|
+
* the library's own conversion (`z.toJSONSchema(schema)` for Zod 4) or a
|
|
33
|
+
* hand-written schema. Without it the manifest advertises an opaque schema,
|
|
34
|
+
* which downgrades cross-language interoperability.
|
|
35
|
+
*/
|
|
36
|
+
jsonSchema?: JSONSchema;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Adapts a Standard Schema (Zod, Valibot, …) to the AppPort schema contract.
|
|
40
|
+
*
|
|
41
|
+
* Validation must be synchronous: AppPort validates inside the dispatch
|
|
42
|
+
* pipeline, and an async validator would change request ordering semantics.
|
|
43
|
+
*/
|
|
44
|
+
export declare function fromStandardSchema<Output>(schema: StandardSchemaV1<unknown, Output>, options?: StandardAdapterOptions): Schema<Output>;
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=standard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard.d.ts","sourceRoot":"","sources":["../src/standard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAa,MAAM,EAAqC,MAAM,YAAY,CAAC;AAEvF;;;;GAIG;AACH,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC/D,QAAQ,CAAC,WAAW,EAAE;QACpB,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,cAAc,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;QAChG,QAAQ,CAAC,KAAK,CAAC,EAAE;YAAE,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;YAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;SAAE,GAAG,SAAS,CAAC;KACjF,CAAC;CACH;AAED,UAAU,cAAc,CAAC,MAAM;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,aAAa,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,aAAa,CAAC,WAAW,GAAG;YAAE,GAAG,EAAE,WAAW,CAAA;SAAE,CAAC,CAAA;KAAE,CAAC,CAAC;CACvG;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAE1E;AAgBD,MAAM,WAAW,sBAAsB;IACrC;;;;;OAKG;IACH,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EACvC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,EACzC,OAAO,GAAE,sBAA2B,GACnC,MAAM,CAAC,MAAM,CAAC,CAwBhB"}
|
package/dist/standard.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function isStandardSchema(value) {
|
|
2
|
+
return typeof value === "object" && value !== null && "~standard" in value;
|
|
3
|
+
}
|
|
4
|
+
function toIssuePath(path, base) {
|
|
5
|
+
if (!path)
|
|
6
|
+
return base;
|
|
7
|
+
return [
|
|
8
|
+
...base,
|
|
9
|
+
...path.map((segment) => {
|
|
10
|
+
const key = typeof segment === "object" && segment !== null ? segment.key : segment;
|
|
11
|
+
return typeof key === "number" ? key : String(key);
|
|
12
|
+
})
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Adapts a Standard Schema (Zod, Valibot, …) to the AppPort schema contract.
|
|
17
|
+
*
|
|
18
|
+
* Validation must be synchronous: AppPort validates inside the dispatch
|
|
19
|
+
* pipeline, and an async validator would change request ordering semantics.
|
|
20
|
+
*/
|
|
21
|
+
export function fromStandardSchema(schema, options = {}) {
|
|
22
|
+
const jsonSchema = options.jsonSchema ?? { "x-appport-schema": "opaque", description: "Schema not published" };
|
|
23
|
+
return {
|
|
24
|
+
appport: "schema",
|
|
25
|
+
validate(value, path = []) {
|
|
26
|
+
const result = schema["~standard"].validate(value);
|
|
27
|
+
if (result instanceof Promise) {
|
|
28
|
+
throw new TypeError("AppPort requires synchronous schema validation; this Standard Schema returned a Promise");
|
|
29
|
+
}
|
|
30
|
+
if (result.issues && result.issues.length > 0) {
|
|
31
|
+
const issues = result.issues.map((entry) => ({
|
|
32
|
+
path: toIssuePath(entry.path, path),
|
|
33
|
+
message: entry.message
|
|
34
|
+
}));
|
|
35
|
+
return { ok: false, issues };
|
|
36
|
+
}
|
|
37
|
+
return { ok: true, value: result.value };
|
|
38
|
+
},
|
|
39
|
+
toJSONSchema: () => jsonSchema
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=standard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard.js","sourceRoot":"","sources":["../src/standard.ts"],"names":[],"mappings":"AAsBA,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAA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EAAE,IAAI,CAAC;oBACnC,OAAO,EAAE,KAAK,CAAC,OAAO;iBACvB,CAAC,CAAC,CAAC;gBACJ,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;YAC/B,CAAC;YACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,KAAe,EAAE,CAAC;QACrD,CAAC;QACD,YAAY,EAAE,GAAG,EAAE,CAAC,UAAU;KAC/B,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { JSONSchema } from "@appport/protocol";
|
|
2
|
+
/** Path to the offending value inside a validated payload. */
|
|
3
|
+
export type IssuePath = Array<string | number>;
|
|
4
|
+
export interface ValidationIssue {
|
|
5
|
+
path: IssuePath;
|
|
6
|
+
message: string;
|
|
7
|
+
/** Optional machine-readable reason, e.g. `type`, `required`, `pattern`. */
|
|
8
|
+
code?: string;
|
|
9
|
+
}
|
|
10
|
+
export type ValidationResult<T> = {
|
|
11
|
+
ok: true;
|
|
12
|
+
value: T;
|
|
13
|
+
} | {
|
|
14
|
+
ok: false;
|
|
15
|
+
issues: ValidationIssue[];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The schema contract used by the AppPort runtime.
|
|
19
|
+
*
|
|
20
|
+
* A schema does two things: it validates an unknown value at runtime, and it
|
|
21
|
+
* renders itself as JSON Schema so the manifest stays language-neutral (§32).
|
|
22
|
+
*/
|
|
23
|
+
export interface Schema<T = unknown> {
|
|
24
|
+
readonly appport: "schema";
|
|
25
|
+
/** True when the schema is used as an optional object property. */
|
|
26
|
+
readonly isOptional?: boolean;
|
|
27
|
+
validate(value: unknown, path?: IssuePath): ValidationResult<T>;
|
|
28
|
+
toJSONSchema(): JSONSchema;
|
|
29
|
+
}
|
|
30
|
+
export type Infer<S> = S extends Schema<infer T> ? T : never;
|
|
31
|
+
export declare function isSchema(value: unknown): value is Schema;
|
|
32
|
+
/** Renders issues into a compact, non-sensitive detail payload for errors. */
|
|
33
|
+
export declare function issuesToDetails(issues: ValidationIssue[]): {
|
|
34
|
+
issues: Array<{
|
|
35
|
+
path: string;
|
|
36
|
+
message: string;
|
|
37
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
export declare function formatIssues(issues: ValidationIssue[]): string;
|
|
40
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,8DAA8D;AAC9D,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;AAE/C,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,4EAA4E;IAC5E,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,eAAe,EAAE,CAAA;CAAE,CAAC;AAEpG;;;;;GAKG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC,GAAG,OAAO;IACjC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,mEAAmE;IACnE,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAChE,YAAY,IAAI,UAAU,CAAC;CAC5B;AAED,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAE7D,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED,8EAA8E;AAC9E,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe,EAAE,GAAG;IAAE,MAAM,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;CAAE,CAO/G;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,EAAE,GAAG,MAAM,CAK9D"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function isSchema(value) {
|
|
2
|
+
return typeof value === "object" && value !== null && value.appport === "schema";
|
|
3
|
+
}
|
|
4
|
+
/** Renders issues into a compact, non-sensitive detail payload for errors. */
|
|
5
|
+
export function issuesToDetails(issues) {
|
|
6
|
+
return {
|
|
7
|
+
issues: issues.slice(0, 25).map((issue) => ({
|
|
8
|
+
path: issue.path.length === 0 ? "" : issue.path.map(String).join("."),
|
|
9
|
+
message: issue.message
|
|
10
|
+
}))
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function formatIssues(issues) {
|
|
14
|
+
return issues
|
|
15
|
+
.slice(0, 5)
|
|
16
|
+
.map((issue) => (issue.path.length ? `${issue.path.map(String).join(".")}: ${issue.message}` : issue.message))
|
|
17
|
+
.join("; ");
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA8BA,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAAgB,CAAC,OAAO,KAAK,QAAQ,CAAC;AAC/F,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,eAAe,CAAC,MAAyB;IACvD,OAAO;QACL,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YAC1C,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;YACrE,OAAO,EAAE,KAAK,CAAC,OAAO;SACvB,CAAC,CAAC;KACJ,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,MAAyB;IACpD,OAAO,MAAM;SACV,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;SACX,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;SAC7G,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@appport/schema",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Schema adapters and JSON Schema generation for AppPort capabilities.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": { "type": "git", "url": "git+https://github.com/rkendel1/appport.git", "directory": "packages/schema" },
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -b",
|
|
22
|
+
"clean": "tsc -b --clean"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@appport/protocol": "1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": { "access": "public" }
|
|
28
|
+
}
|