Skip to content

fix: conversion for anyOf type: null to nullable: true #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ function convertDependencies(schema: SchemaType) {
function convertNullable(schema: SchemaType) {
for (const key of ['oneOf', 'anyOf'] as const) {
const schemas = schema[key] as JSONSchema4[];
if (!schemas) continue;

if (!Array.isArray(schemas)) {
return schema;
}
Expand Down
117 changes: 62 additions & 55 deletions test/nullable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ describe('nullable', () => {
});
});

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

const result = await convert(schema);
const result = await convert(schema);

expect(result).toEqual({
[key]: [{ type: 'string', nullable: true }],
});
}
);

expect(result).toEqual({
oneOf: [{ type: 'string', nullable: true }],
});
});
it('supports nullables inside definitions', async ({ expect }) => {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
Expand Down Expand Up @@ -149,59 +153,62 @@ describe('nullable', () => {
});
});

it('adds nullable for types with null', async ({ expect }) => {
const schema = {
$schema: 'http://json-schema.org/draft-04/schema#',
title: 'NullExample',
description: 'Null Example',
oneOf: [
{
type: 'object',
properties: {
foo: {
type: 'string',
it.each(['oneOf', 'anyOf'] as const)(
'adds nullable for types with null',
async (key) => {
const schema = {
$schema: 'http://json-schema.org/draft-04/schema#',
title: 'NullExample',
description: 'Null Example',
[key]: [
{
type: 'object',
properties: {
foo: {
type: 'string',
},
},
},
},
{
type: 'object',
properties: {
bar: {
type: 'number',
{
type: 'object',
properties: {
bar: {
type: 'number',
},
},
},
},
{
type: 'null',
},
],
} satisfies JSONSchema4;
{
type: 'null',
},
],
} satisfies JSONSchema4;

const result = await convert(schema);
const result = await convert(schema);

expect(result).toEqual({
title: 'NullExample',
description: 'Null Example',
oneOf: [
{
type: 'object',
properties: {
foo: {
type: 'string',
expect(result).toEqual({
title: 'NullExample',
description: 'Null Example',
[key]: [
{
type: 'object',
properties: {
foo: {
type: 'string',
},
},
nullable: true,
},
nullable: true,
},
{
type: 'object',
properties: {
bar: {
type: 'number',
{
type: 'object',
properties: {
bar: {
type: 'number',
},
},
nullable: true,
},
nullable: true,
},
],
});
});
],
});
}
);
});