Skip to content

Commit

Permalink
Feat() Add support to extends validator (#19)
Browse files Browse the repository at this point in the history
Feat() Add support to extends validator
  • Loading branch information
adrien2p committed Feb 8, 2022
1 parent 89f7223 commit c352d9e
Show file tree
Hide file tree
Showing 47 changed files with 491 additions and 110 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- [Service](#service)
- [Middleware](#middleware)
- [Router](#router)
- [Validator](#validator)
- [Module](#module)
- [Decorators](#decorators)
- [Entity event handling](#entity-event-handling)
Expand Down Expand Up @@ -344,6 +345,30 @@ export class ProductRouter {
```
</details>

### Validator

If you add a custom field on an entity, there is a huge risk that you end up getting
an error as soon as you it the end point with that new field. The medusa validators
are not aware of your new field once the request arrive. In order to handle that
you can extend the class validator in order to add your custom field constraint.

<details>
<summary>Click to see the example!</summary>

```typescript
// modules/product/AdminPostProductsReq.validator.ts

import { Validator } from 'medusa-extender';
import { AdminPostProductsReq } from "@medusajs/medusa/dist";

@Validator({ override: AdminPostProductsReq })
class ExtendedClassValidator extends AdminPostProductsReq {
@IsString()
customField: string;
}
```
</details>

### Module

the last step is to import everything in our module :package:
Expand All @@ -369,7 +394,8 @@ import AddFieldToProduct1611063162649 from './product.20211126000001-add-field-t
ProductService,
ProductRouter,
CustomMiddleware,
AddFieldToProduct1611063162649
AddFieldToProduct1611063162649,
ExtendedClassValidator
]
})
export class MyModule {}
Expand Down
2 changes: 1 addition & 1 deletion assets/coverage/badge-functions.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/coverage/badge-lines.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/coverage/badge-statements.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 27 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ medusa-extender / [Exports](modules.md)
- [Service](#service)
- [Middleware](#middleware)
- [Router](#router)
- [Validator](#validator)
- [Module](#module)
- [Decorators](#decorators)
- [Entity event handling](#entity-event-handling)
Expand Down Expand Up @@ -345,6 +346,30 @@ export class ProductRouter {
```
</details>

### Validator

If you add a custom field on an entity, there is a huge risk that you end up getting
an error as soon as you it the end point with that new field. The medusa validators
are not aware of your new field once the request arrive. In order to handle that
you can extend the class validator in order to add your custom field constraint.

<details>
<summary>Click to see the example!</summary>

```typescript
// modules/product/AdminPostProductsReq.validator.ts

import { Validator } from 'medusa-extender';
import { AdminPostProductsReq } from "@medusajs/medusa/dist";

@Validator({ override: AdminPostProductsReq })
class ExtendedClassValidator extends AdminPostProductsReq {
@IsString()
customField: string;
}
```
</details>

### Module

the last step is to import everything in our module :package:
Expand All @@ -370,7 +395,8 @@ import AddFieldToProduct1611063162649 from './product.20211126000001-add-field-t
ProductService,
ProductRouter,
CustomMiddleware,
AddFieldToProduct1611063162649
AddFieldToProduct1611063162649,
ExtendedClassValidator
]
})
export class MyModule {}
Expand Down
80 changes: 80 additions & 0 deletions docs/classes/Medusa.Medusa-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[medusa-extender](../README.md) / [Exports](../modules.md) / [Medusa](../modules/Medusa.md) / Medusa

# Class: Medusa

[Medusa](../modules/Medusa.md).Medusa

Load medusa and apply all middlewares and migrations before registering the medusa
internal container and database connection.

## Table of contents

### Constructors

- [constructor](Medusa.Medusa-1.md#constructor)

### Properties

- [#express](Medusa.Medusa-1.md##express)
- [#rootDir](Medusa.Medusa-1.md##rootdir)

### Methods

- [load](Medusa.Medusa-1.md#load)

## Constructors

### constructor

**new Medusa**(`rootDir`, `express`)

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `rootDir` | `string` | Directory where the `medusa-config` is located |
| `express` | `Express` | Express instance |

#### Defined in

[src/Medusa.ts:37](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/Medusa.ts#L37)

## Properties

### #express

`Private` `Readonly` **#express**: `Express`

#### Defined in

[src/Medusa.ts:30](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/Medusa.ts#L30)

___

### #rootDir

`Private` `Readonly` **#rootDir**: `string`

#### Defined in

[src/Medusa.ts:31](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/Medusa.ts#L31)

## Methods

### load

**load**(`modules`): `Promise`<`AwilixContainer`<`any`\>\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `modules` | [`Constructor`](../modules/types.md#constructor)<`unknown`\>[] |

#### Returns

`Promise`<`AwilixContainer`<`any`\>\>

#### Defined in

[src/Medusa.ts:45](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/Medusa.ts#L45)
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:42](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L42)
[src/decorators/onMedusaEntityEvent.decorator.ts:42](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L42)

## Properties

Expand All @@ -55,7 +55,7 @@

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:40](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L40)
[src/decorators/onMedusaEntityEvent.decorator.ts:40](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L40)

___

Expand All @@ -65,7 +65,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:39](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L39)
[src/decorators/onMedusaEntityEvent.decorator.ts:39](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L39)

## Accessors

Expand All @@ -79,7 +79,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:50](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L50)
[src/decorators/onMedusaEntityEvent.decorator.ts:50](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L50)

___

Expand All @@ -93,7 +93,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:46](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L46)
[src/decorators/onMedusaEntityEvent.decorator.ts:46](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L46)

## Methods

Expand All @@ -120,7 +120,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:70](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L70)
[src/decorators/onMedusaEntityEvent.decorator.ts:70](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L70)

___

Expand All @@ -146,7 +146,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:58](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L58)
[src/decorators/onMedusaEntityEvent.decorator.ts:58](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L58)

___

Expand All @@ -173,7 +173,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:84](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L84)
[src/decorators/onMedusaEntityEvent.decorator.ts:84](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L84)

___

Expand All @@ -199,7 +199,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:66](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L66)
[src/decorators/onMedusaEntityEvent.decorator.ts:66](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L66)

___

Expand All @@ -226,7 +226,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:77](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L77)
[src/decorators/onMedusaEntityEvent.decorator.ts:77](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L77)

___

Expand All @@ -252,7 +252,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:62](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L62)
[src/decorators/onMedusaEntityEvent.decorator.ts:62](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L62)

___

Expand Down Expand Up @@ -280,7 +280,7 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:91](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L91)
[src/decorators/onMedusaEntityEvent.decorator.ts:91](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L91)

___

Expand All @@ -300,4 +300,4 @@ ___

#### Defined in

[src/decorators/onMedusaEntityEvent.decorator.ts:54](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/decorators/onMedusaEntityEvent.decorator.ts#L54)
[src/decorators/onMedusaEntityEvent.decorator.ts:54](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/decorators/onMedusaEntityEvent.decorator.ts#L54)
12 changes: 6 additions & 6 deletions docs/classes/event_emmiter.Internals.CustomEventEmmiter.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ EventEmitter.constructor

#### Defined in

[src/event-emmiter.ts:21](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/event-emmiter.ts#L21)
[src/event-emmiter.ts:21](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/event-emmiter.ts#L21)

## Properties

Expand All @@ -51,7 +51,7 @@ EventEmitter.constructor

#### Defined in

[src/event-emmiter.ts:19](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/event-emmiter.ts#L19)
[src/event-emmiter.ts:19](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/event-emmiter.ts#L19)

## Methods

Expand Down Expand Up @@ -80,7 +80,7 @@ Emit an asynchrone event entity based and wait for the result.

#### Defined in

[src/event-emmiter.ts:80](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/event-emmiter.ts#L80)
[src/event-emmiter.ts:80](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/event-emmiter.ts#L80)

___

Expand Down Expand Up @@ -110,7 +110,7 @@ Register a new event handler.

#### Defined in

[src/event-emmiter.ts:31](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/event-emmiter.ts#L31)
[src/event-emmiter.ts:31](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/event-emmiter.ts#L31)

___

Expand All @@ -132,7 +132,7 @@ Apply all event handlers hold by the `listenerDescriptor`.

#### Defined in

[src/event-emmiter.ts:43](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/event-emmiter.ts#L43)
[src/event-emmiter.ts:43](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/event-emmiter.ts#L43)

___

Expand All @@ -146,4 +146,4 @@ ___

#### Defined in

[src/event-emmiter.ts:68](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/event-emmiter.ts#L68)
[src/event-emmiter.ts:68](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/event-emmiter.ts#L68)
2 changes: 1 addition & 1 deletion docs/classes/metadata_reader.Internals.CustomMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ Map.get

#### Defined in

[src/metadata-reader.ts:5](https://github.com/adrien2p/medusa-extender/blob/c048da3/src/metadata-reader.ts#L5)
[src/metadata-reader.ts:5](https://github.com/adrien2p/medusa-extender/blob/7e89c01/src/metadata-reader.ts#L5)

0 comments on commit c352d9e

Please sign in to comment.