Skip to content

Commit

Permalink
feat: validators loader (#21)
Browse files Browse the repository at this point in the history
feat: Add support to extends validator
  • Loading branch information
adrien2p committed Feb 9, 2022
1 parent bbc3786 commit 451edf9
Show file tree
Hide file tree
Showing 53 changed files with 5,874 additions and 1,205 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
Loading
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
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion docs/.nojekyll

This file was deleted.

Loading

0 comments on commit 451edf9

Please sign in to comment.