Structure does type coercion based on the declared schema, let's break it into 3 categories:
Structure never coerces the following scenarios:
- value is
undefined
; - value is
null
whennullable
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