Skip to content

Commit 2a16ff5

Browse files
committed
start work on Schema rewrite
1 parent 5594ebc commit 2a16ff5

File tree

4 files changed

+278
-23
lines changed

4 files changed

+278
-23
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect, use } from 'chai';
19+
import sinonChai from 'sinon-chai';
20+
import { ObjectSchema, Schema } from './schema-builder';
21+
22+
use(sinonChai);
23+
24+
describe.only('request formatting methods', () => {
25+
it('builds string schema', () => {
26+
const schema = Schema.createString({ description: 'hey' });
27+
expect(schema.toJSON().type).to.equal('string');
28+
expect(schema.toJSON().description).to.equal('hey');
29+
});
30+
it('builds layered schema', () => {
31+
const schema = Schema.createArray({
32+
items: Schema.createObject({
33+
properties: {
34+
name: Schema.createString({})
35+
}
36+
})
37+
});
38+
expect(schema.toJSON().type).to.equal('array');
39+
expect((schema.toJSON().items as Schema).type).to.equal('object');
40+
expect(
41+
((schema.toJSON().items as ObjectSchema).properties.name as Schema).type
42+
).to.equal('string');
43+
});
44+
});
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { SchemaInterface, SchemaType, SchemaParams } from '../types/schema';
2+
3+
export class Schema implements SchemaInterface {
4+
/**
5+
* Optional. The type of the property. {@link
6+
* SchemaType}.
7+
*/
8+
type: SchemaType;
9+
/** Optional. The format of the property.
10+
* Supported formats:
11+
* for NUMBER type: "float", "double"
12+
* for INTEGER type: "int32", "int64"
13+
* for STRING type: "email", "byte", etc
14+
*/
15+
format?: string;
16+
/** Optional. The description of the property. */
17+
description?: string;
18+
/** Optional. Whether the property is nullable. */
19+
nullable: boolean;
20+
/** Optional. Array of required property. */
21+
required: boolean;
22+
/** Optional. The example of the property. */
23+
example?: unknown;
24+
25+
constructor(schemaParams: SchemaInterface) {
26+
this.type = schemaParams.type;
27+
this.format = schemaParams?.format;
28+
this.description = schemaParams?.description;
29+
this.example = schemaParams?.example;
30+
this.nullable = schemaParams?.nullable || false;
31+
this.required = schemaParams?.required || true;
32+
}
33+
34+
toJSON(): Record<string, unknown> {
35+
const obj: Record<string, unknown> = {};
36+
for (const prop in this) {
37+
if (this.hasOwnProperty(prop) && this[prop] !== undefined) {
38+
obj[prop] = this[prop];
39+
}
40+
}
41+
return obj;
42+
}
43+
44+
static createArray(
45+
arrayParams: SchemaParams & { items: Schema }
46+
): ArraySchema {
47+
return new ArraySchema(arrayParams, arrayParams.items);
48+
}
49+
50+
static createObject(
51+
objectParams: SchemaParams & {
52+
properties: {
53+
[k: string]: Schema;
54+
};
55+
}
56+
): ObjectSchema {
57+
return new ObjectSchema(objectParams, objectParams.properties);
58+
}
59+
60+
static createString(stringParams: SchemaParams): StringSchema {
61+
return new StringSchema(stringParams);
62+
}
63+
64+
static createEnumString(
65+
stringParams: SchemaParams & { enumValues: string[] }
66+
): StringSchema {
67+
return new StringSchema(stringParams, stringParams.enumValues);
68+
}
69+
70+
static createInteger(integerParams?: SchemaParams): IntegerSchema {
71+
return new IntegerSchema(integerParams);
72+
}
73+
74+
static createNumber(numberParams?: SchemaParams): NumberSchema {
75+
return new NumberSchema(numberParams);
76+
}
77+
78+
static createBoolean(booleanParams?: SchemaParams): BooleanSchema {
79+
return new BooleanSchema(booleanParams);
80+
}
81+
}
82+
83+
export class IntegerSchema extends Schema {
84+
constructor(schemaParams?: SchemaParams) {
85+
super({
86+
type: SchemaType.INTEGER,
87+
...schemaParams
88+
});
89+
}
90+
}
91+
export class NumberSchema extends Schema {
92+
constructor(schemaParams?: SchemaParams) {
93+
super({
94+
type: SchemaType.NUMBER,
95+
...schemaParams
96+
});
97+
}
98+
}
99+
export class BooleanSchema extends Schema {
100+
constructor(schemaParams?: SchemaParams) {
101+
super({
102+
type: SchemaType.BOOLEAN,
103+
...schemaParams
104+
});
105+
}
106+
}
107+
108+
export class StringSchema extends Schema {
109+
constructor(schemaParams?: SchemaParams, public enumValues?: string[]) {
110+
super({
111+
type: SchemaType.STRING,
112+
...schemaParams
113+
});
114+
}
115+
}
116+
117+
export class ArraySchema extends Schema {
118+
constructor(schemaParams: SchemaParams, public items: Schema) {
119+
super({
120+
type: SchemaType.ARRAY,
121+
...schemaParams
122+
});
123+
}
124+
125+
toJSON(): Record<string, unknown> {
126+
const obj = super.toJSON();
127+
obj.items = this.items.toJSON();
128+
return obj;
129+
}
130+
}
131+
132+
export class ObjectSchema extends Schema {
133+
constructor(
134+
schemaParams: SchemaParams,
135+
public properties: {
136+
[k: string]: Schema;
137+
}
138+
) {
139+
super({
140+
type: SchemaType.OBJECT,
141+
...schemaParams
142+
});
143+
}
144+
145+
toJSON(): Record<string, unknown> {
146+
const obj = super.toJSON();
147+
const properties: Record<string, unknown> = {};
148+
for (const property in this.properties) {
149+
if (this.properties.hasOwnProperty(property)) {
150+
properties[property] = this.properties[property].toJSON();
151+
}
152+
}
153+
obj.properties = properties;
154+
return obj;
155+
}
156+
}

