Skip to content

Commit

Permalink
Merge pull request #38 from nmfs-fish-tools/37-use-json-schema-to-val…
Browse files Browse the repository at this point in the history
…idate-terms-in-the-dictionary

37 use json schema to validate terms in the dictionary
  • Loading branch information
ChristineStawitz-NOAA authored Oct 7, 2022
2 parents 41b0711 + 63f42ff commit 489e28d
Show file tree
Hide file tree
Showing 19 changed files with 442 additions and 6 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/call-r-cmd-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Run r cmd check
name: call-r-cmd-check
# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
on:
# The default build trigger is to run the action on every push and pull request, for any branch
push:
# To run the default repository branch weekly on sunday, uncomment the following 2 lines
#schedule:
#- cron: '0 0 * * 0'
jobs:
call-workflow:
uses: nmfs-fish-tools/ghactions4r/.github/workflows/r-cmd-check.yml@main
9 changes: 7 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0.9000
RoxygenNote: 7.2.1
Imports:
mvbutils
mvbutils,
tools
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Config/testthat/parallel: true
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export(Weight)
export(add_object)
export(read_in)
export(shiny_dd)
export(validate_Rd)
import(tools)
72 changes: 72 additions & 0 deletions R/validate_Rd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' Function to validate terms in the dictionary.
#'
#' The dictionary terms are written in R documentation (Rd) format.
#' This function reads the Rd file using the `tools::parse_Rd` function
#' validate the information in each section (e.g., examples, rationale,
#' range of possible values, etc.).
#'
#' @param Rd_file a Rd filename to use as input.
#' @return a message if the term passes all validations.
#' @import tools
#' @export
validate_Rd <- function(Rd_file) {
if (is.null(Rd_file)) stop("Please provide the path to the R documentation (Rd) file.")

Rd <- tools::parse_Rd(Rd_file)
tags <- tools:::RdTags(Rd)

# Validate name
name <- Rd[[which(tags == "\\name")]]
if (is.null(name[[1]][1])) stop("Name is not provided. This should be the basename of the Rd file.")

# Validate title
title <- Rd[[which(tags == "\\title")]]
if (is.null(title[[1]][1])) stop("Title is not provided. Title should be capitalized and not end in a period.")

# Validate description
description <- Rd[[which(tags == "\\description")]]
if (gsub("[\r\n]", "", description[[2]][1]) == title[[1]][1] |
is.null(description[[1]][1])) {
stop("Description is not provided. This should be capitalized and not end in a period.")
}

# Extract format section for validating examples, rationale, alternatives, range of possible values, and units
format <- Rd[[which(tags == "\\format")]][[2]]
items <- c("Examples", "Rationale", "Alternatives", "Range of possible values", "Units")
missing_items <- items[!is.element(items, unlist(format))]
if (any(!is.element(items, unlist(format)))) stop(paste("Missing", paste(missing_items, collapse = ", ")))

# Validate examples
if (format[[2]][[1]][[1]][1] == "Examples" &
length(format[[2]][[2]]) == 0) {
stop("Examples are not provided. Use NA if there is no input for examples.")
}

# Validate rationale
if (format[[4]][[1]][[1]][1] == "Rationale" &
length(format[[4]][[2]]) == 0) {
stop("Rationale is not provided.")
}

# Validate alternatives
if (format[[6]][[1]][[1]][1] == "Alternatives" &
length(format[[6]][[2]]) == 0) {
stop("Alternatives are not provided. Use NA if there is no input for alternatives.")
}

# Validate range of possible values
if (format[[8]][[1]][[1]][1] == "Range of possible values" &
length(format[[8]][[2]]) == 0) {
stop("Range of possible values is not provided. Use NA if there is no input for range of possible values.")
}

# Validate range of possible values
if (format[[10]][[1]][[1]][1] == "Units" &
length(format[[10]][[2]]) == 0) {
stop("Units are not provided. Use NA if there is no input for units.")
}


# Return a message if the term passes all validations
message(paste("Term", name[[1]][1], "has passed all validations!"))
}
34 changes: 34 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_good_input.Rd

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

25 changes: 25 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_missing_items.Rd

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

33 changes: 33 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_no_alternatives.Rd

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

31 changes: 31 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_no_description.Rd

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

33 changes: 33 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_no_examples.Rd

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

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

27 changes: 27 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_no_rationale.Rd

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

34 changes: 34 additions & 0 deletions inst/extdata/testthat_data/validate_Rd_no_units.Rd

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

3 changes: 2 additions & 1 deletion man/Biomass.Rd

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

2 changes: 1 addition & 1 deletion man/Forecast.Rd

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

Loading

0 comments on commit 489e28d

Please sign in to comment.