Interface

Validates value against Interface pattern requiring it to match expected properties and available methods(compared only by name - and not by with method parameters). The value may also contain other keys with arbitrary values not defined in pattern(equivalent of Meteor's Match.ObjectIncluding).

returns Returns true if value is matching Interface pattern expectation, else throws.

example

import { check, ValidationError } from 'typend';
interface PersonInterface {
firstName: string;
lastName: string;
getFullName(): string;
}
class Person implements PersonInterface {
firstName: string;
lastName: string;
age: number; // Arbitrary key not listed on PersonInterface
getFullName(): string {
return `${this.firstName} ${this.lastName}`;
}
constructor(firstName: string, lastName: string, age: number) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
check<PersonInterface>(new Person('Jane', 'Doe', 175));
expect(
() => check<PersonInterface>({ firstName: 'Jane', lastName: 'Don' }) // Missing getFullName method
).to.throw(ValidationError);

Hierarchy

Pattern

  • Pattern

    Interface

Implements

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

+ new Interface(properties: Record‹keyof any, any›): Interface

Overrides Pattern.constructor

Creates an instance of an Interface(pattern). Creates an instance of an Interface(pattern).

throws {InvalidTypeError} Thrown if provided properties is not an plain object or instance of Collection pattern.

throws {InvalidTypeError} Thrown if provided properties is not an plain object or instance of Collection pattern.

Parameters:

NameTypeDescription
propertiesRecord‹keyof any, any›Properties that describes interface.

Returns: Interface

Properties

Static describer

describer: Describer

Inherited from Pattern.describer

Overrides void


Static kind

kind: KINDS = KINDS.INTERFACE

Overrides Pattern.kind

Accessors

isOptional

get isOptional(): Optional

Inherited from Pattern.isOptional

Overrides void

Returns: Optional


isRequired

get isRequired(): any

Inherited from Pattern.isRequired

Overrides void

Returns: any

Methods

describe

describe(value: any): string

Inherited from Pattern.describe

Overrides void

Parameters:

NameType
valueany

Returns: string


getInitializer

getInitializer(): any | undefined

Implementation of Pattern

Inherited from Pattern.getInitializer

Overrides void

Returns: any | undefined


getKind

getKind(): string

Implementation of Pattern

Inherited from Pattern.getKind

Overrides void

Returns: string


getName

getName(): string

Returns interface name from metadata.

Returns: string

Interface name as a string.


hasInitializer

hasInitializer(): boolean

Implementation of Pattern

Inherited from Pattern.hasInitializer

Overrides void

Returns: boolean


hasOwnProperty

hasOwnProperty(v: PropertyKey): boolean

Inherited from Pattern.hasOwnProperty

Overrides Pattern.hasOwnProperty

Determines whether an object has a property with the specified name.

Parameters:

NameTypeDescription
vPropertyKeyA property name.

Returns: boolean


isPrototypeOf

isPrototypeOf(v: Object): boolean

Inherited from Pattern.isPrototypeOf

Overrides Pattern.isPrototypeOf

Determines whether an object exists in another object's prototype chain.

Parameters:

NameTypeDescription
vObjectAnother object whose prototype chain is to be checked.

Returns: boolean


propertyIsEnumerable

propertyIsEnumerable(v: PropertyKey): boolean

Inherited from Pattern.propertyIsEnumerable

Overrides Pattern.propertyIsEnumerable

Determines whether a specified property is enumerable.

Parameters:

NameTypeDescription
vPropertyKeyA property name.

Returns: boolean


setInitializer

setInitializer(initializer: any): void

Implementation of Pattern

Inherited from Pattern.setInitializer

Overrides void

Parameters:

NameType
initializerany

Returns: void


setName

setName(name: string): void

Sets interface name as type's metadata.

Parameters:

NameTypeDescription
namestringInterface name.

Returns: void


toLocaleString

toLocaleString(): string

Inherited from Pattern.toLocaleString

Overrides Pattern.toLocaleString

Returns a date converted to a string using the current locale.

Returns: string


toString

toString(): string

Inherited from Pattern.toString

Overrides Pattern.toString

Returns a string representation of an object.

Returns: string


valueOf

valueOf(): Object

Inherited from Pattern.valueOf

Overrides Pattern.valueOf

Returns the primitive value of the specified object.

Returns: Object


Static getDescriber

getDescriber(): Describer

Inherited from Pattern.getDescriber

Overrides void

Returns: Describer


Static setDescriber

setDescriber(describer: Describer): void

Inherited from Pattern.setDescriber

Overrides void

Parameters:

NameType
describerDescriber

Returns: void