Field: Field

FieldR Documentation

Field

Description

Basic field from which other fields should extend

Details

It applies no formatting by default, and should only be used in cases where data does not need to be formatted before being serialized or deserialized. On error, the name of the field will be returned.

Super class

staypuft::FieldABC -> Field

Public fields

class_name

(character) xxx

CHECK_ATTRIBUTE

(logical) default: TRUE

creation_index

(integer) xxx

default

a class, default: Missing

attribute

(character) xxx

data_key

(character) xxx

validate

xxx

required

(logical) xxx

allow_none

(logical) xxx

load_only

(logical) xxx

dump_only

(logical) xxx

missing

(logical) xxx

metadata

Extra arguments to be stored as metadata.

error_messages

(list) xxx

validators

(list) xxx

Methods

Public methods

Inherited methods

Method new()

Create a new Field object

Usage
Field$new(
  default = miss_ing,
  attribute = NULL,
  data_key = NULL,
  validate = NULL,
  required = FALSE,
  allow_none = NULL,
  load_only = FALSE,
  dump_only = FALSE,
  missing = miss_ing,
  error_messages = NULL
)
Arguments
default

If set, this value will be used during serialization if the input value is missing. If not set, the field will be excluded from the serialized output if the input value is missing. May be a value or a callable.

attribute

The name of the key to get the value from when deserializing. If None, assumes the key has the same name as the field.

data_key

The name of the key to get the value from when deserializing. If None, assumes the key has the same name as the field.

validate

Validator or collection of validators that are called during deserialization. Validator takes a field's input value as its only parameter and returns a boolean. If it returns FALSE, an ValidationError is raised.

required

Raise a ValidationError if the field value is not supplied during deserialization.

allow_none

Set this to TRUE if None should be considered a valid value during validation/deserialization. If missing=NULL and allow_none is unset, will default to TRUE. Otherwise, the default is FALSE.

load_only

If TRUE skip this field during serialization, otherwise its value will be present in the serialized data.

dump_only

If TRUE skip this field during deserialization, otherwise its value will be present in the deserialized object. In the context of an HTTP API, this effectively marks the field as "read-only".

missing

Default deserialization value for the field if the field is not found in the input data. May be a value or a callable.

error_messages

Overrides for Field.default_error_messages.

Returns

A new Field object


Method print()

print method for Field objects

Usage
Field$print(x, ...)
Arguments
x

self

...

ignored


Method get_value()

Return the value for a given key from an object.

Usage
Field$get_value(obj, attr, accessor = NULL, default = miss_ing)
Arguments
obj

The object to get the value from

attr

The attribute/key in obj to get the value from.

accessor

(callback) A callable used to retrieve the value of attr

default

If set, this value will be used during serialization if the input value is missing. If not set, the field will be excluded from the serialized output if the input value is missing. May be a value or a callable. from the object obj. Defaults to marshmallow.utils.get_value.


Method validate_()

Perform validation on value. Raise a ValidationError if validation does not succeed.

Usage
Field$validate_(value)
Arguments
value

a value


Method fail()

A helper method that simply raises a ValidationError

Usage
Field$fail(key)
Arguments
key

a key


Method validate_missing_()

Validate missing values. Raise a ValidationError if value should be considered missing.

Usage
Field$validate_missing_(value)
Arguments
value

a value


Method serialize()

Pulls the value for the given key from the object, applies the field's formatting and returns the result.

Usage
Field$serialize(attr, obj, accessor = NULL)
Arguments
attr

(character) The attribute or key to get from the object.

obj

(character) The object to pull the key from.

accessor

(callback) Function used to pull values from obj.

Details

raise ValidationError: In case of formatting problem

Returns

xxxx


Method deserialize()

Deserialize value.

Usage
Field$deserialize(value, attr = NULL, data = NULL, ...)
Arguments
value

The value to be deserialized.

attr

(character) The attribute/key in data to be deserialized.

data

(list) The raw input data passed to the Schema.load.

Details

raise ValidationError: If an invalid value is passed or if a required value is missing.


Method bind_to_schema()

Update field with values from its parent schema.

Usage
Field$bind_to_schema(field_name, schema)
Arguments
field_name

(character) Field name set in schema.

schema

Parent schema.


Method serialize_()

Serializes value to a basic Python datatype. Noop by default. Concrete :class:Field classes should implement this method.

Usage
Field$serialize_(value, attr = NULL, obj = NULL)
Arguments
value

The value to be deserialized.

attr

(character) The attribute/key in data to be deserialized.

obj

(character) The object to pull the key from.

Details

raise ValidationError: In case of formatting or validation failure.

Returns

The serialized value


Method deserialize_()

Deserialize value. Concrete :class:Field classes should implement this method.

Usage
Field$deserialize_(value, attr, data)
Arguments
value

The value to be deserialized.

attr

(character) The attribute/key in data to be deserialized.

data

(list) The raw input data passed to the Schema.load.

Details

raise ValidationError: In case of formatting or validation failure.

Returns

The deserialized value


Method context()

The context dictionary for the parent Schema

Usage
Field$context()

Method root()

Reference to the Schema that this field belongs to even if it is buried in a List

Usage
Field$root()
Returns

None for unbound fields


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

x <- fields$field()
x
x$error_messages

z <- fields$character()
z
z$error_messages
z$serialize(attr = "foo", obj = list(foo = "bar"))
z$deserialize("foo")
z$deserialize(fields$missing())

x <- Schema$new("x", cow = fields$character(data_key = "stuff"))
x
x$fields$cow$data_key
if (interactive()) x$load(list(cow = 5))
x$load(list(stuff = 5))

ropensci/staypuft documentation built on Sept. 9, 2022, 4:35 p.m.