Strict mode

To instantiate a structure that automatically throws an error if that is invalid, you can use the buildStrict function.

const { attributes } = require('structure');
const User = attributes({
  name: {
    type: String,
    required: true,
  },
  age: Number,
})(class User {});

const user = User.buildStrict({
  age: 'Twenty',
});

// Error: Invalid Attributes
// details: [
//   { message: '"name" is required', path: ['name'] },
//   { message: '"age" must be a number', path: ['age'] }
// ]

Custom error

Normally buildStrict will throw a default Error when attributes are invalid but you can customize the error class that will be used passing a strictValidationErrorClass to the second parameter of the attributes function.

The value of strictValidationErrorClass should be a class that accepts an array of erros in the constructor.

Last updated

Was this helpful?