Skip to content

Commit ffa595f

Browse files
authored
Merge c5089e2 into 39505cc
2 parents 39505cc + c5089e2 commit ffa595f

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

.changeset/angry-scissors-sit.md

+6
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

+5
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,9 @@ export abstract class Schema implements SchemaInterface {
790790
format?: string;
791791
// (undocumented)
792792
static integer(integerParams?: SchemaParams): IntegerSchema;
793+
items?: SchemaInterface;
794+
maxItems?: number;
795+
minItems?: number;
793796
nullable: boolean;
794797
// (undocumented)
795798
static number(numberParams?: SchemaParams): NumberSchema;
@@ -831,6 +834,8 @@ export interface SchemaShared<T> {
831834
example?: unknown;
832835
format?: string;
833836
items?: T;
837+
maxItems?: number;
838+
minItems?: number;
834839
nullable?: boolean;
835840
properties?: {
836841
[k: string]: T;

docs-devsite/ai.schema.md

+33
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

+22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface SchemaShared<T>
2727
| [example](./ai.schemashared.md#schemasharedexample) | unknown | Optional. The example of the property. |
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. |
30+
| [maxItems](./ai.schemashared.md#schemasharedmaxitems) | number | The maximum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->. |
31+
| [minItems](./ai.schemashared.md#schemasharedminitems) | number | The minimum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->. |
3032
| [nullable](./ai.schemashared.md#schemasharednullable) | boolean | Optional. Whether the property is nullable. |
3133
| [properties](./ai.schemashared.md#schemasharedproperties) | { \[k: string\]: T; } | Optional. Map of <code>Schema</code> objects. |
3234

@@ -80,6 +82,26 @@ Optional. The items of the property.
8082
items?: T;
8183
```
8284

85+
## SchemaShared.maxItems
86+
87+
The maximum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->.
88+
89+
<b>Signature:</b>
90+
91+
```typescript
92+
maxItems?: number;
93+
```
94+
95+
## SchemaShared.minItems
96+
97+
The minimum number of items (elements) in a schema of type [SchemaType.ARRAY](./ai.md#schematypearray_enummember)<!-- -->.
98+
99+
<b>Signature:</b>
100+
101+
```typescript
102+
minItems?: number;
103+
```
104+
83105
## SchemaShared.nullable
84106

85107
Optional. Whether the property is nullable.

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

+87
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,93 @@ describe('Schema builder', () => {
8181
nullable: false
8282
});
8383
});
84+
describe('Schema.array', () => {
85+
it('builds an array schema with basic items', () => {
86+
const schema = Schema.array({
87+
items: Schema.string()
88+
});
89+
expect(schema.toJSON()).to.eql({
90+
type: 'array',
91+
nullable: false,
92+
items: {
93+
type: 'string',
94+
nullable: false
95+
}
96+
});
97+
});
98+
99+
it('builds an array schema with items and minItems', () => {
100+
const schema = Schema.array({
101+
items: Schema.number(),
102+
minItems: 1
103+
});
104+
expect(schema.toJSON()).to.eql({
105+
type: 'array',
106+
nullable: false,
107+
items: {
108+
type: 'number',
109+
nullable: false
110+
},
111+
minItems: 1
112+
});
113+
});
114+
115+
it('builds an array schema with items and maxItems', () => {
116+
const schema = Schema.array({
117+
items: Schema.boolean(),
118+
maxItems: 10
119+
});
120+
expect(schema.toJSON()).to.eql({
121+
type: 'array',
122+
nullable: false,
123+
items: {
124+
type: 'boolean',
125+
nullable: false
126+
},
127+
maxItems: 10
128+
});
129+
});
130+
131+
it('builds an array schema with items, minItems, and maxItems', () => {
132+
const schema = Schema.array({
133+
items: Schema.integer(),
134+
minItems: 0,
135+
maxItems: 5
136+
});
137+
expect(schema.toJSON()).to.eql({
138+
type: 'array',
139+
nullable: false,
140+
items: {
141+
type: 'integer',
142+
nullable: false
143+
},
144+
minItems: 0,
145+
maxItems: 5
146+
});
147+
});
148+
149+
it('builds an array schema with items, minItems, maxItems, and other options', () => {
150+
const schema = Schema.array({
151+
items: Schema.string({ description: 'A list of names' }),
152+
minItems: 1,
153+
maxItems: 3,
154+
nullable: true,
155+
description: 'An array of strings'
156+
});
157+
expect(schema.toJSON()).to.eql({
158+
type: 'array',
159+
nullable: true,
160+
description: 'An array of strings',
161+
items: {
162+
type: 'string',
163+
description: 'A list of names',
164+
nullable: false
165+
},
166+
minItems: 1,
167+
maxItems: 3
168+
});
169+
});
170+
});
84171
it('builds an object schema', () => {
85172
const schema = Schema.object({
86173
properties: {

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

+6
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

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export interface SchemaShared<T> {
5151
description?: string;
5252
/** Optional. The items of the property. */
5353
items?: T;
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;
5458
/** Optional. Map of `Schema` objects. */
5559
properties?: {
5660
[k: string]: T;

0 commit comments

Comments
 (0)