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.
Before creating your own, check the data types reference to see if libmodulor
does not already provide it.
If you don't find your "bonheur" there, check out the example below.
Example
As an example, defining an ISIN as a string
is too approximative.
Indeed, it has a specific length, format, etc.
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.