packages/vertexai/src/types/requests.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
HarmBlockThreshold,
2323
HarmCategory
2424
} from './enums';
25+
import { SchemaType } from './schema';
2526

2627
/**
2728
* Base parameters for a number of methods.
@@ -177,33 +178,15 @@ export declare interface FunctionDeclarationsTool {
177178
functionDeclarations?: FunctionDeclaration[];
178179
}
179180

180-
/**
181-
* Contains the list of OpenAPI data types
182-
* as defined by https://swagger.io/docs/specification/data-models/data-types/
183-
* @public
184-
*/
185-
export enum FunctionDeclarationSchemaType {
186-
/** String type. */
187-
STRING = 'STRING',
188-
/** Number type. */
189-
NUMBER = 'NUMBER',
190-
/** Integer type. */
191-
INTEGER = 'INTEGER',
192-
/** Boolean type. */
193-
BOOLEAN = 'BOOLEAN',
194-
/** Array type. */
195-
ARRAY = 'ARRAY',
196-
/** Object type. */
197-
OBJECT = 'OBJECT'
198-
}
199-
200181
/**
201182
* Schema for parameters passed to {@link FunctionDeclaration.parameters}.
183+
* For function declarations, the top level {@link Schema} type must be
184+
* type `object`.
202185
* @public
203186
*/
204187
export interface FunctionDeclarationSchema {
205188
/** The type of the parameter. */
206-
type: FunctionDeclarationSchemaType;
189+
type: SchemaType.OBJECT;
207190
/** The format of the parameter. */
208191
properties: { [k: string]: FunctionDeclarationSchemaProperty };
209192
/** Optional. Description of the parameter. */
@@ -221,9 +204,9 @@ export interface FunctionDeclarationSchema {
221204
export interface FunctionDeclarationSchemaProperty {
222205
/**
223206
* Optional. The type of the property. {@link
224-
* FunctionDeclarationSchemaType}.
207+
* SchemaType}.
225208
*/
226-
type?: FunctionDeclarationSchemaType;
209+
type?: SchemaType;
227210
/** Optional. The format of the property. */
228211
format?: string;
229212
/** Optional. The description of the property. */

packages/vertexai/src/types/schema.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/**
3+
* Contains the list of OpenAPI data types
4+
* as defined by https://swagger.io/docs/specification/data-models/data-types/
5+
* @public
6+
*/
7+
export enum SchemaType {
8+
/** String type. */
9+
STRING = "string",
10+
/** Number type. */
11+
NUMBER = "number",
12+
/** Integer type. */
13+
INTEGER = "integer",
14+
/** Boolean type. */
15+
BOOLEAN = "boolean",
16+
/** Array type. */
17+
ARRAY = "array",
18+
/** Object type. */
19+
OBJECT = "object",
20+
}
21+
22+
interface SchemaShared {
23+
/** Optional. The format of the property. */
24+
format?: string;
25+
/** Optional. The description of the property. */
26+
description?: string;
27+
/** Optional. The items of the property. */
28+
items?: SchemaInterface;
29+
/** Optional. The enum of the property. */
30+
enum?: string[];
31+
/** Optional. Map of {@link Schema}. */
32+
properties?: {
33+
[k: string]: SchemaInterface;
34+
};
35+
/** Optional. The example of the property. */
36+
example?: unknown;
37+
/** Optional. Whether the property is nullable. */
38+
nullable?: boolean;
39+
}
40+
41+
/**
42+
* User-facing params passed to specific Schema static methods.
43+
*/
44+
export interface SchemaParams extends SchemaShared {
45+
/** Optional. Array of required property. */
46+
required?: boolean;
47+
48+
}
49+
50+
/**
51+
* Final format for Schema params passed to backend requests.
52+
*/
53+
export interface SchemaRequest extends SchemaShared {
54+
/**
55+
* The type of the property. {@link
56+
* SchemaType}.
57+
*/
58+
type?: SchemaType;
59+
/** Optional. Array of required property. */
60+
required?: string[];
61+
}
62+
63+
/**
64+
* Interface for Schema class.
65+
*/
66+
export interface SchemaInterface extends SchemaParams {
67+
/**
68+
* The type of the property. {@link
69+
* SchemaType}.
70+
*/
71+
type: SchemaType;
72+
}

0 commit comments

Comments
 (0)