Learn R Programming

tableschema.r (version 1.1.2)

Field: Field class

Description

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.

Usage

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

Value

Object of R6Class .

Format

R6Class object.

Arguments

descriptor

Schema field descriptor

missingValues

A list with vector strings representing missing values

base_path

see description

strict

see description

value

see description

constraints

see description

...

see description

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.

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".

See Also

Examples

Run this code
DESCRIPTOR = list(name = "height", type = "number")

field <- Field$new(descriptor = DESCRIPTOR)

# get correct instance
field$name
field$format
field$type

# return true on test
field$testValue(1)

# cast value
field$cast_value(1)

# expand descriptor by defaults
field <- Field$new(descriptor = list(name = "name"))

field$descriptor


# parse descriptor with "enum" constraint
field <- Field$new(descriptor = list(name = "status", type = "string", 
                   constraints = list(enum = list('active', 'inactive'))))

field$testValue('active')
field$testValue('inactive')
field$testValue('activia')
field$cast_value('active')


# parse descriptor with "minimum" constraint'
field <- Field$new(descriptor = list(name = "length", type = "integer", 
                   constraints = list(minimum = 100)))

field$testValue(200)
field$testValue(50)


# parse descriptor with "maximum" constraint'
field <- Field$new(descriptor = list(name = "length", type = "integer", 
                   constraints = list(maximum = 100)))

field$testValue(50)
field$testValue(200)

Run the code above in your browser using DataLab