# runtime-type

> Module for casting to types at runtime.

Latest version **4.1.1** (published 2022-11-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install runtime-type
pnpm add runtime-type
yarn add runtime-type
bun add runtime-type
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.1 |
| Published | 2022-11-17 |
| First published | 2018-05-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Unpacked size | 111.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lukáš Mačuda |
| Maintainers | zabkwak |
| Keywords | data-type, runtime, casting |

## Links

- npm: https://www.npmjs.com/package/runtime-type
- Repository: https://github.com/zabkwak/runtime-type
- Homepage: https://github.com/zabkwak/runtime-type#readme
- Issues: https://github.com/zabkwak/runtime-type/issues
- npm.io page: https://npm.io/package/runtime-type

## Dependencies (1)

- [smart-error](https://npm.io/package/smart-error.md) ^3.0.0

## Recent versions

- 4.1.1 (latest) — 2022-11-17
- 4.1.0 — 2022-11-17
- 4.0.4 — 2021-12-15
- 4.0.3 — 2021-10-05
- 4.0.2 — 2021-10-05
- 4.0.1 — 2021-08-18
- 4.0.0 — 2021-06-03
- 3.8.3 — 2021-01-27
- 3.8.2 — 2020-08-15
- 3.8.1 — 2020-08-10
- 3.8.0 — 2020-08-10
- 3.7.3 — 2020-07-06
- 3.7.2 — 2020-06-11
- 3.7.1 — 2020-05-26
- 3.7.0 — 2020-03-30
- … 25 more at https://npm.io/package/runtime-type/versions

## README

# runtime-type
Module for casting to types at runtime.

## Installation
```
npm install --save runtime-type
```

## Usage
```javascript
import Type from 'runtime-type';

Type.integer.cast('5'); // returns 5
Type.integer.cast('test'); // throws an Error

Type.integer.canCast('5'); // returns true
Type.integer.isValidType('5'); // returns false
Type.integer.isValid('5'); // returns false
```

## Functions
Every type has several methods.

### cast(value)
Casts the value to the type. It returns value in the desired type or throws an `Error`. 

### saveCast(value, defaultValue)
Tries to cast the value. If the cast fails the defaultValue is returned.

### canCast(value)
Checks if the value is valid. It calls the `cast` method and if the cast is successful `true` is returned. If the cast throw an error `false` is returned. 

### isValidType(value)
Checks the value using `typeof` defined in the inherited type.

### isValid(value)
Checks if the value is strictly valid. Calls `canCast(value)` and `isValidType(value)`.

### getTSType()
Returns the TypeScript type.

## Types

| Type                                            | Description                                                                                                                                     | typeof  | JS cast                   |
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------- | ------------------------- |
| any                                             | It's just a placeholder. The value is returned in `cast` method and `canCast` is always `true`.                                                 |         |                           |
| arrayOf(type: Type)                             | The values of array in the `cast` method are casted by the `type`.                                                                              | object  |                           | 
| boolean                                         | The value is validated with the base javascript `Boolean` function in `cast` method. If the value is `'false'` or `'0'` it's casted as `false`. | boolean | Boolean(value)            |
| date                                            | The value is used as parameter in the `Date` constructor in `cast` method.                                                                      | object  | new Date(value)           |
| enum(defaultValue: string, ...values: string[]) | The `cast` method checks if the value is in the `values` array.                                                                                 | string  |                           |
| float                                           | The value is parsed by `parseFloat` function in `cast` method.                                                                                  | number  | parseFloat(value)         |
| instanceOf(cls: Class)                          | Equivivalent of the `instanceof`.                                                                                                               |         | value instanceOf Class    |
| integer                                         | The value is parsed by `parseInt` function in `cast` method. `isValidType` method checks if the value is not decimal.                           | number  | parseInt(value)           |
| object                                          | The value is validated with `typeof value === 'object'`.                                                                                        | object  | typeof value === 'object' |
| shape                                           | The value's keys are validated with the specified types. The key with `?` as last character defines optional key. The key in `[]` defines dynamic field.                                                                                      | object  |                           |
| string           | The value is converted to string with `toString` method.                                                                                        | string  | value.toString()          |
| union(...types: Type[])           | The value is converted to one of the types defined.                                                                                       |   |           |

### enum_
The `enum_` function is alias to `enum` because of typescript.

### Shapes
#### Optional fields
The shapes can have optional fields defined. 
```javascript
import Type from 'runtime-type';

const shape = Type.shape({
	integer: Type.integer,
	'optional?': Type.string,
});
```
#### Dynamic keys
The shapes can have dynamic keys defined. The validated shape can use keys that are not defined in the shape. The dynamic key is checked indefinitely.
```javascript
import Type from 'runtime-type';

const shape = Type.shape({
	integer: Type.integer,
	'[dynamic]': Type.string,
}); // The shape must contain `integer` key and at least one of the `string` keys

const shape2 = Type.shape({
	integer: Type.integer,
	'[dynamic]?': Type.string,
}); // The shape must contain `integer` key and none or indefinitely `string` keys
```

### Union
Special type which checks the types in series and converts value to the valid one. If none of types are valid error is thrown.

## Model
`Model` is base class that supports decorators for typings of its properties.
```javascript
import Type, { Model } from 'runtime-type';

class Test extends Model {

	@Model.type(Type.string)
	test = 'test';
}
```
The `test` property will be cast to string on set of the value in the property.

## Migrating to v3
The `isValid` method now validates the value strictly as mentioned above. The easiest way to migrate is just to rename all references to `canCast`. Everything else remains the same.

```javascript
// v2
Type.integer.isValid('5'); // returns true
// v3
Type.integer.isValid('5'); // returns false
Type.integer.canCast('5'); // returns true
```

---
_Source: https://npm.io/package/runtime-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
