Skip to content

Commit 1933324

Browse files
authored
feat(ai): Add support for minItems and maxItems to Schema (#9026)
1 parent 40be2db commit 1933324

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

.changeset/angry-scissors-sit.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': minor
3+
'@firebase/ai': minor
4+
---
5+
6+
Add support for `minItems` and `maxItems` to `Schema`.

common/api-review/ai.api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ export abstract class Schema implements SchemaInterface {
791791
format?: string;
792792
// (undocumented)
793793
static integer(integerParams?: SchemaParams): IntegerSchema;
794+
items?: SchemaInterface;
795+
maxItems?: number;
796+
minItems?: number;
794797
nullable: boolean;
795798
// (undocumented)
796799
static number(numberParams?: SchemaParams): NumberSchema;
@@ -833,7 +836,9 @@ export interface SchemaShared<T> {
833836
format?: string;
834837
items?: T;
835838
maximum?: number;
839+
maxItems?: number;
836840
minimum?: number;
841+
minItems?: number;
837842
nullable?: boolean;
838843
properties?: {
839844
[k: string]: T;

docs-devsite/ai.schema.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export declare abstract class Schema implements SchemaInterface
3232
| [description](./ai.schema.md#schemadescription) | | string | Optional. The description of the property. |
3333
| [example](./ai.schema.md#schemaexample) | | unknown | Optional. The example of the property. |
3434
| [format](./ai.schema.md#schemaformat) | | string | Optional. The format of the property. Supported formats:<br/> <ul> <li>for NUMBER type: "float", "double"</li> <li>for INTEGER type: "int32", "int64"</li> <li>for STRING type: "email", "byte", etc</li> </ul> |
35+
| [items](./ai.schema.md#schemaitems) | | [SchemaInterface](./ai.schemainterface.md#schemainterface_interface) | Optional. The items of the property. |
36+
| [maxItems](./ai.schema.md#schemamaxitems) | | number | The maximum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->. |
37+
| [minItems](./ai.schema.md#schemaminitems) | | number | The minimum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->. |
3538
| [nullable](./ai.schema.md#schemanullable) | | boolean | Optional. Whether the property is nullable. Defaults to false. |
3639
| [type](./ai.schema.md#schematype) | | [SchemaType](./ai.md#schematype) | Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
3740
@@ -93,6 +96,36 @@ Optional. The format of the property. Supported formats:<br/> <ul> <li>for NUMBE
9396
format?: string;
9497
```
9598
99+
## Schema.items
100+
101+
Optional. The items of the property.
102+
103+
<b>Signature:</b>
104+
105+
```typescript
106+
items?: SchemaInterface;
107+
```
108+
109+
## Schema.maxItems
110+
111+
The maximum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->.
112+
113+
<b>Signature:</b>
114+
115+
```typescript
116+
maxItems?: number;
117+
```
118+
119+
## Schema.minItems
120+
121+
The minimum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->.
122+
123+
<b>Signature:</b>
124+
125+
```typescript
126+
minItems?: number;
127+
```
128+
96129
## Schema.nullable
97130
98131
Optional. Whether the property is nullable. Defaults to false.

docs-devsite/ai.schemashared.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export interface SchemaShared<T>
2828
| [format](./ai.schemashared.md#schemasharedformat) | string | Optional. The format of the property. When using the Gemini Developer API ([GoogleAIBackend](./ai.googleaibackend.md#googleaibackend_class)<!-- -->), this must be either <code>'enum'</code> or <code>'date-time'</code>, otherwise requests will fail. |
2929
| [items](./ai.schemashared.md#schemashareditems) | T | Optional. The items of the property. |
3030
| [maximum](./ai.schemashared.md#schemasharedmaximum) | number | The maximum value of a numeric type. |
31+
| [maxItems](./ai.schemashared.md#schemasharedmaxitems) | number | The maximum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->. |
3132
| [minimum](./ai.schemashared.md#schemasharedminimum) | number | The minimum value of a numeric type. |
33+
| [minItems](./ai.schemashared.md#schemasharedminitems) | number | The minimum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->. |
3234
| [nullable](./ai.schemashared.md#schemasharednullable) | boolean | Optional. Whether the property is nullable. |
3335
| [properties](./ai.schemashared.md#schemasharedproperties) | { \[k: string\]: T; } | Optional. Map of <code>Schema</code> objects. |
3436
| [propertyOrdering](./ai.schemashared.md#schemasharedpropertyordering) | string\[\] | A hint suggesting the order in which the keys should appear in the generated JSON string. |
@@ -94,6 +96,16 @@ The maximum value of a numeric type.
9496
maximum?: number;
9597
```
9698

99+
## SchemaShared.maxItems
100+
101+
The maximum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->.
102+
103+
<b>Signature:</b>
104+
105+
```typescript
106+
maxItems?: number;
107+
```
108+
97109
## SchemaShared.minimum
98110

99111
The minimum value of a numeric type.
@@ -104,6 +116,16 @@ The minimum value of a numeric type.
104116
minimum?: number;
105117
```
106118

119+
## SchemaShared.minItems
120+
121+
The minimum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->.
122+
123+
<b>Signature:</b>
124+
125+
```typescript
126+
minItems?: number;
127+
```
128+
107129
## SchemaShared.nullable
108130

109131
Optional. Whether the property is nullable.

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,93 @@ describe('Schema builder', () => {
103103
title: 'Direction'
104104
});
105105
});
106+
describe('Schema.array', () => {
107+
it('builds an array schema with basic items', () => {
108+
const schema = Schema.array({
109+
items: Schema.string()
110+
});
111+
expect(schema.toJSON()).to.eql({
112+
type: 'array',
113+
nullable: false,
114+
items: {
115+
type: 'string',
116+
nullable: false
117+
}
118+
});
119+
});
120+
121+
it('builds an array schema with items and minItems', () => {
122+
const schema = Schema.array({
123+
items: Schema.number(),
124+
minItems: 1
125+
});
126+
expect(schema.toJSON()).to.eql({
127+
type: 'array',
128+
nullable: false,
129+
items: {
130+
type: 'number',
131+
nullable: false
132+
},
133+
minItems: 1
134+
});
135+
});
136+
137+
it('builds an array schema with items and maxItems', () => {
138+
const schema = Schema.array({
139+
items: Schema.boolean(),
140+
maxItems: 10
141+
});
142+
expect(schema.toJSON()).to.eql({
143+
type: 'array',
144+
nullable: false,
145+
items: {
146+
type: 'boolean',
147+
nullable: false
148+
},
149+
maxItems: 10
150+
});
151+
});
152+
153+
it('builds an array schema with items, minItems, and maxItems', () => {
154+
const schema = Schema.array({
155+
items: Schema.integer(),
156+
minItems: 0,
157+
maxItems: 5
158+
});
159+
expect(schema.toJSON()).to.eql({
160+
type: 'array',
161+
nullable: false,
162+
items: {
163+
type: 'integer',
164+
nullable: false
165+
},
166+
minItems: 0,
167+
maxItems: 5
168+
});
169+
});
170+
171+
it('builds an array schema with items, minItems, maxItems, and other options', () => {
172+
const schema = Schema.array({
173+
items: Schema.string({ description: 'A list of names' }),
174+
minItems: 1,
175+
maxItems: 3,
176+
nullable: true,
177+
description: 'An array of strings'
178+
});
179+
expect(schema.toJSON()).to.eql({
180+
type: 'array',
181+
nullable: true,
182+
description: 'An array of strings',
183+
items: {
184+
type: 'string',
185+
description: 'A list of names',
186+
nullable: false
187+
},
188+
minItems: 1,
189+
maxItems: 3
190+
});
191+
});
192+
});
106193
it('builds an object schema', () => {
107194
const schema = Schema.object({
108195
properties: {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export abstract class Schema implements SchemaInterface {
4949
format?: string;
5050
/** Optional. The description of the property. */
5151
description?: string;
52+
/** Optional. The items of the property. */
53+
items?: SchemaInterface;
54+
/** The minimum number of items (elements) in a schema of type {@link SchemaType.ARRAY}. */
55+
minItems?: number;
56+
/** The maximum number of items (elements) in a schema of type {@link SchemaType.ARRAY}. */
57+
maxItems?: number;
5258
/** Optional. Whether the property is nullable. Defaults to false. */
5359
nullable: boolean;
5460
/** Optional. The example of the property. */

packages/ai/src/types/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export interface SchemaShared<T> {
5757
title?: string;
5858
/** Optional. The items of the property. */
5959
items?: T;
60+
/** The minimum number of items (elements) in a schema of type {@link SchemaType.ARRAY}. */
61+
minItems?: number;
62+
/** The maximum number of items (elements) in a schema of type {@link SchemaType.ARRAY}. */
63+
maxItems?: number;
6064
/** Optional. Map of `Schema` objects. */
6165
properties?: {
6266
[k: string]: T;

0 commit comments

Comments
 (0)