Skip to content

Commit

Permalink
fix: conversion for anyOf type: null to nullable: true (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Eva Arnika Varga <[email protected]>
  • Loading branch information
jonluca and arnikaeva authored Nov 8, 2023
1 parent ade6d2f commit 9318da1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
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,
},
],
});
});
],
});
}
);
});

0 comments on commit 9318da1

Please sign in to comment.