Skip to content

Commit db629b1

Browse files
committed
Fixes based on testing
1 parent 2f17c17 commit db629b1

File tree

3 files changed

+45
-74
lines changed

3 files changed

+45
-74
lines changed

common/api-review/vertexai-preview.api.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class ArraySchema extends Schema {
1515
// (undocumented)
1616
items: TypedSchema;
1717
// @internal (undocumented)
18-
_toRequest(): _SchemaRequest;
18+
toJSON(): _SchemaRequest;
1919
}
2020

2121
// @public
@@ -492,7 +492,7 @@ export class ObjectSchema extends Schema {
492492
[k: string]: TypedSchema;
493493
};
494494
// @internal (undocumented)
495-
_toRequest(): _SchemaRequest;
495+
toJSON(): _SchemaRequest;
496496
}
497497

498498
// @public
@@ -580,13 +580,6 @@ export abstract class Schema implements SchemaInterface {
580580
example?: unknown;
581581
format?: string;
582582
// (undocumented)
583-
static functionDeclaration(objectParams: SchemaParams & {
584-
properties: {
585-
[k: string]: Schema;
586-
};
587-
optionalProperties?: string[];
588-
}): ObjectSchema;
589-
// (undocumented)
590583
static integer(integerParams?: SchemaParams): IntegerSchema;
591584
nullable: boolean;
592585
// (undocumented)
@@ -600,15 +593,11 @@ export abstract class Schema implements SchemaInterface {
600593
}): ObjectSchema;
601594
// (undocumented)
602595
static string(stringParams?: SchemaParams): StringSchema;
603-
// (undocumented)
604-
toJSON(): string;
605-
// Warning: (ae-incompatible-release-tags) The symbol "_toRequest" is marked as @public, but its signature references "_SchemaRequest" which is marked as @internal
606-
_toRequest(): _SchemaRequest;
596+
// Warning: (ae-incompatible-release-tags) The symbol "toJSON" is marked as @public, but its signature references "_SchemaRequest" which is marked as @internal
597+
toJSON(): _SchemaRequest;
607598
type: SchemaType;
608599
}
609600

