Structure
Structure v1
Structure v1
  • Introduction
  • Schema concept
    • Shorthand and complete type descriptor
    • Circular reference
    • Nullable attributes
  • Coercion
    • Primitive type coercion
    • Arrays coercion
    • Generic coercion
    • Recursive coercion
    • Observations
  • Validation
    • String validations
    • Number validations
    • Boolean validations
    • Date validations
    • Array validations
    • Attribute reference
    • Nested validations
    • Validate raw data
  • Strict mode
  • Cloning an instance
  • Serialization
  • Contributing
  • License
  • GitHub
Powered by GitBook
On this page

Was this helpful?

  1. Validation

Nested validations

Structure will validate nested values, including array items validations and nested structures.

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' }
]
*/
PreviousAttribute referenceNextValidate raw data

Last updated 5 years ago

Was this helpful?