@agentuity/schema 0.0.108 → 0.0.109
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/dist/json-schema.d.ts.map +1 -1
- package/dist/json-schema.js +51 -24
- package/dist/json-schema.js.map +1 -1
- package/package.json +3 -3
- package/src/json-schema.ts +55 -24
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-schema.d.ts","sourceRoot":"","sources":["../src/json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"json-schema.d.ts","sourceRoot":"","sources":["../src/json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAoBrC;;;GAGG;AACH,MAAM,WAAW,UAAU;IAC1B,IAAI,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,CAAC;IACjF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;IAClC,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,UAAU,CAqIjE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAoJvE"}
|
package/dist/json-schema.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
1
|
+
import { string } from './primitives/string';
|
|
2
|
+
import { number } from './primitives/number';
|
|
3
|
+
import { boolean } from './primitives/boolean';
|
|
4
|
+
import { null_ } from './primitives/null';
|
|
5
|
+
import { object } from './complex/object';
|
|
6
|
+
import { array } from './complex/array';
|
|
7
|
+
import { literal } from './utils/literal';
|
|
8
|
+
import { optional } from './utils/optional';
|
|
9
|
+
import { nullable } from './utils/nullable';
|
|
10
|
+
import { union } from './utils/union';
|
|
11
|
+
/**
|
|
12
|
+
* Check schema type by constructor name (works across bundled modules).
|
|
13
|
+
* Using instanceof fails when code is bundled or multiple copies of the package exist.
|
|
14
|
+
*/
|
|
15
|
+
function isSchemaType(schema, typeName) {
|
|
16
|
+
return schema?.constructor?.name === typeName;
|
|
17
|
+
}
|
|
12
18
|
/**
|
|
13
19
|
* Convert a schema to JSON Schema format.
|
|
14
20
|
* Supports primitives, objects, arrays, unions, literals, optional, and nullable types.
|
|
@@ -33,30 +39,30 @@ export function toJSONSchema(schema) {
|
|
|
33
39
|
if (schema.description) {
|
|
34
40
|
result.description = schema.description;
|
|
35
41
|
}
|
|
36
|
-
// Primitive types
|
|
37
|
-
if (schema
|
|
42
|
+
// Primitive types - use constructor name checks (works across bundled modules)
|
|
43
|
+
if (isSchemaType(schema, 'StringSchema') || isSchemaType(schema, 'CoerceStringSchema')) {
|
|
38
44
|
result.type = 'string';
|
|
39
45
|
return result;
|
|
40
46
|
}
|
|
41
|
-
if (schema
|
|
47
|
+
if (isSchemaType(schema, 'NumberSchema') || isSchemaType(schema, 'CoerceNumberSchema')) {
|
|
42
48
|
result.type = 'number';
|
|
43
49
|
return result;
|
|
44
50
|
}
|
|
45
|
-
if (schema
|
|
51
|
+
if (isSchemaType(schema, 'BooleanSchema') || isSchemaType(schema, 'CoerceBooleanSchema')) {
|
|
46
52
|
result.type = 'boolean';
|
|
47
53
|
return result;
|
|
48
54
|
}
|
|
49
|
-
if (schema
|
|
55
|
+
if (isSchemaType(schema, 'NullSchema')) {
|
|
50
56
|
result.type = 'null';
|
|
51
57
|
return result;
|
|
52
58
|
}
|
|
53
|
-
if (schema
|
|
59
|
+
if (isSchemaType(schema, 'UndefinedSchema')) {
|
|
54
60
|
// JSON Schema doesn't have a direct "undefined" type
|
|
55
61
|
// We can represent it as an empty schema or omit the field
|
|
56
62
|
return {};
|
|
57
63
|
}
|
|
58
64
|
// Literal types
|
|
59
|
-
if (schema
|
|
65
|
+
if (isSchemaType(schema, 'LiteralSchema')) {
|
|
60
66
|
const value = schema.value;
|
|
61
67
|
result.const = value;
|
|
62
68
|
if (typeof value === 'string') {
|
|
@@ -71,7 +77,7 @@ export function toJSONSchema(schema) {
|
|
|
71
77
|
return result;
|
|
72
78
|
}
|
|
73
79
|
// Object types
|
|
74
|
-
if (schema
|
|
80
|
+
if (isSchemaType(schema, 'ObjectSchema')) {
|
|
75
81
|
result.type = 'object';
|
|
76
82
|
const shape = schema.shape;
|
|
77
83
|
result.properties = {};
|
|
@@ -79,7 +85,7 @@ export function toJSONSchema(schema) {
|
|
|
79
85
|
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
80
86
|
result.properties[key] = toJSONSchema(fieldSchema);
|
|
81
87
|
// If the field is not optional, add it to required
|
|
82
|
-
if (!(fieldSchema
|
|
88
|
+
if (!isSchemaType(fieldSchema, 'OptionalSchema')) {
|
|
83
89
|
result.required.push(key);
|
|
84
90
|
}
|
|
85
91
|
}
|
|
@@ -90,21 +96,21 @@ export function toJSONSchema(schema) {
|
|
|
90
96
|
return result;
|
|
91
97
|
}
|
|
92
98
|
// Array types
|
|
93
|
-
if (schema
|
|
99
|
+
if (isSchemaType(schema, 'ArraySchema')) {
|
|
94
100
|
result.type = 'array';
|
|
95
101
|
const itemSchema = schema.itemSchema;
|
|
96
102
|
result.items = toJSONSchema(itemSchema);
|
|
97
103
|
return result;
|
|
98
104
|
}
|
|
99
105
|
// Optional types
|
|
100
|
-
if (schema
|
|
106
|
+
if (isSchemaType(schema, 'OptionalSchema')) {
|
|
101
107
|
const innerSchema = schema.schema;
|
|
102
108
|
const innerJSON = toJSONSchema(innerSchema);
|
|
103
109
|
// Optional is typically handled at the object level via required array
|
|
104
110
|
return innerJSON;
|
|
105
111
|
}
|
|
106
112
|
// Nullable types
|
|
107
|
-
if (schema
|
|
113
|
+
if (isSchemaType(schema, 'NullableSchema')) {
|
|
108
114
|
const innerSchema = schema.schema;
|
|
109
115
|
const innerJSON = toJSONSchema(innerSchema);
|
|
110
116
|
// Nullable can be represented as anyOf with null
|
|
@@ -114,11 +120,32 @@ export function toJSONSchema(schema) {
|
|
|
114
120
|
};
|
|
115
121
|
}
|
|
116
122
|
// Union types
|
|
117
|
-
if (schema
|
|
123
|
+
if (isSchemaType(schema, 'UnionSchema')) {
|
|
118
124
|
const schemas = schema.schemas;
|
|
119
125
|
result.anyOf = schemas.map((schema) => toJSONSchema(schema));
|
|
120
126
|
return result;
|
|
121
127
|
}
|
|
128
|
+
// Record types (object with string keys and typed values)
|
|
129
|
+
if (isSchemaType(schema, 'RecordSchema')) {
|
|
130
|
+
result.type = 'object';
|
|
131
|
+
// Record schemas have additionalProperties
|
|
132
|
+
const valueSchema = schema.valueSchema;
|
|
133
|
+
if (valueSchema) {
|
|
134
|
+
result.additionalProperties = toJSONSchema(valueSchema);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
// Unknown/Any types - accept anything
|
|
139
|
+
if (isSchemaType(schema, 'UnknownSchema') || isSchemaType(schema, 'AnySchema')) {
|
|
140
|
+
// Return empty schema (accepts any value in JSON Schema)
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
// Coerce date - represented as string in JSON Schema
|
|
144
|
+
if (isSchemaType(schema, 'CoerceDateSchema')) {
|
|
145
|
+
result.type = 'string';
|
|
146
|
+
result.format = 'date-time';
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
122
149
|
// Fallback for unknown schema types
|
|
123
150
|
return result;
|
|
124
151
|
}
|
package/dist/json-schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-schema.js","sourceRoot":"","sources":["../src/json-schema.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"json-schema.js","sourceRoot":"","sources":["../src/json-schema.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC;;;GAGG;AACH,SAAS,YAAY,CAAC,MAAW,EAAE,QAAgB;IAClD,OAAO,MAAM,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC;AAC/C,CAAC;AAmBD;;;;;;;;;;;;;;;;;G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CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;oBAC9B,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;gBACrC,CAAC;gBAED,KAAK,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC;YAC1B,CAAC;YAED,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7B,IAAI,UAAU,CAAC,WAAW,EAAE,CAAC;gBAC5B,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;YACzC,CAAC;YACD,OAAO,MAAM,CAAC;QACf,CAAC;QAED,OAAO,CAAC,CAAC,CAAC;YACT,8DAA8D;YAC9D,IAAI,UAAU,CAAC,UAAU,EAAE,CAAC;gBAC3B,kBAAkB;gBAClB,OAAO,cAAc,CAAC,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC1D,CAAC;YACD,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC;gBACtB,iBAAiB;gBACjB,OAAO,cAAc,CAAC,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;YACzD,CAAC;YACD,iEAAiE;YACjE,gEAAgE;YAChE,OAAO,MAAM,EAAE,CAAC;QACjB,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentuity/schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.109",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"author": "Agentuity employees and contributors",
|
|
6
6
|
"type": "module",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"prepublishOnly": "bun run clean && bun run build"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@agentuity/core": "0.0.
|
|
29
|
+
"@agentuity/core": "0.0.109"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@agentuity/test-utils": "0.0.
|
|
32
|
+
"@agentuity/test-utils": "0.0.109",
|
|
33
33
|
"@types/bun": "latest",
|
|
34
34
|
"bun-types": "latest",
|
|
35
35
|
"typescript": "^5.9.0"
|
package/src/json-schema.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import type { Schema } from './base';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
|
|
3
|
+
import { string } from './primitives/string';
|
|
4
|
+
import { number } from './primitives/number';
|
|
5
|
+
import { boolean } from './primitives/boolean';
|
|
6
|
+
import { null_ } from './primitives/null';
|
|
7
|
+
import { object } from './complex/object';
|
|
8
|
+
import { array } from './complex/array';
|
|
9
|
+
import { literal } from './utils/literal';
|
|
10
|
+
import { optional } from './utils/optional';
|
|
11
|
+
import { nullable } from './utils/nullable';
|
|
12
|
+
import { union } from './utils/union';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Check schema type by constructor name (works across bundled modules).
|
|
16
|
+
* Using instanceof fails when code is bundled or multiple copies of the package exist.
|
|
17
|
+
*/
|
|
18
|
+
function isSchemaType(schema: any, typeName: string): boolean {
|
|
19
|
+
return schema?.constructor?.name === typeName;
|
|
20
|
+
}
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
23
|
* JSON Schema object representation.
|
|
@@ -55,35 +62,35 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
55
62
|
result.description = schema.description;
|
|
56
63
|
}
|
|
57
64
|
|
|
58
|
-
// Primitive types
|
|
59
|
-
if (schema
|
|
65
|
+
// Primitive types - use constructor name checks (works across bundled modules)
|
|
66
|
+
if (isSchemaType(schema, 'StringSchema') || isSchemaType(schema, 'CoerceStringSchema')) {
|
|
60
67
|
result.type = 'string';
|
|
61
68
|
return result;
|
|
62
69
|
}
|
|
63
70
|
|
|
64
|
-
if (schema
|
|
71
|
+
if (isSchemaType(schema, 'NumberSchema') || isSchemaType(schema, 'CoerceNumberSchema')) {
|
|
65
72
|
result.type = 'number';
|
|
66
73
|
return result;
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
if (schema
|
|
76
|
+
if (isSchemaType(schema, 'BooleanSchema') || isSchemaType(schema, 'CoerceBooleanSchema')) {
|
|
70
77
|
result.type = 'boolean';
|
|
71
78
|
return result;
|
|
72
79
|
}
|
|
73
80
|
|
|
74
|
-
if (schema
|
|
81
|
+
if (isSchemaType(schema, 'NullSchema')) {
|
|
75
82
|
result.type = 'null';
|
|
76
83
|
return result;
|
|
77
84
|
}
|
|
78
85
|
|
|
79
|
-
if (schema
|
|
86
|
+
if (isSchemaType(schema, 'UndefinedSchema')) {
|
|
80
87
|
// JSON Schema doesn't have a direct "undefined" type
|
|
81
88
|
// We can represent it as an empty schema or omit the field
|
|
82
89
|
return {};
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
// Literal types
|
|
86
|
-
if (schema
|
|
93
|
+
if (isSchemaType(schema, 'LiteralSchema')) {
|
|
87
94
|
const value = (schema as any).value;
|
|
88
95
|
result.const = value;
|
|
89
96
|
if (typeof value === 'string') {
|
|
@@ -97,7 +104,7 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
// Object types
|
|
100
|
-
if (schema
|
|
107
|
+
if (isSchemaType(schema, 'ObjectSchema')) {
|
|
101
108
|
result.type = 'object';
|
|
102
109
|
const shape = (schema as any).shape;
|
|
103
110
|
result.properties = {};
|
|
@@ -107,7 +114,7 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
107
114
|
result.properties[key] = toJSONSchema(fieldSchema);
|
|
108
115
|
|
|
109
116
|
// If the field is not optional, add it to required
|
|
110
|
-
if (!(fieldSchema
|
|
117
|
+
if (!isSchemaType(fieldSchema, 'OptionalSchema')) {
|
|
111
118
|
result.required.push(key);
|
|
112
119
|
}
|
|
113
120
|
}
|
|
@@ -121,7 +128,7 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
121
128
|
}
|
|
122
129
|
|
|
123
130
|
// Array types
|
|
124
|
-
if (schema
|
|
131
|
+
if (isSchemaType(schema, 'ArraySchema')) {
|
|
125
132
|
result.type = 'array';
|
|
126
133
|
const itemSchema = (schema as any).itemSchema;
|
|
127
134
|
result.items = toJSONSchema(itemSchema);
|
|
@@ -129,7 +136,7 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
129
136
|
}
|
|
130
137
|
|
|
131
138
|
// Optional types
|
|
132
|
-
if (schema
|
|
139
|
+
if (isSchemaType(schema, 'OptionalSchema')) {
|
|
133
140
|
const innerSchema = (schema as any).schema;
|
|
134
141
|
const innerJSON = toJSONSchema(innerSchema);
|
|
135
142
|
// Optional is typically handled at the object level via required array
|
|
@@ -137,7 +144,7 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
137
144
|
}
|
|
138
145
|
|
|
139
146
|
// Nullable types
|
|
140
|
-
if (schema
|
|
147
|
+
if (isSchemaType(schema, 'NullableSchema')) {
|
|
141
148
|
const innerSchema = (schema as any).schema;
|
|
142
149
|
const innerJSON = toJSONSchema(innerSchema);
|
|
143
150
|
// Nullable can be represented as anyOf with null
|
|
@@ -148,12 +155,36 @@ export function toJSONSchema(schema: Schema<any, any>): JSONSchema {
|
|
|
148
155
|
}
|
|
149
156
|
|
|
150
157
|
// Union types
|
|
151
|
-
if (schema
|
|
158
|
+
if (isSchemaType(schema, 'UnionSchema')) {
|
|
152
159
|
const schemas = (schema as any).schemas as Schema<any, any>[];
|
|
153
160
|
result.anyOf = schemas.map((schema) => toJSONSchema(schema));
|
|
154
161
|
return result;
|
|
155
162
|
}
|
|
156
163
|
|
|
164
|
+
// Record types (object with string keys and typed values)
|
|
165
|
+
if (isSchemaType(schema, 'RecordSchema')) {
|
|
166
|
+
result.type = 'object';
|
|
167
|
+
// Record schemas have additionalProperties
|
|
168
|
+
const valueSchema = (schema as any).valueSchema;
|
|
169
|
+
if (valueSchema) {
|
|
170
|
+
(result as any).additionalProperties = toJSONSchema(valueSchema);
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Unknown/Any types - accept anything
|
|
176
|
+
if (isSchemaType(schema, 'UnknownSchema') || isSchemaType(schema, 'AnySchema')) {
|
|
177
|
+
// Return empty schema (accepts any value in JSON Schema)
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Coerce date - represented as string in JSON Schema
|
|
182
|
+
if (isSchemaType(schema, 'CoerceDateSchema')) {
|
|
183
|
+
result.type = 'string';
|
|
184
|
+
(result as any).format = 'date-time';
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
|
|
157
188
|
// Fallback for unknown schema types
|
|
158
189
|
return result;
|
|
159
190
|
}
|