610-
// Warning: (ae-forgotten-export) The symbol "SchemaShared" needs to be exported by the entry point index.d.ts
611-
//
612601
// @public
613602
export interface SchemaInterface extends SchemaShared<SchemaInterface> {
614603
type: SchemaType;
@@ -624,6 +613,21 @@ export interface _SchemaRequest extends SchemaShared<_SchemaRequest> {
624613
type: SchemaType;
625614
}
626615

616+
// @public (undocumented)
617+
export interface SchemaShared<T> {
618+
// (undocumented)
619+
[key: string]: unknown;
620+
description?: string;
621+
enum?: string[];
622+
example?: unknown;
623+
format?: string;
624+
items?: T;
625+
nullable?: boolean;
626+
properties?: {
627+
[k: string]: T;
628+
};
629+
}
630+
627631
// @public
628632
export enum SchemaType {
629633
ARRAY = "array",
@@ -662,7 +666,7 @@ export class StringSchema extends Schema {
662666
// (undocumented)
663667
enum?: string[];
664668
// @internal (undocumented)
665-
_toRequest(): _SchemaRequest;
669+
toJSON(): _SchemaRequest;
666670
}
667671

668672
// @public

packages/vertexai/src/requests/schema-builder.test.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@ use(sinonChai);
2525
describe.only('Schema builder', () => {
2626
it('builds integer schema', () => {
2727
const schema = Schema.integer();
28-
expect(schema.toRequest()).to.eql({
28+
expect(schema.toJSON()).to.eql({
2929
type: 'integer',
3030
nullable: false
3131
});
3232
});
3333
it('builds integer schema with options and overrides', () => {
3434
const schema = Schema.integer({ nullable: true, format: 'int32' });
35-
expect(schema.toRequest()).to.eql({
35+
expect(schema.toJSON()).to.eql({
3636
type: 'integer',
3737
format: 'int32',
3838
nullable: true
3939
});
4040
});
4141
it('builds number schema', () => {
4242
const schema = Schema.number();
43-
expect(schema.toRequest()).to.eql({
43+
expect(schema.toJSON()).to.eql({
4444
type: 'number',
4545
nullable: false
4646
});
4747
});
4848
it('builds number schema with options and unknown options', () => {
4949
const schema = Schema.number({ format: 'float', futureOption: 'test' });
50-
expect(schema.toRequest()).to.eql({
50+
expect(schema.toJSON()).to.eql({
5151
type: 'number',
5252
format: 'float',
5353
futureOption: 'test',
@@ -56,14 +56,14 @@ describe.only('Schema builder', () => {
5656
});
5757
it('builds boolean schema', () => {
5858
const schema = Schema.boolean();
59-
expect(schema.toRequest()).to.eql({
59+
expect(schema.toJSON()).to.eql({
6060
type: 'boolean',
6161
nullable: false
6262
});
6363
});
6464
it('builds string schema', () => {
6565
const schema = Schema.string({ description: 'hey' });
66-
expect(schema.toRequest()).to.eql({
66+
expect(schema.toJSON()).to.eql({
6767
type: 'string',
6868
description: 'hey',
6969
nullable: false
@@ -74,38 +74,20 @@ describe.only('Schema builder', () => {
7474
example: 'east',
7575
enum: ['east', 'west']
7676
});
77-
expect(schema.toRequest()).to.eql({
77+
expect(schema.toJSON()).to.eql({
7878
type: 'string',
7979
example: 'east',
8080
enum: ['east', 'west'],
8181
nullable: false
8282
});
8383
});
84-
it('builds functionDeclaration schema', () => {
85-
const schema = Schema.functionDeclaration({
86-
properties: {
87-
'someInput': Schema.string()
88-
}
89-
});
90-
expect(schema.toRequest()).to.eql({
91-
type: 'object',
92-
nullable: false,
93-
properties: {
94-
'someInput': {
95-
type: 'string',
96-
nullable: false
97-
}
98-
},
99-
required: ['someInput']
100-
});
101-
});
10284
it('builds an object schema', () => {
10385
const schema = Schema.object({
10486
properties: {
10587
'someInput': Schema.string()
10688
}
10789
});
108-
expect(schema.toRequest()).to.eql({
90+
expect(schema.toJSON()).to.eql({
10991
type: 'object',
11092
nullable: false,
11193
properties: {
@@ -125,7 +107,7 @@ describe.only('Schema builder', () => {
125107
},
126108
optionalProperties: ['someBool']
127109
});
128-
expect(schema.toRequest()).to.eql({
110+
expect(schema.toJSON()).to.eql({
129111
type: 'object',
130112
nullable: false,
131113
properties: {
@@ -165,7 +147,7 @@ describe.only('Schema builder', () => {
165147
}
166148
})
167149
});
168-
expect(schema.toRequest()).to.eql(layeredSchemaOutputPartial);
150+
expect(schema.toJSON()).to.eql(layeredSchemaOutputPartial);
169151
});
170152
it('builds layered schema - fully filled out', () => {
171153
const schema = Schema.array({
@@ -221,7 +203,7 @@ describe.only('Schema builder', () => {
221203
})
222204
});
223205

224-
expect(schema.toRequest()).to.eql(layeredSchemaOutput);
206+
expect(schema.toJSON()).to.eql(layeredSchemaOutput);
225207
});
226208
it('can override "nullable" and set optional properties', () => {
227209
const schema = Schema.object({
@@ -232,7 +214,7 @@ describe.only('Schema builder', () => {
232214
},
233215
optionalProperties: ['elevation']
234216
});
235-
expect(schema.toRequest()).to.eql({
217+
expect(schema.toJSON()).to.eql({
236218
'type': 'object',
237219
'nullable': false,
238220
'properties': {
@@ -261,7 +243,7 @@ describe.only('Schema builder', () => {
261243
},
262244
optionalProperties: ['cat']
263245
});
264-
expect(() => schema.toRequest()).to.throw(VertexAIErrorCode.INVALID_SCHEMA);
246+
expect(() => schema.toJSON()).to.throw(VertexAIErrorCode.INVALID_SCHEMA);
265247
});
266248
});
267249

packages/vertexai/src/requests/schema-builder.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export abstract class Schema implements SchemaInterface {
5353
}
5454

5555
/**
56-
* Converts class to a plain JSON object (not a string).
57-
* @internal
56+
* Defines how this Schema should be serialized as JSON.
57+
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior
5858
*/
59-
_toRequest(): _SchemaRequest {
59+
toJSON(): _SchemaRequest {
6060
const obj: { type: SchemaType; [key: string]: unknown } = {
6161
type: this.type
6262
};
@@ -70,10 +70,6 @@ export abstract class Schema implements SchemaInterface {
7070
return obj as _SchemaRequest;
7171
}
7272

73-
toJSON(): string {
74-
return JSON.stringify(this._toRequest());
75-
}
76-
7773
static array(arrayParams: SchemaParams & { items: Schema }): ArraySchema {
7874
return new ArraySchema(arrayParams, arrayParams.items);
7975
}
@@ -93,17 +89,6 @@ export abstract class Schema implements SchemaInterface {
9389
);
9490
}
9591

96-
static functionDeclaration(
97-
objectParams: SchemaParams & {
98-
properties: {
99-
[k: string]: Schema;
100-
};
101-
optionalProperties?: string[];
102-
}
103-
): ObjectSchema {
104-
return this.object(objectParams);
105-
}
106-
10792
// eslint-disable-next-line id-blacklist
10893
static string(stringParams?: SchemaParams): StringSchema {
10994
return new StringSchema(stringParams);
@@ -199,8 +184,8 @@ export class StringSchema extends Schema {
199184
/**
200185
* @internal
201186
*/
202-
_toRequest(): _SchemaRequest {
203-
const obj = super._toRequest();
187+
toJSON(): _SchemaRequest {
188+
const obj = super.toJSON();
204189
if (this.enum) {
205190
obj['enum'] = this.enum;
206191
}
@@ -225,9 +210,9 @@ export class ArraySchema extends Schema {
225210
/**
226211
* @internal
227212
*/
228-
_toRequest(): _SchemaRequest {
229-
const obj = super._toRequest();
230-
obj.items = this.items._toRequest();
213+
toJSON(): _SchemaRequest {
214+
const obj = super.toJSON();
215+
obj.items = this.items.toJSON();
231216
return obj;
232217
}
233218
}
@@ -254,9 +239,9 @@ export class ObjectSchema extends Schema {
254239
/**
255240
* @internal
256241
*/
257-
_toRequest(): _SchemaRequest {
258-
const obj = super._toRequest();
259-
obj.properties = this.properties;
242+
toJSON(): _SchemaRequest {
243+
const obj = super.toJSON();
244+
obj.properties = { ...this.properties };
260245
const required = [];
261246
if (this.optionalProperties) {
262247
for (const propertyKey of this.optionalProperties) {
@@ -272,7 +257,7 @@ export class ObjectSchema extends Schema {
272257
if (this.properties.hasOwnProperty(propertyKey)) {
273258
obj.properties[propertyKey] = this.properties[
274259
propertyKey
275-
]._toRequest() as _SchemaRequest;
260+
].toJSON() as _SchemaRequest;
276261
if (!this.optionalProperties.includes(propertyKey)) {
277262
required.push(propertyKey);
278263
}

0 commit comments

Comments
 (0)