Skip to content

Commit 2481d44

Browse files
committed
feat(ai): Add support for minItems and maxItems to Schema
1 parent 39505cc commit 2481d44

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
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
@@ -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;

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

Lines changed: 87 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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. */
@@ -77,7 +83,7 @@ export abstract class Schema implements SchemaInterface {
7783
* @internal
7884
*/
7985
toJSON(): SchemaRequest {
80-
const obj: { type: SchemaType; [key: string]: unknown } = {
86+
const obj: { type: SchemaType;[key: string]: unknown } = {
8187
type: this.type
8288
};
8389
for (const prop in this) {

packages/ai/src/types/schema.ts

Lines changed: 5 additions & 1 deletion
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;
@@ -69,7 +73,7 @@ export interface SchemaShared<T> {
6973
* {@link Schema} classes.
7074
* @public
7175
*/
72-
export interface SchemaParams extends SchemaShared<SchemaInterface> {}
76+
export interface SchemaParams extends SchemaShared<SchemaInterface> { }
7377

7478
/**
7579
* Final format for {@link Schema} params passed to backend requests.

0 commit comments

Comments
 (0)