Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 996 Bytes

README.md

File metadata and controls

36 lines (25 loc) · 996 Bytes

Coercion

Structure does type coercion based on the declared schema, let's break it into 3 categories:

Observations

Structure never coerces the following scenarios:

  • value is undefined;
  • value is null when nullable option is enabled;
  • value is already of the declared type (except for arrays, we'll talk more about this soon).

Also, Structure only does array items coercion during instantiation, so mutating an array (using push, for example) won't coerce the new item:

const Library = attributes({
  books: {
    type: Array,
    itemType: String,
  },
})(class Library {});

const library = new Library({
  books: [1984],
});

library.books; // ['1984'] => coerced number to string

library.books.push(42);

library.books; // ['1984', 42] => new item was not coerced