DefinableMixin

Hierarchy

  • DefinableMixin

Implements

Index

Methods

Methods

equals

equals(other: any): boolean

Implementation of Definable

Evaluates if value and value's type of passed other instance are equal to current one.

example

class Person extends DefinableMixin {
firstName: string;
lastName: string;
age: number;
constructor(firstName: string, lastName: string, age: number) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
const firstPerson = new Person('Jane', 'Doe', 28);
const secondPerson = new Person('Jane', 'Doe', 28);
expect(firstPerson.equals(secondPerson)).to.be.true;

example

const firstPerson = new Person('Jane', 'Doe', 28);
const secondPerson = new Person('John', 'Doe', 30);
expect(firstPerson.equals(secondPerson)).to.be.false;

example

const firstPerson = new Person('Jane', 'Doe', 28);
const secondPerson = {firstName: 'John', lastName: 'Doe', age: 30);
expect(firstPerson.equals(secondPerson)).to.be.false;

Parameters:

NameTypeDescription
otheranyOther instance of DefinableMixin.

Returns: boolean

Returns true if other instance of DefinableMixin is equal, else false.


getPropTypes

getPropTypes(): Props

Implementation of Definable

Returns class properties types from whole inheritance tree.

example

@define()
class MyClass extends DefinableMixin {
stringKey: string
constructor(props: Record<keyof any, any>) {
super()
Object.assign(this, props);
}
}
expect(new MyClass({stringKey: 'my-string'}).getPropTypes()).to.be.eql({
stringKey: PropTypes.instanceOf(String)
})

Returns: Props

Plain object representation of properties types.


getPropertyInitializers

getPropertyInitializers(): Props

Implementation of Definable

Returns default values metadata from property initializers conversion for whole inheritance tree.

example

@define()
class MyClass extends Struct {
stringKey = 'my-string';
numberKey = 1337;
constructor(props: Partial<MyClass>) {
super();
Object.assign(this, this.processProps(props));
}
}
expect(new MyClass().getPropertyInitializers()).to.be.eql({
stringKey: 'my-string',
numberKey: 1337
})

Returns: Props

Default values for properties.


toPlainObject

toPlainObject(): Props

Implementation of Definable

Converts properties to plain object.

remarks Loosing object references is required since in scenarios when properties resolved from toPlainObject are transformed - in such changes to the modified plain object will cascade to original instance(THIS instance, since properties are referenced).

example

@define()
class Point extends DefinableMixin {
x: number;
y: number;
z: number;
}
const point = new Point({x: 1, y: 2, z: 3})
expect(point.toPlainObject()).to.be.eql({x: 1, y: 2, z: 3});

Returns: Props

Public properties with assigned values as plain object.


validateProps

validateProps(props: Record‹string | number | symbol, any› | undefined, propTypes: PropTypes, isStrict?: boolean): boolean

Validates if provided properties matches prop types.

throws {ValidationError} Thrown if the passed properties do not match prop types.

remarks Disabling of runtime validation is possible via Kernel's configuration(and by that env flags also) or by annotating class with @validable(false).

This is useful when there is external layer(like transportation) that does all the heavy lifting of validation and there are no other sources of incoming data beside points that is handled by layer.

Use env EVEBLE_VALIDATION_TYPE set to manual to disable validation on initialization. You ca re-enable it again on your application configuration via path validation.type set to runtime before staring application.

example

@define()
class MyClass extends DefinableMixin {
stringKey: string
constructor(props: Record<keyof any, any>) {
super()
Object.assign(this, props);
}
}
const instance = new MyClass({stringKey: 'my-string'});
expect(
() => instance.validateProps({stringKey: 1337}, this.getPropTypes())
).to.throw(ValidationError)

Parameters:

NameTypeDescription
propsRecord‹string | number | symbol, any› | undefinedProperties to validate.
propTypesPropTypesProperties types.
isStrict?booleanFlag indicating that validation should be done in strict mode.

Returns: boolean

Returns true if properties are valid, else throws.


Static getPropTypes

getPropTypes(): Props

Returns class properties types from whole inheritance tree.

example

@define()
class MyClass extends DefinableMixin {
stringKey: string
constructor(props: Record<keyof any, any>) {
super()
Object.assign(this, props);
}
}
expect(MyClass.getPropTypes()).to.be.eql({
stringKey: PropTypes.instanceOf(String)
})

Returns: Props

Plain object representation of properties types.


Static getPropertyInitializers

getPropertyInitializers(): Props

Returns class property initializers for whole inheritance tree.

example

@define()
class MyClass extends Struct {
stringKey = 'my-string';
numberKey = 1337;
constructor(props: Partial<MyClass>) {
super();
Object.assign(this, this.processProps(props));
}
}
expect(MyClass.getPropertyInitializers()).to.be.eql({
stringKey: 'my-string',
numberKey: 1337
})

Returns: Props

Plain object representation of property initializers.