Typed entity library with attribute sanitization, normalization and validation.
Provides a low-level implementation of data objects represented as entity classes. Each entity class is assigned a collection of attribute configurations that define the value type (string
, number
, custom type, anything...) and other constraints such as optional
(null
values allowed), immutable
(read-only) or hidden
(from JSON representation).
Attribute values can be sanitized from raw/untrusted data (e.g. JSON or user-input) using the sanitizer
function configured for each attribute, as well as normalized and validated by configuring an optional normalizer
and validator
function, respectively.
Callable attributes can also be defined which use a callback to obtain generate and return a typed value.
The core functionality is provided by @typescript-entity/core
.
yarn add @typescript-entity/core
These packages help minimize the code required for declaring common attribute configuration sets. They are entirely optional but you may find them useful.
Attribute configuration set typings and factory functions for common types of attributes.
yarn add @typescript-entity/configs
Normalizer functions for common normalization strategies.
yarn add @typescript-entity/normalizers
Sanitizer functions for common sanitization strategies.
yarn add @typescript-entity/sanitizers
Validator functions for common validation strategies.
yarn add @typescript-entity/validators
A copy/paste version is available in example.ts
Can be found here.
This monorepo contains the official @typescript-entity
packages. It is configured for use with Yarn Workspaces and Lerna. Linting is provided by eslint
using @typescript-eslint/eslint-plugin
according to the @typescript-eslint/recommended
rules. Tests run on jest
using ts-jest
. API documentation is generated using TypeDoc.
Tests reside in ./tests
and provide unit testing across all packages.
yarn run test
Building will lint, compile, test and regenerate API documentation (in that order).
yarn run build