A model of a schema with helpful methods for working with the schema and supported data.
Schema instances can be initialized with a schema source as a url to a JSON file or a JSON object.
The schema is initially validated (see validate).
By default validation errors will be stored in $errors
but in a strict mode it will be instantly raised.
# Schema.load(descriptor, strict=FALSE)
R6Class
object.
Object of R6Class
.
Schema$new(descriptor = descriptor, strict = strict)
Use Schema.load
to instantiate Schema
class.
getField(name)
Get schema field by name.
name
String with schema field name.
(Field/NULL)
Returns Field
instance or NULL
if not found.
addField(descriptor)
Add new field to schema. The schema descriptor will be validated with newly added field descriptor.
descriptor
List of field descriptor.
TableSchemaError
Raises any error occured in the process.
(Field/NULL)
Returns added Field
instance or NULL
if not added.
removeField(name)
Remove field resource by name. The schema descriptor will be validated after field descriptor removal.
name
String with schema field name.
TableSchemaError
Raises any error occured in the process.
(Field/NULL)
Returns removed Field
instances or NULL
if not found.
castRow(row)
Cast row based on field types and formats.
row
Data row as a list of values.
(any)
Returns cast data row.
infer(rows, headers=1)
Cast row based on field types and formats.
rows
List of lists representing rows.
headers
data sample headers, one of:
row number containing headers (rows
should contain headers rows)
list of headers (rows
should NOT contain headers rows)
{Object}
Returns Table Schema descriptor.
commit(strict)
Cast row based on field types and formats.
strict
Boolean, alter strict mode for further work.
TableSchemaError
Raises any error occured in the process.
(Boolean)
Returns TRUE
on success and FALSE
if not modified.
save(target)
Cast row based on field types and formats.
target
String, path where to save a descriptor.
TableSchemaError
Raises any error occured in the process.
(Boolean)
Returns TRUE
on success.
valid
Returns validation status. It always TRUE
in strict mode.
errors
Returns validation errors. It always empty in strict mode.
descriptor
Returns list of schema descriptor.
primaryKey
Returns string list of schema primary key.
foreignKeys
Returns list of schema foreign keys.
fields
Returns list of Field
instances.
fieldNames
Returns a list of field names.
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.
new()
Schema$new(descriptor = "{}", strict = FALSE, caseInsensitiveHeaders = FALSE)
descriptor
schema descriptor, a JSON string, URL or file
strict
flag to alter validation behaviour:
if FALSE
error will not be raised and all error will be collected in schema$errors
if TRUE
any validation error will be raised immediately
getField()
Schema$getField(fieldName, index = 1)
addField()
Schema$addField(descriptor)
descriptor
schema descriptor, a JSON string, URL or file
removeField()
Schema$removeField(name)
castRow()
Schema$castRow(items, failFast = FALSE, skipConstraints = FALSE)
infer()
Schema$infer(rows, headers = 1)
commit()
Schema$commit(strict = NULL)
strict
flag to alter validation behaviour:
if FALSE
error will not be raised and all error will be collected in schema$errors
if TRUE
any validation error will be raised immediately
save()
Schema$save(target)
clone()
The objects of this class are cloneable with this method.
Schema$clone(deep = FALSE)
deep
Whether to make a deep clone.