Skip to content

Commit

Permalink
feat: type can be omitted when there is enum
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Apr 29, 2018
1 parent 9684928 commit 872905b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
6 changes: 0 additions & 6 deletions demo/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@
]
},
"typeUnionMember7": {
"type": "string",
"enum": [
"foo",
"bar"
Expand Down Expand Up @@ -362,7 +361,6 @@
"type": "object",
"properties": {
"kind": {
"type": "string",
"enum": [
"enum member 1",
"enum member 2"
Expand All @@ -386,7 +384,6 @@
"type": "object",
"properties": {
"kind": {
"type": "integer",
"enum": [
0,
1
Expand All @@ -410,7 +407,6 @@
"type": "object",
"properties": {
"kind": {
"type": "string",
"enum": [
"foo",
"bar"
Expand Down Expand Up @@ -441,7 +437,6 @@
]
},
"TypeUnion8": {
"type": "string",
"enum": [
"foo",
"bar"
Expand Down Expand Up @@ -538,7 +533,6 @@
"type": "number"
},
"kind": {
"type": "integer",
"enum": [
0,
1
Expand Down
15 changes: 10 additions & 5 deletions src/json-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function getJsonSchemaProperty (memberType: Type | ObjectModel | ArrayModel | Un
} else if (memberType.kind === 'enum') {
if (memberType.type === 'string') {
return {
type: 'string',
type: undefined,
enum: memberType.enums
}
} else {
Expand All @@ -65,6 +65,7 @@ function getJsonSchemaProperty (memberType: Type | ObjectModel | ArrayModel | Un
minimum: undefined,
maximum: undefined
})
delete definition.type
return definition
}
} else if (memberType.kind === 'reference') {
Expand Down Expand Up @@ -98,13 +99,18 @@ function getJsonSchemaProperty (memberType: Type | ObjectModel | ArrayModel | Un
maxProperties: memberType.maxProperties && memberType.maxProperties < memberType.members.length ? memberType.maxProperties : undefined
}
} else if (memberType.kind === 'string') {
if (memberType.enums) {
return {
type: undefined,
enum: memberType.enums
}
}
return {
type: memberType.kind,
minLength: memberType.minLength,
maxLength: memberType.maxLength,
pattern: memberType.pattern,
default: memberType.default,
enum: memberType.enums
default: memberType.default
}
} else if (memberType.kind === 'union') {
return {
Expand Down Expand Up @@ -217,7 +223,6 @@ type Definition =
maximum?: number;
exclusiveMinimum?: number;
exclusiveMaximum?: number;
enum?: number[],
multipleOf?: number;
default?: number;
}
Expand Down Expand Up @@ -249,11 +254,11 @@ type Definition =
type: undefined,
$ref?: string,
anyOf?: Definition[]
enum?: string[]
}
|
{
type: 'string',
enum?: string[],
minLength?: number;
maxLength?: number;
pattern?: string;
Expand Down

0 comments on commit 872905b

Please sign in to comment.