# class-validator-mocker

> ![](https://github.com/gustavolopess/class-validator-mocker/actions/workflows/continuous_integration.yml/badge.svg)

Latest version **0.0.4** (published 2022-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install class-validator-mocker
pnpm add class-validator-mocker
yarn add class-validator-mocker
bun add class-validator-mocker
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2022-02-06 |
| First published | 2022-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Gustavo Lopes |
| Maintainers | gustavolopess |

## Links

- npm: https://www.npmjs.com/package/class-validator-mocker
- Repository: https://github.com/gustavolopess/class-validator-mocker
- Homepage: https://github.com/gustavolopess/class-validator-mocker#readme
- Issues: https://github.com/gustavolopess/class-validator-mocker/issues
- npm.io page: https://npm.io/package/class-validator-mocker

## Dependencies (2)

- [class-validator](https://npm.io/package/class-validator.md) ^0.13.2
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Recent versions

- 0.0.4 (latest) — 2022-02-06
- 0.0.3 — 2022-02-06
- 0.0.2 — 2022-01-29
- 0.0.1 — 2022-01-29

## README

# class-validator-mocker

![](https://github.com/gustavolopess/class-validator-mocker/actions/workflows/continuous_integration.yml/badge.svg)


Uses decorators from [class-validator](https://github.com/typestack/class-validator) to generate
objects filled with random values with types based on used decorators.

## Usage

Once you have a class decorated with [class-validator](https://github.com/typestack/class-validator) decorators, you can create a random mocked object using
```ts
const randomMockedObj = ClassValidatorMocker.create<DecoratedClass>(DecoratedClass);
```

### Example

```ts
import {IsString, IsNumber, IsBoolean, IsUUID} from 'class-validator';
import {ClassValidatorMocker} from 'class-validator-mocker';

class MyClass {
    @IsString()
    stringProperty: string;

    @IsNumber()
    numberProperty: number;

    @IsBoolean()
    booleanProperty: boolean;

    @IsUUID()
    uuidProperty: string;
}

const myClassDataMock = ClassValidatorMocker.create<MyClass>(MyClass);

console.log(myClassDataMock);
```

This code could print something like
```ts
{
    stringProperty: '909f126026795c20df',
    numberProperty: 87310,
    booleanProperty: false,
    uuidProperty: 'd967f1b0-3457-4bac-a465-4e714ebe532a'
}

```

### Passing partial object
The `ClassValidatorMocker.create` method accepts an optional argument - a partial object of the type being handled:

```ts
create<T>(constructor: new () => T, partial: Partial<T> = {}): T
```

If the two last lines from example above would be changed to
```ts
const myClassDataMock = ClassValidatorMocker.create<MyClass>(MyClass, { numberProperty: 12 });

console.log(myClassDataMock);
```
The printed object could change to something like
```ts
{
    stringProperty: '909f126026795c20df',
    numberProperty: 12,
    booleanProperty: false,
    uuidProperty: 'd967f1b0-3457-4bac-a465-4e714ebe532a'
}
```
Meaning that all properties have randomly generated values, except the `numberProperty` who came from `partial` parameter.

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