Skip to content

Commit

Permalink
Merge pull request #671 from gethinode/validation
Browse files Browse the repository at this point in the history
Improve argument validation and documentation
  • Loading branch information
markdumay authored Dec 24, 2023
2 parents 865898a + aee6b3a commit 6661434
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 28 deletions.
17 changes: 9 additions & 8 deletions data/structures/card.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ arguments:
type: url
optional: true
comment: >-
Optional address for the button or hyperlink.
Address for the button or hyperlink.
group: partial
class:
type: string
optional: true
parent: merge
comment: >-
Optional class attribute of the card element, e.g. “w-50”.
Class attribute of the card element, e.g. “w-50”.
color:
type: select
optional: true
Expand All @@ -51,7 +51,7 @@ arguments:
default: auto
parent: cascade
comment: >-
Optional padding of the content.
Padding of the content.
options:
values:
- "0"
Expand Down Expand Up @@ -82,7 +82,7 @@ arguments:
default: full
parent: cascade
comment: >-
Optional header components of the card, displayed in small caps.
Header components of the card, displayed in small caps.
options:
values:
- full
Expand All @@ -95,7 +95,7 @@ arguments:
default: none
parent: cascade
comment: >-
Optional footer components of the card, displayed in small caps.
Footer components of the card, displayed in small caps.
options:
values:
- full
Expand All @@ -108,7 +108,7 @@ arguments:
- template.HTML
optional: true
comment: >-
Optional description of the card.
Description of the card.
group: partial
loading:
type: select
Expand All @@ -128,7 +128,7 @@ arguments:
type: path
optional: true
comment: >-
Optional thumbnail image url, displayed on top or the left of the card.
Thumbnail image url, displayed on top or the left of the card.
ratio:
type: select
optional: true
Expand All @@ -146,6 +146,7 @@ arguments:
alt:
type: string
optional: true
release: v0.19.0
comment: >-
Alternate text for the thumbnail, uses "title" by default.
icon:
Expand All @@ -169,5 +170,5 @@ arguments:
body:
optional: true
comment: >-
Optional description of the card.
Description of the card.
group: shortcode
1 change: 1 addition & 0 deletions exampleSite/hugo_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@
"pb-4",
"pb-5",
"pb-md-0",
"pe-1",
"pe-3",
"position-absolute",
"position-fixed",
Expand Down
5 changes: 3 additions & 2 deletions layouts/partials/assets/button.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{{- with .badge }}{{ $badge = . }}{{ end -}}

{{- $label := "" -}}
{{- with .label }}{{ $label = . }}{{ end -}}
{{- if .label }}{{ $label = .label }}{{ else }}{{ $label = $title }}{{ end -}}

{{- $tooltip := "" -}}
{{- if not (strings.HasSuffix $state "active") -}}
Expand Down Expand Up @@ -143,7 +143,8 @@

{{- $attributes := .attributes -}}

<a aria-label="{{ (or $label $title) | safeHTML }}" {{ if ne $state "disabled" }}{{ with $href }}href="{{ . }}"{{ end }}{{ end -}}
<a aria-label="{{ (or $label $title) | safeHTML }}"
{{ if ne $state "disabled" }}{{ with $href }}href="{{ . }}"{{ end }}{{ end -}}
{{- with $id }} id="{{ . }}"{{ end -}}
{{- with $target }} target="{{ . }}"{{ end }}{{ with $rel }} rel="{{ . }}"{{ end -}}
{{- with $toast }} data-toast-target="{{ $toast }}"{{ end -}}
Expand Down
30 changes: 16 additions & 14 deletions layouts/partials/utilities/IsInvalidArgs.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
{{ $error = true }}
{{ end }}

{{ $args := .args }}
{{ if not $args }}
{{- errorf "partial [utilities/ValidateArgs.html] - Missing value for param 'args'" -}}
{{ $error = true }}
{{ end }}
{{ $args := .args | default slice }}

{{ $definitions := (index site.Data.structures $structure).arguments }}
{{ if not $definitions }}
Expand All @@ -40,15 +36,11 @@
{{ end }}
{{ if $extra }}{{ $expected = $expected | append $extra }}{{ end }}
{{ $actual := printf "%T" $val }}
{{ if not (in $expected $actual ) }}
{{ warnf "[%s] argument '%s': expected type '%s', got '%s'" $structure $key (delimit $expected ", ") $actual }}
{{ $error = true }}
{{ end }}

{{/* validate required argument */}}
{{ if and (not $def.optional) (not $val) }}
{{ warnf "[%s] argument '%s': expected value" $structure $key }}
{{ $error = true }}
{{ if $val }}
{{ if not (in $expected $actual ) }}
{{ warnf "[%s] argument '%s': expected type '%s', got '%s'" $structure $key (delimit $expected ", ") $actual }}
{{ $error = true }}
{{ end }}
{{ end }}

{{/* validate permitted values */}}
Expand All @@ -69,6 +61,16 @@
{{ end }}
{{ end }}
{{ end }}

{{/* validate required arguments */}}
{{ range $key, $val := $definitions }}
{{ if not $val.optional }}
{{ if not (in $args $key )}}
{{ warnf "[%s] argument '%s': expected value" $structure $key }}
{{ $error = true }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}

{{ return $error }}
9 changes: 8 additions & 1 deletion layouts/shortcodes/args.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
{{ if not $val.optional }}{{ $required = "yes" }}{{ end }}

{{ $comment := $val.comment }}
{{ if $val.deprecated }}
{{ $comment = printf `{{< release version="%s" short="true" size="sm" inline="true" state="deprecated" >}} %s` $val.deprecated $comment }}
{{ end }}
{{ if $val.release }}
{{ $comment = printf `{{< release version="%s" short="true" size="sm" inline="true" >}} %s` $val.release $comment }}
{{ end }}

{{ $min := "" }}
{{ $max := "" }}
{{ if and $val.options.values (eq $type "select") }}
Expand All @@ -57,5 +64,5 @@
{{ end }}
{{ end }}

{{ partial "assets/table" (dict "page" .Page "input" $table "args" slice) }}
{{ partial "assets/table.html" (dict "page" .Page "input" $table "args" slice) }}
{{ end }}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gethinode/hinode",
"version": "0.22.0-alpha",
"version": "0.22.0-alpha2",
"description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator",
"keywords": [
"hugo",
Expand Down

0 comments on commit 6661434

Please sign in to comment.