Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit ceb13a2

Browse files
Shaleen-25Shaleen Kachhara
and
Shaleen Kachhara
authored
fix(schema-generation): missing options for arrays (#19)
* fix(options): missing options when generating nested objects schema * fix(schema-generation): pass options to arrays as well Co-authored-by: Shaleen Kachhara <[email protected]>
1 parent 5aa8e97 commit ceb13a2

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/schema-builder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ function createSchemaFor(value: any, options?: SchemaGenOptions): Schema {
1616
return { type: ValueType.Null };
1717
}
1818
if (Array.isArray(value)) {
19-
return createSchemaForArray(value);
19+
return createSchemaForArray(value, options);
2020
}
2121
return createSchemaForObject(value, options);
2222
}
2323
}
2424

25-
function createSchemaForArray(arr: Array<any>): Schema {
25+
function createSchemaForArray(arr: Array<any>, options?: SchemaGenOptions): Schema {
2626
if (arr.length === 0) {
2727
return { type: ValueType.Array };
2828
}
29-
const elementSchemas = arr.map((value) => createSchemaFor(value));
29+
const elementSchemas = arr.map((value) => createSchemaFor(value, options));
3030
const items = combineSchemas(elementSchemas);
3131
return { type: ValueType.Array, items };
3232
}

tests/schema-builder.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ describe('SchemaBuilder', () => {
253253
},
254254
});
255255
});
256+
257+
it('should generate schema for array of objects w/o required', () => {
258+
const schema = createSchema(
259+
[
260+
{ one: 'a', two: 'b' },
261+
{ one: 'aa', two: 'bb' },
262+
],
263+
{ noRequired: true }
264+
);
265+
expect(schema).toEqual({
266+
type: 'array',
267+
items: {
268+
type: 'object',
269+
properties: {
270+
one: { type: 'string' },
271+
two: { type: 'string' },
272+
},
273+
},
274+
});
275+
});
256276
});
257277

258278
describe('prototype methods', () => {

0 commit comments

Comments
 (0)