Structure
Structure v2
Structure v2
  • Introduction
  • Schema concept
    • Shorthand and complete attribute definition
    • Circular reference
    • Nullable attributes
  • Custom setters and getters
  • Coercion
    • Primitive type coercion
    • Arrays coercion
    • Generic coercion
    • Recursive coercion
    • Disabling coercion
  • Validation
    • String validations
    • Number validations
    • Boolean validations
    • Date validations
    • Array validations
    • Attribute reference
    • Nested validations
    • Validate raw data
  • Strict mode
  • Cloning an instance
  • Serialization
  • Testing
  • Battlecry generators
  • Migrating from v1
  • Support and compatibility
  • Changelog
  • Contributing
  • License
  • GitHub
Powered by GitBook
On this page
  • Validation errors
  • Nullability and defaults
  • New public methods
  • Upgrade Node

Was this helpful?

Migrating from v1

Migrating an app that uses Structure v1 to use Structure v2 requires some changes in the way validation errors are expected to be returned, default fallback of null values and upgrading Node (if you use a version lower than v10.13.0)

Validation errors

In v1, validations used to be like this:

const Book = attributes({
  name: {
    type: String,
    required: true,
  },
})(class Book {});

const User = attributes({
  initials: {
    type: String,
    minLength: 2,
  },
  favoriteBook: Book,
  books: {
    type: Array,
    itemType: Book,
  },
})(class User {});

const user = new User({
  initials: 'A',
  favoriteBook: new Book(),
  books: [new Book()],
});

const { valid, errors } = user.validate();

valid; // false
errors; /*
[
  { message: '"initials" length must be at least 2 characters long', path: 'initials' },
  { message: '"name" is required', path: 'favoriteBook.name' },
  { message: '"name" is required', path: 'books.0.name' }
]
*/

Notice that the message used to contain only the name of the attribute (so if it's nested it will only show the attribute name of the nested structure) and the path is a string that use a dot . to represent that it's a nested attribute.

If your app relies on the content of the message or the path, you'll have to consider that it's now returned like this:

const Book = attributes({
  name: {
    type: String,
    required: true,
  },
})(class Book {});

const User = attributes({
  initials: {
    type: String,
    minLength: 2,
  },
  favoriteBook: Book,
  books: {
    type: Array,
    itemType: Book,
  },
})(class User {});

const user = new User({
  initials: 'A',
  favoriteBook: new Book(),
  books: [new Book()],
});

const { valid, errors } = user.validate();

valid; // false
errors; /*
[
  { message: '"initials" length must be at least 2 characters long', path: ['initials'] },
  { message: '"favoriteBook.name" is required', path: ['favoriteBook', 'name'] },
  { message: '"books[0].name" is required', path: ['books', 0, 'name'] }
]
*/

In v2 the message contains the whole path for the attribute (using dot . to represent it's a nested attribute) and the path is not an array.

Nullability and defaults

New public methods

So if any of your structures declare methods with these names you'll have to change it or it's gonna break.

Upgrade Node

PreviousBattlecry generatorsNextSupport and compatibility

Last updated 4 years ago

Was this helpful?

So if your apps relies in the content of the message you'll probably have to do some parsing of this message now. And if it relies in the path to be a string, you can use .

Since v2 now allows having nullable attributes, non-nullable attributes will fallback to the the same way it happens with attributes with undefined value since v1.

Structure v2 also adds two new instance methods to the structures: instance.get(attrName) and instance.set(attrName, attrValue). They are not replacing the normal getters and setters, which are still there, they are just alternatives for the case you use a

The minimum Node LTS supported by Structure is now v10.13.0 which is the lowest active .

path.join('.')
custom setter and/or getter
LTS
default