loaders

enojs provides a set of core loaders for important types that are available out of the box (in addition to the possiblity to define your own custom loaders, which can be passed as callbacks to all accessors).

The loaders are exposed through the API as drop in replacements to the standard accessors:

  • Field
    • value() => [loaderName]()
  • Fieldset
    • entry() => [loaderName]()
  • List
    • items() => [loaderName]Items()
  • Section
    • field() => [loaderName]()
    • list() => [loaderName]List()

So for instance, instead of calling ...

document.field('done', { required: true });  // returns 'yes'
document.list('visitor_counts', { required: true });  // returns [ '476', '213', '330', ... ]

... you can just replace field or augment list with the loader name ...

document.boolean('done', { required: true });  // returns true
document.integerList('visitor_counts', { required: true });  // returns [ 476, 213, 330, ... ]

Here's another full example:

const eno = require('enojs');

const doc = eno.parse(`
  publish: yes
  location: 36.987094, -25.091719
  contact: contact@faulty
`);

doc.boolean('publish');
  // returns true

doc.latLng('location');
  // returns {
  //   lat:  36.987094,
  //   lng: -25.091719
  // }

doc.email('contact');
  // throws an error: 'contact' must contain a valid email address, for instance 'jane.doe@eno-lang.org'.

Note that some loaders only perform validation and return their input unaltered as string (e.g. color, email), while others both validate and transform the value into a new type (e.g. float, boolean) or even object (e.g. latLng).

Subpages

boolean
color
commaSeparated
date
datetime
float
integer
json
latLng
number
string
url