Class represents field in the schema.

Data values can be cast to native R types. Casting a value will check the value is of the expected type, is in the correct format, and complies with any constraints imposed by a schema.

# Field$new(descriptor, missingValues = list(""))

Format

R6Class object.

Value

Object of R6Class .

Details

A field descriptor MUST be a JSON object that describes a single field. The descriptor provides additional human-readable documentation for a field, as well as additional information that may be used to validate the field or create a user interface for data entry.

The field descriptor object MAY contain any number of other properties. Some specific properties are defined below. Of these, only the name property is REQUIRED.

name

The field descriptor MUST contain a name property. This property SHOULD correspond to the name of field/column in the data file (if it has a name). As such it SHOULD be unique (though it is possible, but very bad practice, for the data file to have multiple columns with the same name). name SHOULD NOT be considered case sensitive in determining uniqueness. However, since it should correspond to the name of the field in the data file it may be important to preserve case.

title

A human readable label or title for the field.

description

A description for this field e.g. "The recipient of the funds".

Methods

Field$new(descriptor, missingValues = list(""))

Constructor to instantiate Field class.

descriptor Schema field descriptor. missingValues A list with vector strings representing missing values. TableSchemaError Raises any error occured in the process. Field Returns Field class instance.
cast_value(value, constraints=TRUE)

Cast given value according to the field type and format.

value Value to cast against field constraints Gets constraints configuration: it could be set to true to disable constraint checks, or it could be a List of constraints to check errors$TableSchemaError Raises any error occured in the process any Returns cast value
testValue(value, constraints=TRUE)

Test if value is compliant to the field.

value Value to cast against field constraints Constraints configuration Boolean Returns if value is compliant to the field

Properties

name

Returns field name

type

Returns field type

format

Returns field format

required

Returns TRUE if field is required

constraints

Returns list with field constraints

descriptor

Returns field descriptor

Language

The key words MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD, SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this package documents are to be interpreted as described in RFC 2119.

See also

Methods

Public methods


Method new()

Usage

Field$new(
  descriptor,
  base_path = NULL,
  strict = NULL,
  missingValues = as.list(config::get("DEFAULT_MISSING_VALUES", file =
    system.file("config/config.yml", package = "tableschema.r"))),
  ...
)

Arguments

descriptor

Schema field descriptor

missingValues

A list with vector strings representing missing values


Method cast_value()

Usage

Field$cast_value(...)


Method testValue()

Usage

Field$testValue(value, constraints = TRUE)


Method clone()

The objects of this class are cloneable with this method.

Usage

Field$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

DESCRIPTOR = list(name = "height", type = "number") field <- Field$new(descriptor = DESCRIPTOR) # get correct instance field$name
#> [1] "height"
field$format
#> [1] "default"
field$type
#> [1] "number"
# return true on test field$testValue(1)
#> [1] TRUE
# cast value field$cast_value(1)
#> [1] 1
# expand descriptor by defaults field <- Field$new(descriptor = list(name = "name")) field$descriptor
#> $name #> [1] "name" #> #> $type #> [1] "string" #> #> $format #> [1] "default" #>
# parse descriptor with "enum" constraint field <- Field$new(descriptor = list(name = "status", type = "string", constraints = list(enum = list('active', 'inactive')))) field$testValue('active')
#> [1] TRUE
field$testValue('inactive')
#> [1] TRUE
field$testValue('activia')
#> [1] FALSE
field$cast_value('active')
#> [1] "active"
# parse descriptor with "minimum" constraint' field <- Field$new(descriptor = list(name = "length", type = "integer", constraints = list(minimum = 100))) field$testValue(200)
#> [1] TRUE
field$testValue(50)
#> [1] FALSE
# parse descriptor with "maximum" constraint' field <- Field$new(descriptor = list(name = "length", type = "integer", constraints = list(maximum = 100))) field$testValue(50)
#> [1] TRUE
field$testValue(200)
#> [1] FALSE