@bedrockio/yada 1.10.6 → 1.11.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/CHANGELOG.md +9 -0
- package/dist/cjs/Schema.js +15 -8
- package/dist/cjs/array.js +21 -0
- package/dist/cjs/object.js +39 -3
- package/dist/cjs/openai.js +51 -0
- package/dist/cjs/tuple.js +14 -0
- package/package.json +5 -5
- package/src/Schema.js +14 -7
- package/src/array.js +21 -0
- package/src/object.js +43 -2
- package/src/openai.js +54 -0
- package/src/tuple.js +14 -0
- package/types/Schema.d.ts +6 -5
- package/types/Schema.d.ts.map +1 -1
- package/types/array.d.ts +10 -0
- package/types/array.d.ts.map +1 -1
- package/types/object.d.ts +10 -0
- package/types/object.d.ts.map +1 -1
- package/types/openai.d.ts +2 -0
- package/types/openai.d.ts.map +1 -0
- package/types/tuple.d.ts +54 -0
- package/types/tuple.d.ts.map +1 -1
- package/dist/cjs/formats.js +0 -28
- package/src/formats.js +0 -30
package/CHANGELOG.md
CHANGED
package/dist/cjs/Schema.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.default = void 0;
|
|
|
7
7
|
exports.isSchema = isSchema;
|
|
8
8
|
var _lodash = require("lodash");
|
|
9
9
|
var _errors = require("./errors");
|
|
10
|
-
var
|
|
10
|
+
var _openai = require("./openai");
|
|
11
11
|
var _utils = require("./utils");
|
|
12
12
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform', 'empty'];
|
|
13
13
|
const REQUIRED_TYPES = ['default', 'required', 'missing'];
|
|
@@ -226,18 +226,15 @@ class Schema {
|
|
|
226
226
|
* custom (code-based) assertions will not be output.
|
|
227
227
|
* @param {Object} [options]
|
|
228
228
|
* @param {Function} [options.tag] - Allows adding additional custom tags.
|
|
229
|
-
* @param {'openai'} [options.style] - Constrains schema output. Currently only
|
|
230
|
-
* supports OpenAI which will ensure the resulting schema works with the Structured
|
|
231
|
-
* Outputs API.
|
|
232
229
|
* @param {boolean} [options.stripExtensions] - Strips out JSON schema extensions.
|
|
233
230
|
*/
|
|
234
231
|
toJsonSchema(options) {
|
|
235
232
|
return {
|
|
236
233
|
...this.getType(),
|
|
234
|
+
...this.getFormat(),
|
|
237
235
|
...this.getDefault(),
|
|
238
236
|
...this.getEnum(options),
|
|
239
|
-
...this.getTags(options)
|
|
240
|
-
...this.getFormat(options)
|
|
237
|
+
...this.getTags(options)
|
|
241
238
|
};
|
|
242
239
|
}
|
|
243
240
|
|
|
@@ -249,6 +246,16 @@ class Schema {
|
|
|
249
246
|
return this.toJsonSchema(options);
|
|
250
247
|
}
|
|
251
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Converts the schema to OpenAI's [more strict flavor](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas)
|
|
251
|
+
* for structured output.
|
|
252
|
+
*/
|
|
253
|
+
toOpenAi() {
|
|
254
|
+
return this.transform(schema => {
|
|
255
|
+
return (0, _openai.toOpenAi)(schema);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
252
259
|
/**
|
|
253
260
|
* Export JSON schema when invoked by JSON serializer.
|
|
254
261
|
*/
|
|
@@ -270,11 +277,11 @@ class Schema {
|
|
|
270
277
|
};
|
|
271
278
|
}
|
|
272
279
|
}
|
|
273
|
-
getFormat(
|
|
280
|
+
getFormat() {
|
|
274
281
|
const {
|
|
275
282
|
format
|
|
276
283
|
} = this.meta;
|
|
277
|
-
if (
|
|
284
|
+
if (format) {
|
|
278
285
|
return {
|
|
279
286
|
format
|
|
280
287
|
};
|
package/dist/cjs/array.js
CHANGED
|
@@ -127,6 +127,27 @@ class ArraySchema extends _TypeSchema.default {
|
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Sets a path on the schema using a transform function. Deep
|
|
132
|
+
* fields accept either a string using dot syntax or an array
|
|
133
|
+
* representing the path.
|
|
134
|
+
*
|
|
135
|
+
* @param {string|Array<string>} path The path to set.
|
|
136
|
+
* @param {Function} fn - Transform function that accepts an instance
|
|
137
|
+
* of the schema.
|
|
138
|
+
*/
|
|
139
|
+
set(path, fn) {
|
|
140
|
+
const {
|
|
141
|
+
schema
|
|
142
|
+
} = this.meta;
|
|
143
|
+
if (schema.meta.type !== 'object') {
|
|
144
|
+
throw new Error('Nested object schema required.');
|
|
145
|
+
}
|
|
146
|
+
return new ArraySchema({
|
|
147
|
+
schema: schema.set(path, fn)
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
130
151
|
// Private
|
|
131
152
|
|
|
132
153
|
toString() {
|
package/dist/cjs/object.js
CHANGED
|
@@ -152,6 +152,45 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Sets a path on the schema using a transform function. Deep
|
|
157
|
+
* fields accept either a string using dot syntax or an array
|
|
158
|
+
* representing the path.
|
|
159
|
+
*
|
|
160
|
+
* @param {string|Array<string>} path The path to set.
|
|
161
|
+
* @param {Function} fn - Transform function that accepts an instance
|
|
162
|
+
* of the schema.
|
|
163
|
+
*/
|
|
164
|
+
set(path, fn) {
|
|
165
|
+
if (typeof path === 'string') {
|
|
166
|
+
path = path.split('.');
|
|
167
|
+
}
|
|
168
|
+
const [base, ...rest] = path;
|
|
169
|
+
const {
|
|
170
|
+
fields: currentFields
|
|
171
|
+
} = this.meta;
|
|
172
|
+
const keys = Object.keys(currentFields);
|
|
173
|
+
if (!keys.includes(base)) {
|
|
174
|
+
throw new Error(`Unknown field "${base}".`);
|
|
175
|
+
}
|
|
176
|
+
const fields = {};
|
|
177
|
+
for (let key of keys) {
|
|
178
|
+
const schema = currentFields[key];
|
|
179
|
+
if (key === base) {
|
|
180
|
+
if (rest.length) {
|
|
181
|
+
fields[key] = schema.set(rest, fn);
|
|
182
|
+
} else {
|
|
183
|
+
fields[key] = fn(schema);
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
fields[key] = schema;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return new ObjectSchema({
|
|
190
|
+
fields
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
155
194
|
/**
|
|
156
195
|
* Returns the inner schema of an array field. This only makes
|
|
157
196
|
* sense if the array field holds a single schema, so all other
|
|
@@ -362,9 +401,6 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
362
401
|
options(options) {
|
|
363
402
|
return super.options(options);
|
|
364
403
|
}
|
|
365
|
-
|
|
366
|
-
// Private
|
|
367
|
-
|
|
368
404
|
toJsonSchema(options) {
|
|
369
405
|
const {
|
|
370
406
|
stripUnknown = false
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.toOpenAi = toOpenAi;
|
|
7
|
+
// Confirmed as of: 2026-02-09
|
|
8
|
+
const OPENAI_ALLOWED_FORMATS = ['date-time', 'duration', 'hostname', 'date', 'time', 'email', 'ipv4', 'ipv6', 'uuid'];
|
|
9
|
+
function toOpenAi(schema) {
|
|
10
|
+
const {
|
|
11
|
+
type,
|
|
12
|
+
required,
|
|
13
|
+
format,
|
|
14
|
+
tags
|
|
15
|
+
} = schema.meta;
|
|
16
|
+
if (type === 'object') {
|
|
17
|
+
return schema.required();
|
|
18
|
+
} else if (type === 'array') {
|
|
19
|
+
const {
|
|
20
|
+
schemas
|
|
21
|
+
} = schema.meta;
|
|
22
|
+
if (schemas) {
|
|
23
|
+
return schema.toArray().required();
|
|
24
|
+
} else {
|
|
25
|
+
return schema.required();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// All fields in OpenAI flavor JSON schema must be required,
|
|
30
|
+
// so make fields nullable to allow optional behvior.
|
|
31
|
+
if (!required) {
|
|
32
|
+
schema = schema.required().nullable();
|
|
33
|
+
}
|
|
34
|
+
if (hasInvalidFormat(format)) {
|
|
35
|
+
schema = schema.clone({
|
|
36
|
+
format: null
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (tags) {
|
|
40
|
+
schema = schema.clone({
|
|
41
|
+
tags: null
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return schema;
|
|
45
|
+
}
|
|
46
|
+
function hasInvalidFormat(format) {
|
|
47
|
+
if (!format) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return !OPENAI_ALLOWED_FORMATS.includes(format);
|
|
51
|
+
}
|
package/dist/cjs/tuple.js
CHANGED
|
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = _default;
|
|
7
|
+
var _lodash = require("lodash");
|
|
7
8
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
9
|
+
var _array = _interopRequireDefault(require("./array"));
|
|
8
10
|
var _errors = require("./errors");
|
|
9
11
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
12
|
class TupleSchema extends _Schema.default {
|
|
@@ -77,6 +79,17 @@ class TupleSchema extends _Schema.default {
|
|
|
77
79
|
loose: true
|
|
78
80
|
});
|
|
79
81
|
}
|
|
82
|
+
toArray() {
|
|
83
|
+
const {
|
|
84
|
+
schemas
|
|
85
|
+
} = this.meta;
|
|
86
|
+
const unique = (0, _lodash.uniqBy)(schemas, s => s.meta.type);
|
|
87
|
+
if (unique.length > 1) {
|
|
88
|
+
return (0, _array.default)(new _Schema.default().allow(unique));
|
|
89
|
+
} else {
|
|
90
|
+
return (0, _array.default)(unique[0]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
80
93
|
toString() {
|
|
81
94
|
return 'tuple';
|
|
82
95
|
}
|
|
@@ -86,6 +99,7 @@ class TupleSchema extends _Schema.default {
|
|
|
86
99
|
} = this.meta;
|
|
87
100
|
return {
|
|
88
101
|
...super.toJsonSchema(options),
|
|
102
|
+
items: false,
|
|
89
103
|
prefixItems: schemas.map(schema => {
|
|
90
104
|
return schema.toJsonSchema(options);
|
|
91
105
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/yada",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.1",
|
|
4
4
|
"description": "Validation library inspired by Joi.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jest",
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"lodash": "^4.17.21",
|
|
21
|
-
"validator": "^13.15.
|
|
21
|
+
"validator": "^13.15.26"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@babel/cli": "^7.
|
|
25
|
-
"@babel/core": "^7.
|
|
26
|
-
"@babel/preset-env": "^7.
|
|
24
|
+
"@babel/cli": "^7.28.3",
|
|
25
|
+
"@babel/core": "^7.28.5",
|
|
26
|
+
"@babel/preset-env": "^7.28.5",
|
|
27
27
|
"@bedrockio/eslint-plugin": "^1.2.2",
|
|
28
28
|
"@bedrockio/prettier-config": "^1.1.1",
|
|
29
29
|
"babel-plugin-add-module-exports": "^1.0.4",
|
package/src/Schema.js
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
ValidationError,
|
|
11
11
|
} from './errors';
|
|
12
12
|
|
|
13
|
-
import {
|
|
13
|
+
import { toOpenAi } from './openai';
|
|
14
14
|
import { canAllowEmptyString } from './utils';
|
|
15
15
|
|
|
16
16
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform', 'empty'];
|
|
@@ -222,18 +222,15 @@ export default class Schema {
|
|
|
222
222
|
* custom (code-based) assertions will not be output.
|
|
223
223
|
* @param {Object} [options]
|
|
224
224
|
* @param {Function} [options.tag] - Allows adding additional custom tags.
|
|
225
|
-
* @param {'openai'} [options.style] - Constrains schema output. Currently only
|
|
226
|
-
* supports OpenAI which will ensure the resulting schema works with the Structured
|
|
227
|
-
* Outputs API.
|
|
228
225
|
* @param {boolean} [options.stripExtensions] - Strips out JSON schema extensions.
|
|
229
226
|
*/
|
|
230
227
|
toJsonSchema(options) {
|
|
231
228
|
return {
|
|
232
229
|
...this.getType(),
|
|
230
|
+
...this.getFormat(),
|
|
233
231
|
...this.getDefault(),
|
|
234
232
|
...this.getEnum(options),
|
|
235
233
|
...this.getTags(options),
|
|
236
|
-
...this.getFormat(options),
|
|
237
234
|
};
|
|
238
235
|
}
|
|
239
236
|
|
|
@@ -245,6 +242,16 @@ export default class Schema {
|
|
|
245
242
|
return this.toJsonSchema(options);
|
|
246
243
|
}
|
|
247
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Converts the schema to OpenAI's [more strict flavor](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas)
|
|
247
|
+
* for structured output.
|
|
248
|
+
*/
|
|
249
|
+
toOpenAi() {
|
|
250
|
+
return this.transform((schema) => {
|
|
251
|
+
return toOpenAi(schema);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
248
255
|
/**
|
|
249
256
|
* Export JSON schema when invoked by JSON serializer.
|
|
250
257
|
*/
|
|
@@ -265,9 +272,9 @@ export default class Schema {
|
|
|
265
272
|
}
|
|
266
273
|
}
|
|
267
274
|
|
|
268
|
-
getFormat(
|
|
275
|
+
getFormat() {
|
|
269
276
|
const { format } = this.meta;
|
|
270
|
-
if (
|
|
277
|
+
if (format) {
|
|
271
278
|
return { format };
|
|
272
279
|
}
|
|
273
280
|
}
|
package/src/array.js
CHANGED
|
@@ -125,6 +125,27 @@ class ArraySchema extends TypeSchema {
|
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Sets a path on the schema using a transform function. Deep
|
|
130
|
+
* fields accept either a string using dot syntax or an array
|
|
131
|
+
* representing the path.
|
|
132
|
+
*
|
|
133
|
+
* @param {string|Array<string>} path The path to set.
|
|
134
|
+
* @param {Function} fn - Transform function that accepts an instance
|
|
135
|
+
* of the schema.
|
|
136
|
+
*/
|
|
137
|
+
set(path, fn) {
|
|
138
|
+
const { schema } = this.meta;
|
|
139
|
+
|
|
140
|
+
if (schema.meta.type !== 'object') {
|
|
141
|
+
throw new Error('Nested object schema required.');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return new ArraySchema({
|
|
145
|
+
schema: schema.set(path, fn),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
128
149
|
// Private
|
|
129
150
|
|
|
130
151
|
toString() {
|
package/src/object.js
CHANGED
|
@@ -168,6 +168,49 @@ class ObjectSchema extends TypeSchema {
|
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Sets a path on the schema using a transform function. Deep
|
|
173
|
+
* fields accept either a string using dot syntax or an array
|
|
174
|
+
* representing the path.
|
|
175
|
+
*
|
|
176
|
+
* @param {string|Array<string>} path The path to set.
|
|
177
|
+
* @param {Function} fn - Transform function that accepts an instance
|
|
178
|
+
* of the schema.
|
|
179
|
+
*/
|
|
180
|
+
set(path, fn) {
|
|
181
|
+
if (typeof path === 'string') {
|
|
182
|
+
path = path.split('.');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const [base, ...rest] = path;
|
|
186
|
+
|
|
187
|
+
const { fields: currentFields } = this.meta;
|
|
188
|
+
const keys = Object.keys(currentFields);
|
|
189
|
+
|
|
190
|
+
if (!keys.includes(base)) {
|
|
191
|
+
throw new Error(`Unknown field "${base}".`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const fields = {};
|
|
195
|
+
|
|
196
|
+
for (let key of keys) {
|
|
197
|
+
const schema = currentFields[key];
|
|
198
|
+
if (key === base) {
|
|
199
|
+
if (rest.length) {
|
|
200
|
+
fields[key] = schema.set(rest, fn);
|
|
201
|
+
} else {
|
|
202
|
+
fields[key] = fn(schema);
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
fields[key] = schema;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return new ObjectSchema({
|
|
210
|
+
fields,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
171
214
|
/**
|
|
172
215
|
* Returns the inner schema of an array field. This only makes
|
|
173
216
|
* sense if the array field holds a single schema, so all other
|
|
@@ -390,8 +433,6 @@ class ObjectSchema extends TypeSchema {
|
|
|
390
433
|
return super.options(options);
|
|
391
434
|
}
|
|
392
435
|
|
|
393
|
-
// Private
|
|
394
|
-
|
|
395
436
|
toJsonSchema(options) {
|
|
396
437
|
const { stripUnknown = false } = this.meta;
|
|
397
438
|
|
package/src/openai.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Confirmed as of: 2026-02-09
|
|
2
|
+
const OPENAI_ALLOWED_FORMATS = [
|
|
3
|
+
'date-time',
|
|
4
|
+
'duration',
|
|
5
|
+
'hostname',
|
|
6
|
+
'date',
|
|
7
|
+
'time',
|
|
8
|
+
'email',
|
|
9
|
+
'ipv4',
|
|
10
|
+
'ipv6',
|
|
11
|
+
'uuid',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
export function toOpenAi(schema) {
|
|
15
|
+
const { type, required, format, tags } = schema.meta;
|
|
16
|
+
|
|
17
|
+
if (type === 'object') {
|
|
18
|
+
return schema.required();
|
|
19
|
+
} else if (type === 'array') {
|
|
20
|
+
const { schemas } = schema.meta;
|
|
21
|
+
if (schemas) {
|
|
22
|
+
return schema.toArray().required();
|
|
23
|
+
} else {
|
|
24
|
+
return schema.required();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// All fields in OpenAI flavor JSON schema must be required,
|
|
29
|
+
// so make fields nullable to allow optional behvior.
|
|
30
|
+
if (!required) {
|
|
31
|
+
schema = schema.required().nullable();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (hasInvalidFormat(format)) {
|
|
35
|
+
schema = schema.clone({
|
|
36
|
+
format: null,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (tags) {
|
|
41
|
+
schema = schema.clone({
|
|
42
|
+
tags: null,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return schema;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function hasInvalidFormat(format) {
|
|
50
|
+
if (!format) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return !OPENAI_ALLOWED_FORMATS.includes(format);
|
|
54
|
+
}
|
package/src/tuple.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { uniqBy } from 'lodash';
|
|
2
|
+
|
|
1
3
|
import Schema from './Schema';
|
|
4
|
+
import array from './array';
|
|
2
5
|
import { ArrayError, ElementError, LocalizedError } from './errors';
|
|
3
6
|
|
|
4
7
|
class TupleSchema extends Schema {
|
|
@@ -65,6 +68,16 @@ class TupleSchema extends Schema {
|
|
|
65
68
|
return this.clone({ loose: true });
|
|
66
69
|
}
|
|
67
70
|
|
|
71
|
+
toArray() {
|
|
72
|
+
const { schemas } = this.meta;
|
|
73
|
+
const unique = uniqBy(schemas, (s) => s.meta.type);
|
|
74
|
+
if (unique.length > 1) {
|
|
75
|
+
return array(new Schema().allow(unique));
|
|
76
|
+
} else {
|
|
77
|
+
return array(unique[0]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
68
81
|
toString() {
|
|
69
82
|
return 'tuple';
|
|
70
83
|
}
|
|
@@ -73,6 +86,7 @@ class TupleSchema extends Schema {
|
|
|
73
86
|
const { schemas } = this.meta;
|
|
74
87
|
return {
|
|
75
88
|
...super.toJsonSchema(options),
|
|
89
|
+
items: false,
|
|
76
90
|
prefixItems: schemas.map((schema) => {
|
|
77
91
|
return schema.toJsonSchema(options);
|
|
78
92
|
}),
|
package/types/Schema.d.ts
CHANGED
|
@@ -79,14 +79,10 @@ export default class Schema {
|
|
|
79
79
|
* custom (code-based) assertions will not be output.
|
|
80
80
|
* @param {Object} [options]
|
|
81
81
|
* @param {Function} [options.tag] - Allows adding additional custom tags.
|
|
82
|
-
* @param {'openai'} [options.style] - Constrains schema output. Currently only
|
|
83
|
-
* supports OpenAI which will ensure the resulting schema works with the Structured
|
|
84
|
-
* Outputs API.
|
|
85
82
|
* @param {boolean} [options.stripExtensions] - Strips out JSON schema extensions.
|
|
86
83
|
*/
|
|
87
84
|
toJsonSchema(options?: {
|
|
88
85
|
tag?: Function;
|
|
89
|
-
style?: "openai";
|
|
90
86
|
stripExtensions?: boolean;
|
|
91
87
|
}): any;
|
|
92
88
|
/**
|
|
@@ -94,6 +90,11 @@ export default class Schema {
|
|
|
94
90
|
* @alias toJsonSchema.
|
|
95
91
|
*/
|
|
96
92
|
toOpenApi(options: any): any;
|
|
93
|
+
/**
|
|
94
|
+
* Converts the schema to OpenAI's [more strict flavor](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas)
|
|
95
|
+
* for structured output.
|
|
96
|
+
*/
|
|
97
|
+
toOpenAi(): this;
|
|
97
98
|
/**
|
|
98
99
|
* Export JSON schema when invoked by JSON serializer.
|
|
99
100
|
*/
|
|
@@ -101,7 +102,7 @@ export default class Schema {
|
|
|
101
102
|
getType(): {
|
|
102
103
|
type: any;
|
|
103
104
|
};
|
|
104
|
-
getFormat(
|
|
105
|
+
getFormat(): {
|
|
105
106
|
format: any;
|
|
106
107
|
};
|
|
107
108
|
getDefault(): {
|
package/types/Schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAkBA,kDAEC;AAED;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;;OAGG;IACH,iBAHW,OAAO,GACL,IAAI,CAUhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,uBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;;OAIG;IACH,iBAHW,OAAO,GACL,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDA0CC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,MAAM,CAMlB;IAED
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAkBA,kDAEC;AAED;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;;OAGG;IACH,iBAHW,OAAO,GACL,IAAI,CAUhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,uBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;;OAIG;IACH,iBAHW,OAAO,GACL,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDA0CC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,MAAM,CAMlB;IAED;;;;;;;OAOG;IACH,uBAHG;QAA2B,GAAG;QACJ,eAAe,GAAjC,OAAO;KACjB,OASA;IAED;;;OAGG;IACH,6BAEC;IAED;;;OAGG;IACH,iBAIC;IAED;;OAEG;IACH,cAEC;IAED;;MAWC;IAED;;MAKC;IAED;;;;MASC;IAED,2BAgDC;IAED,2BAmBC;IAED;;;;;OAKG;IACH,oCAFa,IAAI,CAgBhB;IAED,kBAEC;IAED,YAGC;IAID;;OAEG;IACH,kCAFa,IAAI,CAsDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED,gEAQC;IAED;;OAEG;IACH,yBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAYC;CACF"}
|
package/types/array.d.ts
CHANGED
|
@@ -19,6 +19,16 @@ declare class ArraySchema extends TypeSchema {
|
|
|
19
19
|
* @returns {this}
|
|
20
20
|
*/
|
|
21
21
|
transform(fn: Function, root?: boolean): this;
|
|
22
|
+
/**
|
|
23
|
+
* Sets a path on the schema using a transform function. Deep
|
|
24
|
+
* fields accept either a string using dot syntax or an array
|
|
25
|
+
* representing the path.
|
|
26
|
+
*
|
|
27
|
+
* @param {string|Array<string>} path The path to set.
|
|
28
|
+
* @param {Function} fn - Transform function that accepts an instance
|
|
29
|
+
* of the schema.
|
|
30
|
+
*/
|
|
31
|
+
set(path: string | Array<string>, fn: Function): ArraySchema;
|
|
22
32
|
toString(): string;
|
|
23
33
|
toJsonSchema(options: any): any;
|
|
24
34
|
}
|
package/types/array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"AAqKA;;;;;GAKG;AACH,0CAHW,GAAC,+BAYX;AA/KD;IACE,uBAGC;IAED,cAoCC;IAED,0BAUC;IAED,uBAUC;IAED,uBAaC;IAED,eAaC;IAED;;;;;OAKG;IACH,yCAFa,IAAI,CAkBhB;IAED;;;;;;;;OAQG;IACH,UAJW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,6BAc9B;IAID,mBAEC;IAED,gCAQC;CACF;uBAjKsB,cAAc"}
|
package/types/object.d.ts
CHANGED
|
@@ -24,6 +24,16 @@ declare class ObjectSchema extends TypeSchema {
|
|
|
24
24
|
* @param {string|Array<string>} [path] The path of the field.
|
|
25
25
|
*/
|
|
26
26
|
get(path?: string | Array<string>): any;
|
|
27
|
+
/**
|
|
28
|
+
* Sets a path on the schema using a transform function. Deep
|
|
29
|
+
* fields accept either a string using dot syntax or an array
|
|
30
|
+
* representing the path.
|
|
31
|
+
*
|
|
32
|
+
* @param {string|Array<string>} path The path to set.
|
|
33
|
+
* @param {Function} fn - Transform function that accepts an instance
|
|
34
|
+
* of the schema.
|
|
35
|
+
*/
|
|
36
|
+
set(path: string | Array<string>, fn: Function): ObjectSchema;
|
|
27
37
|
/**
|
|
28
38
|
* Returns the inner schema of an array field. This only makes
|
|
29
39
|
* sense if the array field holds a single schema, so all other
|
package/types/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AA+iBA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAQnB;wBA/iBY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IACE,uBAIC;IAED,sBAOC;IAED,cAiHC;IAED;;;;;;OAMG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAkB9B;IAED;;;;;;;;OAQG;IACH,UAJW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,8BAoC9B;IAED;;;;;;OAMG;IACH,cAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAc9B;IAED;;;;OAIG;IACH,eAFc,MAAM,EAAA,gBASnB;IAED;;;;OAIG;IACH,eAFc,MAAM,EAAA,gBASnB;IAED;;;;;;;OAOG;IACH,mBAHc,MAAM,EAAA,gBAuBnB;IAED;;OAEG;IACH,2BAQC;IAED;;;;;;OAMG;IACH,yCAFa,IAAI,CAiBhB;IAED;;;;;;OAMG;IACH,cAEC;IAED;;;;;;;;;OASG;IACH,YAFW,SAAS,GAAC,MAAM,gBA+B1B;IAID;;OAEG;IACH,mBAIC;IAED;;OAEG;IACH,qBAIC;IAED;;OAEG;IACH,sBAIC;IAED;;OAEG;IACH,uBAIC;IAED;;;;;;;;;;;OAWG;IACH,kBALG;QAA0B,UAAU,GAA5B,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,aAAa,GAA/B,OAAO;QACW,cAAc,GAAhC,OAAO;KAA0B,QAI3C;IAED,gCAmBC;CACF;mBArcgC,UAAU;uBACpB,cAAc"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../src/openai.js"],"names":[],"mappings":"AAaA,2CAiCC"}
|
package/types/tuple.d.ts
CHANGED
|
@@ -11,6 +11,60 @@ declare class TupleSchema extends Schema {
|
|
|
11
11
|
constructor(schemas: any);
|
|
12
12
|
setup(): void;
|
|
13
13
|
loose(): this;
|
|
14
|
+
toArray(): {
|
|
15
|
+
setup(): void;
|
|
16
|
+
length(length: any): /*elided*/ any;
|
|
17
|
+
min(length: any): /*elided*/ any;
|
|
18
|
+
max(length: any): /*elided*/ any;
|
|
19
|
+
latlng(): /*elided*/ any;
|
|
20
|
+
transform(fn: Function, root?: boolean): /*elided*/ any;
|
|
21
|
+
set(path: string | Array<string>, fn: Function): /*elided*/ any;
|
|
22
|
+
toString(): string;
|
|
23
|
+
toJsonSchema(options: any): any;
|
|
24
|
+
format(name: any, fn: any): /*elided*/ any;
|
|
25
|
+
assertions: any[];
|
|
26
|
+
meta: {};
|
|
27
|
+
required(allow?: boolean): /*elided*/ any;
|
|
28
|
+
default(arg: any): /*elided*/ any;
|
|
29
|
+
custom(fn: Function): /*elided*/ any;
|
|
30
|
+
missing(fn: Function): /*elided*/ any;
|
|
31
|
+
strip(strip: any): /*elided*/ any;
|
|
32
|
+
allow(...set: any[]): /*elided*/ any;
|
|
33
|
+
reject(...set: any[]): /*elided*/ any;
|
|
34
|
+
nullable(allow?: boolean): /*elided*/ any;
|
|
35
|
+
message(message: any): /*elided*/ any;
|
|
36
|
+
tag(tags: any): /*elided*/ any;
|
|
37
|
+
description(description: any): /*elided*/ any;
|
|
38
|
+
options(options: any): /*elided*/ any;
|
|
39
|
+
validate(value: any, options?: {}): Promise<any>;
|
|
40
|
+
clone(meta: any): /*elided*/ any;
|
|
41
|
+
append(schema: any): Schema;
|
|
42
|
+
toOpenApi(options: any): any;
|
|
43
|
+
toOpenAi(): /*elided*/ any;
|
|
44
|
+
toJSON(): any;
|
|
45
|
+
getType(): {
|
|
46
|
+
type: any;
|
|
47
|
+
};
|
|
48
|
+
getFormat(): {
|
|
49
|
+
format: any;
|
|
50
|
+
};
|
|
51
|
+
getDefault(): {
|
|
52
|
+
default?: undefined;
|
|
53
|
+
} | {
|
|
54
|
+
default: any;
|
|
55
|
+
};
|
|
56
|
+
getEnum(options: any): any;
|
|
57
|
+
getTags(options?: {}): any;
|
|
58
|
+
inspect(): string;
|
|
59
|
+
get(): void;
|
|
60
|
+
assertEnum(set: any, allow: any): /*elided*/ any;
|
|
61
|
+
assert(type: any, fn: any): /*elided*/ any;
|
|
62
|
+
pushAssertion(assertion: any): void;
|
|
63
|
+
canSkipAssertion(value: any, assertion: any, options: any): any;
|
|
64
|
+
transformValue(fn: any): /*elided*/ any;
|
|
65
|
+
getSortIndex(type: any): number;
|
|
66
|
+
runAssertion(value: any, assertion: any, options?: {}): Promise<any>;
|
|
67
|
+
};
|
|
14
68
|
toJsonSchema(options: any): any;
|
|
15
69
|
}
|
|
16
70
|
export {};
|
package/types/tuple.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"AAgGA;;;;;;GAMG;AACH,6CAJc,MAAM,EAAA,eASnB;mBA1GkB,UAAU;AAI7B;IACE,0BAGC;IAED,cAoDC;IAED,cAEC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAQC;IAMD,gCASC;CACF"}
|
package/dist/cjs/formats.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.isAllowedFormat = isAllowedFormat;
|
|
7
|
-
exports.isOpenAiAllowedFormat = isOpenAiAllowedFormat;
|
|
8
|
-
// Confirmed working as of: 2025-11-11
|
|
9
|
-
// Tested with gpt-5-nano-2025-08-07
|
|
10
|
-
const OPENAI_ALLOWED_FORMATS = ['date-time', 'date', 'time', 'email', 'hostname', 'ipv4', 'ipv6', 'uuid'];
|
|
11
|
-
function isAllowedFormat(format, options = {}) {
|
|
12
|
-
const {
|
|
13
|
-
style
|
|
14
|
-
} = options;
|
|
15
|
-
if (style === 'openai') {
|
|
16
|
-
// OpenAI schema inputs will fail on unknown formats.
|
|
17
|
-
// However these formats also offer useful guides for
|
|
18
|
-
// the LLM so we want to keep them as much as possible.
|
|
19
|
-
return isOpenAiAllowedFormat(format);
|
|
20
|
-
} else if (format) {
|
|
21
|
-
// JSON Schema has pre-defined types but also allows
|
|
22
|
-
// custom formats so by default allow any format.
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function isOpenAiAllowedFormat(format) {
|
|
27
|
-
return OPENAI_ALLOWED_FORMATS.includes(format);
|
|
28
|
-
}
|
package/src/formats.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
// Confirmed working as of: 2025-11-11
|
|
2
|
-
// Tested with gpt-5-nano-2025-08-07
|
|
3
|
-
const OPENAI_ALLOWED_FORMATS = [
|
|
4
|
-
'date-time',
|
|
5
|
-
'date',
|
|
6
|
-
'time',
|
|
7
|
-
'email',
|
|
8
|
-
'hostname',
|
|
9
|
-
'ipv4',
|
|
10
|
-
'ipv6',
|
|
11
|
-
'uuid',
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
export function isAllowedFormat(format, options = {}) {
|
|
15
|
-
const { style } = options;
|
|
16
|
-
if (style === 'openai') {
|
|
17
|
-
// OpenAI schema inputs will fail on unknown formats.
|
|
18
|
-
// However these formats also offer useful guides for
|
|
19
|
-
// the LLM so we want to keep them as much as possible.
|
|
20
|
-
return isOpenAiAllowedFormat(format);
|
|
21
|
-
} else if (format) {
|
|
22
|
-
// JSON Schema has pre-defined types but also allows
|
|
23
|
-
// custom formats so by default allow any format.
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function isOpenAiAllowedFormat(format) {
|
|
29
|
-
return OPENAI_ALLOWED_FORMATS.includes(format);
|
|
30
|
-
}
|