Skip to content

Commit

Permalink
fix: Add more valid types for JSON type
Browse files Browse the repository at this point in the history
fixes #303
  • Loading branch information
Valentin Palkovič committed Jan 26, 2022
1 parent 2f96a6f commit 3ead22c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ into:
User: {
properties: {
biography: {
anyOf: [
{ type: 'object' },
{
type: 'array',
},
]
type: [
'number',
'string',
'boolean',
'object',
'array',
'null'
],
},
createdAt: { format: 'date-time', type: 'string' },
email: { type: 'string' },
Expand Down
31 changes: 11 additions & 20 deletions src/generator/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import type {
} from 'json-schema'
import { assertFieldTypeIsString } from './assertions'

function getJSONSchemaScalar(fieldType: PrismaPrimitive): JSONSchema7TypeName {
function getJSONSchemaScalar(
fieldType: PrismaPrimitive,
): JSONSchema7TypeName | Array<JSONSchema7TypeName> {
switch (fieldType) {
case 'Int':
case 'BigInt':
Expand All @@ -34,7 +36,7 @@ function getJSONSchemaScalar(fieldType: PrismaPrimitive): JSONSchema7TypeName {
case 'Decimal':
return 'number'
case 'Json':
return 'object'
return ['number', 'string', 'boolean', 'object', 'array', 'null']
case 'Boolean':
return 'boolean'
default:
Expand All @@ -53,7 +55,13 @@ function getJSONSchemaType(field: DMMF.Field): JSONSchema7['type'] {
? 'string'
: 'object'

return isRequired || isList ? scalarFieldType : [scalarFieldType, 'null']
const isFieldUnion = Array.isArray(scalarFieldType)

return isRequired || isList
? scalarFieldType
: isFieldUnion
? Array.from(new Set([...scalarFieldType, 'null']))
: [scalarFieldType, 'null']
}

function getDefaultValue(field: DMMF.Field): JSONSchema7['default'] {
Expand Down Expand Up @@ -129,21 +137,6 @@ function isSingleReference(field: DMMF.Field) {
return !isScalarType(field) && !field.isList && !isEnumType(field)
}

function isJSONField(field: DMMF.Field) {
return isScalarType(field) && getJSONSchemaScalar(field.type) === 'object'
}

function getJSONFieldProperty(): JSONSchema7 {
return {
anyOf: [
{ type: 'object' },
{
type: 'array',
},
],
}
}

function getEnumListByDMMFType(modelMetaData: ModelMetaData) {
return (field: DMMF.Field): string[] | undefined => {
const enumItem = modelMetaData.enums.find(
Expand Down Expand Up @@ -188,8 +181,6 @@ export function getJSONSchemaProperty(

const property = isSingleReference(field)
? getJSONSchemaForPropertyReference(field, transformOptions)
: isJSONField(field)
? getJSONFieldProperty()
: getPropertyDefinition(modelMetaData, transformOptions, field)

return [field.name, property, propertyMetaData]
Expand Down
36 changes: 21 additions & 15 deletions src/tests/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ describe('JSON Schema Generator', () => {
User: {
properties: {
biography: {
anyOf: [
{ type: 'object' },
{
type: 'array',
},
type: [
'number',
'string',
'boolean',
'object',
'array',
'null',
],
},
createdAt: { format: 'date-time', type: 'string' },
Expand Down Expand Up @@ -139,11 +141,13 @@ describe('JSON Schema Generator', () => {
User: {
properties: {
biography: {
anyOf: [
{ type: 'object' },
{
type: 'array',
},
type: [
'number',
'string',
'boolean',
'object',
'array',
'null',
],
},
createdAt: { format: 'date-time', type: 'string' },
Expand Down Expand Up @@ -228,11 +232,13 @@ describe('JSON Schema Generator', () => {
User: {
properties: {
biography: {
anyOf: [
{ type: 'object' },
{
type: 'array',
},
type: [
'number',
'string',
'boolean',
'object',
'array',
'null',
],
},
createdAt: { format: 'date-time', type: 'string' },
Expand Down

0 comments on commit 3ead22c

Please sign in to comment.