Section » field

field(name) → value or null
field(name, options) → object/value or null
field(name, loader) → value or null
field(name, loader, options) → object/value or null

Retrieve a field's value from the section, optionally supplying a loader to validate and/or transform the value, and/or an options object.

enocolor: blue
sound:
javascriptsection.field('color');   // returns 'blue'
section.field('sound');   // returns null

section.field('sound', { required: true });   // throws an error
section.field('sound', { enforceElement: true });   // returns null
section.field('texture', { enforceElement: true });   // throws an error

section.field('color', ({ value }) => value.toUpperCase());   // returns 'BLUE'

section.field('color', ({ value }) => {   // throws an error
  if(value !== 'green') {
    throw 'Only green is allowed!';
  }
  return value;
});

section.field('color', { withElement: true });
  // returns { element: [object Field name="blue" value="blue"], value: 'blue' }

Parameters

name

A string representing the name of the field to return.

loader

A function returning the transformed/validated value or throwing an error.

options

enforceElement

Whether the field must be present in the document (true or false, defaults to false)

enforceValue

Whether there must be a value or the field is allowed to be empty (defaults to false)

required

Alias for enforceValue (this exists on many methods and depending on context refers to either element or value)

withElement

Whether to return an object of the form { element: ..., value: ... } instead of just the value (default false)

Return value

The value of the field, or null if empty, or an array of the form { element: ..., value: ... } when the option withElement is specified.