Skip to content
Closed
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
13 changes: 8 additions & 5 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,23 @@ function generateRawType(ast: AST, options: Options): string {
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]'
})()
case 'BOOLEAN':
return 'boolean'
return 'boolean' + (ast.isNullable ? ' | null' : '')
case 'INTERFACE':
return generateInterface(ast, options)
case 'INTERSECTION':
return generateSetOperation(ast, options)
case 'LITERAL':
return JSON.stringify(ast.params)
case 'NUMBER':
return 'number'
return 'number' + (ast.isNullable ? ' | null' : '')
case 'NULL':
return 'null'
case 'OBJECT':
return 'object'
return 'object' + (ast.isNullable ? ' | null' : '')
case 'REFERENCE':
return ast.params
case 'STRING':
return 'string'
return 'string' + (ast.isNullable ? ' | null' : '')
case 'TUPLE':
return (() => {
const minItems = ast.minItems
Expand Down Expand Up @@ -284,7 +284,7 @@ function generateRawType(ast: AST, options: Options): string {
case 'UNION':
return generateSetOperation(ast, options)
case 'CUSTOM_TYPE':
return ast.params
return ast.params + (ast.isNullable ? ' | null' : '')
}
}

Expand All @@ -293,6 +293,9 @@ function generateRawType(ast: AST, options: Options): string {
*/
function generateSetOperation(ast: TIntersection | TUnion, options: Options): string {
const members = (ast as TUnion).params.map(_ => generateType(_, options))
if (ast.type === 'UNION' && ast.isNullable) {
members.push('null')
}
const separator = ast.type === 'UNION' ? '|' : '&'
return members.length === 1 ? members[0] : '(' + members.join(' ' + separator + ' ') + ')'
}
Expand Down
9 changes: 9 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function parseNonLiteral(
case 'ANY_OF':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
params: schema.anyOf!.map(_ => parse(_, options, rootSchema, undefined, true, processed, usedNames)),
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
Expand All @@ -112,13 +113,15 @@ function parseNonLiteral(
case 'BOOLEAN':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
type: 'BOOLEAN'
})
case 'CUSTOM_TYPE':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
params: schema.tsType!,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
Expand Down Expand Up @@ -147,20 +150,23 @@ function parseNonLiteral(
case 'NUMBER':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
type: 'NUMBER'
})
case 'OBJECT':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
type: 'OBJECT'
})
case 'ONE_OF':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
params: schema.oneOf!.map(_ => parse(_, options, rootSchema, undefined, true, processed, usedNames)),
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
Expand All @@ -171,6 +177,7 @@ function parseNonLiteral(
case 'STRING':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
type: 'STRING'
Expand Down Expand Up @@ -218,6 +225,7 @@ function parseNonLiteral(
case 'UNION':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
params: (schema.type as JSONSchema4TypeName[]).map(_ =>
parse({...schema, type: _}, options, rootSchema, undefined, true, processed, usedNames)
Expand All @@ -228,6 +236,7 @@ function parseNonLiteral(
case 'UNNAMED_ENUM':
return set({
comment: schema.description,
isNullable: !!schema.nullable,
keyName,
params: schema.enum!.map(_ => parse(_, options, rootSchema, undefined, false, processed, usedNames)),
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
Expand Down
6 changes: 6 additions & 0 deletions src/types/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface TArray extends AbstractAST {
}

export interface TBoolean extends AbstractAST {
isNullable: boolean
type: 'BOOLEAN'
}

Expand Down Expand Up @@ -83,6 +84,7 @@ export interface TLiteral extends AbstractAST {
}

export interface TNumber extends AbstractAST {
isNullable: boolean
type: 'NUMBER'
}

Expand All @@ -91,6 +93,7 @@ export interface TNull extends AbstractAST {
}

export interface TObject extends AbstractAST {
isNullable: boolean
type: 'OBJECT'
}

Expand All @@ -100,6 +103,7 @@ export interface TReference extends AbstractAST {
}

export interface TString extends AbstractAST {
isNullable: boolean
type: 'STRING'
}

Expand All @@ -112,11 +116,13 @@ export interface TTuple extends AbstractAST {
}

export interface TUnion extends AbstractAST {
isNullable: boolean
type: 'UNION'
params: AST[]
}

export interface TCustomType extends AbstractAST {
isNullable: boolean
type: 'CUSTOM_TYPE'
params: string
}
Expand Down