libmodulor v0.13.1 is out 🚀 !
libmodulor
Guides

Create a data type

Use cases define input/output using specific data types.

Intro

A data type is a class that extends the TBase class directly or indirectly.

libmodulor provides data types that are commonly used. But some apps need specific data types.

Example

As an example, defining an ISIN as a string is too approximative. Indeed, it has a specific length, format, etc.

src/lib/TISIN.ts
export type ISIN = Capitalize<string>;

export class TISIN extends TString<ISIN, 'ISIN'> {
    public static readonly FORMAT: RegExp = /^[A-Z]{2}[A-Z0-9]{9}[0-9]$/;

    constructor(constraints?: TStringConstraints) {
        super({
            ...constraints,
            format: { f: 'ISIN', regexp: TISIN.FORMAT },
        });
    }

    public override tName(): TName {
        return 'ISIN';
    }

    public override example(): ISIN {
        return 'US02079K3059';
    }
}

It defines two things that need to be exported :

  • A type (e.g. ISIN that specifies the data type with a TypeScript type)
  • A class (e.g. TISIN that specifies how the data type behaves)

libmodulor provides base data types that you can extend.

i18n

In the example above we've defined a format for the ISIN.

When the end user inputs an invalid ISIN, it will trigger an error with the following key: validation_format_ISIN.

This key can be translated in the app's i18n. Check Translate an app for more details.

On this page