staypuft is a port of Python’s marshmallow for converting objects to and from R data structures

Installation

remotes::install_github("ropensci/staypuft")
library("staypuft")

hello world

z <- Schema$new("MySchema",
  name = puft_fields$character(),
  title = puft_fields$character(),
  num = puft_fields$integer()
)
z
#> <schema: MySchema>
#> fields: name, title, num
x <- list(name = "Jane Doe", title = "Howdy doody", num = 5.5)
z$load(data = x)
#> $name
#> [1] "Jane Doe"
#> 
#> $title
#> [1] "Howdy doody"
#> 
#> $num
#> [1] 5.5
z$load(data = x, as_df = TRUE)
#> # A tibble: 1 x 3
#>   name     title         num
#>   <chr>    <chr>       <dbl>
#> 1 Jane Doe Howdy doody   5.5
z$load_json(jsonlite::toJSON(x, auto_unbox=TRUE))
#> $name
#> [1] "Jane Doe"
#> 
#> $title
#> [1] "Howdy doody"
#> 
#> $num
#> [1] 5.5

strict mode for integer

z <- Schema$new("MySchema",
  name = puft_fields$character(),
  title = puft_fields$character(),
  num = puft_fields$integer(strict = TRUE)
)
z$fields$num
#> <fields.Integer>
#> default=Missing
#> attribute=none
#> validate=none
#> required=FALSE
#> load_only=FALSE
#> dump_only=FALSE
#> missing=Missing
#> allow_none=FALSE
#> error_messages=required: 'Missing data for required field.'; null: 'Field may not be null.'; validator_failed: 'Invalid value.'; invalid: 'Not a valid integer.'
x <- list(name = "Jane Doe", title = "Howdy doody", num = 5.5)
z$load(data = x)
#> Error in super$fail("invalid"): ValidationError: Not a valid integer.

another example

z <- Schema$new("MySchema",
  name = puft_fields$character(),
  title = puft_fields$character(),
  num = puft_fields$integer(),
  uuid = puft_fields$uuid(),
  foo = puft_fields$boolean()
)
x <- list(name = "Jane Doe", title = "Howdy doody", num = 5.5,
    uuid = "9a5f6bba-4101-48e9-a7e3-b5ac456a04b5",
    foo = TRUE)
z$load(data = x)
#> $name
#> [1] "Jane Doe"
#> 
#> $title
#> [1] "Howdy doody"
#> 
#> $num
#> [1] 5.5
#> 
#> $uuid
#> [1] "9a5f6bba-4101-48e9-a7e3-b5ac456a04b5"
#> 
#> $foo
#> [1] TRUE

# invalid uuid
x$uuid <- "foo-bar"
z$load(data = x)
#> Error in super$fail("invalid_uuid"): ValidationError: Not a valid UUID.

# invalid boolean
x$uuid <- "9a5f6bba-4101-48e9-a7e3-b5ac456a04b5"
x$foo <- "bar"
z$load(data = x)
#> Error in super$fail("invalid"): ValidationError: Not a valid boolean.

Meta