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
  • Shorthand attribute definition
  • Complete attribute definition
  • default
  • itemType
  • Type concept

Was this helpful?

  1. Schema concept

Shorthand and complete attribute definition

Shorthand attribute definition

The shorthand is a pair of propertyName: Type key/value like this:

const User = attributes({
  name: String,
  brithday: Date,
})(
  class User {
    generateRandomBook() {
      return '...';
    }
  }
);

Complete attribute definition

The complete definition allows you to declare additional info for the attribute. For Array types it's required to use the complete attribute definition because you must specify the itemType.

const User = attributes({
  name: {
    type: String,
    default: 'Anonymous',
  },
  cars: {
    type: Array,
    itemType: String,
    default: ['Golf', 'Polo'],
  },
  book: {
    type: String,
    default: (instance) => instance.generateRandomBook(),
  },
})(
  class User {
    generateRandomBook() {
      return '...';
    }
  }
);

default

The default of an attribute will be used if no value was provided for the specific attribute at construction time.

You can also use a function which receives the instance as a parameter in order to provide the default. The operation must be synchronous and the function will called after all the other attributes are already assigned, thus, you can use the other attributes of your class to compose a default value.

const User = attributes({
  name: {
    type: String,
    default: 'Anonymous', // static default value
  },
  greeting: {
    type: String,
    default: (instance) => instance.greeting(), // dynamic default value
  },
})(
  class User {
    greeting() {
      return `Hello ${this.name}`;
    }
  }
);

Please note that initializing an attribute with undefined will make it fallback to the default value while instantiating the structure, but it will not fallback when assigning the attribute after the structure is already constructed.

const User = attributes({
  name: {
    type: String,
    default: 'Anonymous', // static default value
  },
})(class User {});

const firstUser = new User({ name: undefined });
firstUser.name; // 'Anonymous' =>  fallbacks to default value

const secondUser = new User({ name: 'Some name' });

secondUser.name = undefined;
secondUser.name; // undefined => does not fallback to default value

itemType

The itemType of an attribute is used to validate and coerce the type of each item from the attribute, like when the attribute type is Array or some class that extends Array.

Type concept

Each attribute needs a type definition, that's how Structure validates and coerces the attribute's value. It can be divided into three categories (as in right now):

  • Primitives (Number, String, Boolean)

  • Classes (Date, Object, regular Classes and Structure classes as well)

  • Array/Array-like (Array, extended Array)

PreviousSchema conceptNextCircular reference

Last updated 4 years ago

Was this helpful?

Please refer to in order to check a bit more on validation properties.

Validation