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

Was this helpful?

  1. Validation

Attribute reference

You can reference attributes for some validations so the value of the referenced attribute will be used for the comparison:

const User = attributes({
  name: String,
  password: String,
  passwordConfirmation: {
    type: String,
    equal: { attr: 'password' },
  },
})(class User {});

const user = new User({
  name: 'Gandalf',
  password: 'safestpasswordever',
  passwordConfirmation: 'notthatsafetho',
});

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

valid; // false
errors; /* [
  {
    message: '"passwordConfirmation" must be one of [ref:password]',
    path: ['passwordConfirmation']
  }
]
*/
PreviousArray validationsNextNested validations

Last updated 4 years ago

Was this helpful?