Skip to content

Commit

Permalink
docs: improve rendering of "alternatives" schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jul 22, 2019
1 parent 6e40f18 commit 7b061ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ A key/value map of variables that modules can reference when using this environm

A list of environments to configure for the project.

| Type | Required |
| -------------- | -------- |
| `alternatives` | No |
| Type | Required |
| ------------------------------- | -------- |
| `array[object] | array[string]` | No |

Example:

Expand Down
20 changes: 19 additions & 1 deletion garden-service/src/docs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ function normalizeKeyDescription(description: Description): NormalizedDescriptio
let hasChildren: boolean = false
let arrayType: string | undefined
const { type } = description
const formattedType = formatType(description)

const children = type === "object" && Object.entries(description.children || {})
const items = type === "array" && description.items

if (children && children.length > 0) {
hasChildren = true
} else if (items && items.length > 0) {
Expand All @@ -164,7 +167,6 @@ function normalizeKeyDescription(description: Description): NormalizedDescriptio
}

const formattedName = type === "array" ? `${description.name}[]` : description.name
const formattedType = (type === "array" && arrayType ? `array[${arrayType}]` : type) || ""

return {
...description,
Expand All @@ -178,6 +180,22 @@ function normalizeKeyDescription(description: Description): NormalizedDescriptio
}
}

function formatType(description: Description) {
const { type } = description
const items = type === "array" && description.items

if (items && items.length > 0) {
// We don't consider an array of primitives as children
const arrayType = items[0].type
return `array[${arrayType}]`
} else if (type === "alternatives") {
// returns e.g. "string|number"
return uniq(description.alternatives.map(formatType)).join(" | ")
} else {
return type || ""
}
}

export function getDefaultValue(description: Joi.Description) {
const defaultSpec = get(description, "flags.default")

Expand Down

0 comments on commit 7b061ec

Please sign in to comment.