Skip to content

Commit 9318da1

Browse files
jonlucaarnikaeva
andauthored
fix: conversion for anyOf type: null to nullable: true (#44)
Co-authored-by: Eva Arnika Varga <[email protected]>
1 parent ade6d2f commit 9318da1

File tree

2 files changed

+64
-55
lines changed

2 files changed

+64
-55
lines changed

Diff for: src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ function convertDependencies(schema: SchemaType) {
201201
function convertNullable(schema: SchemaType) {
202202
for (const key of ['oneOf', 'anyOf'] as const) {
203203
const schemas = schema[key] as JSONSchema4[];
204+
if (!schemas) continue;
205+
204206
if (!Array.isArray(schemas)) {
205207
return schema;
206208
}

Diff for: test/nullable.test.ts

+62-55
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ describe('nullable', () => {
1717
});
1818
});
1919

20-
it('supports nullables inside sub-schemas', async ({ expect }) => {
21-
const schema = {
22-
$schema: 'http://json-schema.org/draft-04/schema#',
23-
oneOf: [{ type: 'string' }, { type: 'null' }],
24-
} satisfies JSONSchema4;
20+
it.each(['oneOf', 'anyOf'] as const)(
21+
'supports nullables inside sub-schemas %s',
22+
async (key) => {
23+
const schema = {
24+
$schema: 'http://json-schema.org/draft-04/schema#',
25+
[key]: [{ type: 'string' }, { type: 'null' }],
26+
} satisfies JSONSchema4;
2527

26-
const result = await convert(schema);
28+
const result = await convert(schema);
29+
30+
expect(result).toEqual({
31+
[key]: [{ type: 'string', nullable: true }],
32+
});
33+
}
34+
);
2735

28-
expect(result).toEqual({
29-
oneOf: [{ type: 'string', nullable: true }],
30-
});
31-
});
3236
it('supports nullables inside definitions', async ({ expect }) => {
3337
const schema = {
3438
$schema: 'http://json-schema.org/draft-07/schema#',
@@ -149,59 +153,62 @@ describe('nullable', () => {
149153
});
150154
});
151155

152-
it('adds nullable for types with null', async ({ expect }) => {
153-
const schema = {
154-
$schema: 'http://json-schema.org/draft-04/schema#',
155-
title: 'NullExample',
156-
description: 'Null Example',
157-
oneOf: [
158-
{
159-
type: 'object',
160-
properties: {
161-
foo: {
162-
type: 'string',
156+
it.each(['oneOf', 'anyOf'] as const)(
157+
'adds nullable for types with null',
158+
async (key) => {
159+
const schema = {
160+
$schema: 'http://json-schema.org/draft-04/schema#',
161+
title: 'NullExample',
162+
description: 'Null Example',
163+
[key]: [
164+
{
165+
type: 'object',
166+
properties: {
167+
foo: {
168+
type: 'string',
169+
},
163170
},
164171
},
165-
},
166-
{
167-
type: 'object',
168-
properties: {
169-
bar: {
170-
type: 'number',
172+
{
173+
type: 'object',
174+
properties: {
175+
bar: {
176+
type: 'number',
177+
},
171178
},
172179
},
173-
},
174-
{
175-
type: 'null',
176-
},
177-
],
178-
} satisfies JSONSchema4;
180+
{
181+
type: 'null',
182+
},
183+
],
184+
} satisfies JSONSchema4;
179185

180-
const result = await convert(schema);
186+
const result = await convert(schema);
181187

182-
expect(result).toEqual({
183-
title: 'NullExample',
184-
description: 'Null Example',
185-
oneOf: [
186-
{
187-
type: 'object',
188-
properties: {
189-
foo: {
190-
type: 'string',
188+
expect(result).toEqual({
189+
title: 'NullExample',
190+
description: 'Null Example',
191+
[key]: [
192+
{
193+
type: 'object',
194+
properties: {
195+
foo: {
196+
type: 'string',
197+
},
191198
},
199+
nullable: true,
192200
},
193-
nullable: true,
194-
},
195-
{
196-
type: 'object',
197-
properties: {
198-
bar: {
199-
type: 'number',
201+
{
202+
type: 'object',
203+
properties: {
204+
bar: {
205+
type: 'number',
206+
},
200207
},
208+
nullable: true,
201209
},
202-
nullable: true,
203-
},
204-
],
205-
});
206-
});
210+
],
211+
});
212+
}
213+
);
207214
});

0 commit comments

Comments
 (0)